Removed Search-Index and LokiJS, replaced with Mongo + Updated VueJs
This commit is contained in:
133
models/ws/search.js
Normal file
133
models/ws/search.js
Normal file
@@ -0,0 +1,133 @@
|
||||
"use strict";
|
||||
|
||||
const Promise = require('bluebird'),
|
||||
_ = require('lodash'),
|
||||
path = require('path');
|
||||
|
||||
/**
|
||||
* Search Model
|
||||
*/
|
||||
module.exports = {
|
||||
|
||||
_si: null,
|
||||
|
||||
/**
|
||||
* Initialize Search model
|
||||
*
|
||||
* @param {Object} appconfig The application config
|
||||
* @return {Object} Search model instance
|
||||
*/
|
||||
init(appconfig) {
|
||||
|
||||
let self = this;
|
||||
|
||||
return self;
|
||||
|
||||
},
|
||||
|
||||
find(terms) {
|
||||
|
||||
let self = this;
|
||||
terms = _.chain(terms)
|
||||
.deburr()
|
||||
.toLower()
|
||||
.trim()
|
||||
.replace(/[^a-z0-9 ]/g, '')
|
||||
.split(' ')
|
||||
.filter((f) => { return !_.isEmpty(f); })
|
||||
.join(' ')
|
||||
.value();
|
||||
|
||||
return db.Entry.find(
|
||||
{ $text: { $search: terms } },
|
||||
{ score: { $meta: "textScore" }, title: 1 }
|
||||
)
|
||||
.sort({ score: { $meta: "textScore" } })
|
||||
.limit(10)
|
||||
.exec()
|
||||
.then((hits) => {
|
||||
|
||||
/*if(hits.length < 5) {
|
||||
return self._si.matchAsync({
|
||||
beginsWith: terms,
|
||||
threshold: 3,
|
||||
limit: 5,
|
||||
type: 'simple'
|
||||
}).then((matches) => {
|
||||
|
||||
return {
|
||||
match: hits,
|
||||
suggest: matches
|
||||
};
|
||||
|
||||
});
|
||||
} else {*/
|
||||
return {
|
||||
match: hits,
|
||||
suggest: []
|
||||
};
|
||||
//}
|
||||
|
||||
}).catch((err) => {
|
||||
|
||||
if(err.type === 'NotFoundError') {
|
||||
return {
|
||||
match: [],
|
||||
suggest: []
|
||||
};
|
||||
} else {
|
||||
winston.error(err);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Delete an entry from the index
|
||||
*
|
||||
* @param {String} The entry path
|
||||
* @return {Promise} Promise of the operation
|
||||
*/
|
||||
delete(entryPath) {
|
||||
|
||||
let self = this;
|
||||
/*let hasResults = false;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
self._si.search({
|
||||
query: {
|
||||
AND: { 'entryPath': [entryPath] }
|
||||
}
|
||||
}).on('data', (results) => {
|
||||
|
||||
hasResults = true;
|
||||
|
||||
if(results.totalHits > 0) {
|
||||
let delIds = _.map(results.hits, 'id');
|
||||
self._si.del(delIds).on('end', () => { return resolve(true); });
|
||||
} else {
|
||||
resolve(true);
|
||||
}
|
||||
|
||||
}).on('error', (err) => {
|
||||
|
||||
if(err.type === 'NotFoundError') {
|
||||
resolve(true);
|
||||
} else {
|
||||
winston.error(err);
|
||||
reject(err);
|
||||
}
|
||||
|
||||
}).on('end', () => {
|
||||
if(!hasResults) {
|
||||
resolve(true);
|
||||
}
|
||||
});
|
||||
|
||||
});*/
|
||||
|
||||
}
|
||||
|
||||
};
|
160
models/ws/uploads.js
Normal file
160
models/ws/uploads.js
Normal file
@@ -0,0 +1,160 @@
|
||||
"use strict";
|
||||
|
||||
var path = require('path'),
|
||||
Promise = require('bluebird'),
|
||||
fs = Promise.promisifyAll(require('fs-extra')),
|
||||
multer = require('multer'),
|
||||
_ = require('lodash');
|
||||
|
||||
var regFolderName = new RegExp("^[a-z0-9][a-z0-9\-]*[a-z0-9]$");
|
||||
|
||||
/**
|
||||
* Uploads
|
||||
*/
|
||||
module.exports = {
|
||||
|
||||
_uploadsPath: './repo/uploads',
|
||||
_uploadsThumbsPath: './data/thumbs',
|
||||
|
||||
/**
|
||||
* Initialize Local Data Storage model
|
||||
*
|
||||
* @param {Object} appconfig The application config
|
||||
* @return {Object} Uploads model instance
|
||||
*/
|
||||
init(appconfig) {
|
||||
|
||||
this._uploadsPath = path.resolve(ROOTPATH, appconfig.paths.repo, 'uploads');
|
||||
this._uploadsThumbsPath = path.resolve(ROOTPATH, appconfig.paths.data, 'thumbs');
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the thumbnails folder path.
|
||||
*
|
||||
* @return {String} The thumbs path.
|
||||
*/
|
||||
getThumbsPath() {
|
||||
return this._uploadsThumbsPath;
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the uploads folders.
|
||||
*
|
||||
* @param {Array<String>} arrFolders The arr folders
|
||||
* @return {Void} Void
|
||||
*/
|
||||
setUploadsFolders(arrFolders) {
|
||||
|
||||
this._uploadsFolders = arrFolders;
|
||||
return;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the uploads folders.
|
||||
*
|
||||
* @return {Array<String>} The uploads folders.
|
||||
*/
|
||||
getUploadsFolders() {
|
||||
return this._uploadsFolders;
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates an uploads folder.
|
||||
*
|
||||
* @param {String} folderName The folder name
|
||||
* @return {Promise} Promise of the operation
|
||||
*/
|
||||
createUploadsFolder(folderName) {
|
||||
|
||||
let self = this;
|
||||
|
||||
folderName = _.kebabCase(_.trim(folderName));
|
||||
|
||||
if(_.isEmpty(folderName) || !regFolderName.test(folderName)) {
|
||||
return Promise.resolve(self.getUploadsFolders());
|
||||
}
|
||||
|
||||
return fs.ensureDirAsync(path.join(self._uploadsPath, folderName)).then(() => {
|
||||
if(!_.includes(self._uploadsFolders, folderName)) {
|
||||
self._uploadsFolders.push(folderName);
|
||||
self._uploadsFolders = _.sortBy(self._uploadsFolders);
|
||||
}
|
||||
return self.getUploadsFolders();
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Check if folder is valid and exists
|
||||
*
|
||||
* @param {String} folderName The folder name
|
||||
* @return {Boolean} True if valid
|
||||
*/
|
||||
validateUploadsFolder(folderName) {
|
||||
|
||||
if(_.includes(this._uploadsFolders, folderName)) {
|
||||
return path.resolve(this._uploadsPath, folderName);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the uploads files.
|
||||
*
|
||||
* @param {Array<Object>} arrFiles The uploads files
|
||||
* @return {Void} Void
|
||||
*/
|
||||
setUploadsFiles(arrFiles) {
|
||||
|
||||
let self = this;
|
||||
|
||||
/*if(_.isArray(arrFiles) && arrFiles.length > 0) {
|
||||
self._uploadsDb.Files.clear();
|
||||
self._uploadsDb.Files.insert(arrFiles);
|
||||
self._uploadsDb.Files.ensureIndex('category', true);
|
||||
self._uploadsDb.Files.ensureIndex('folder', true);
|
||||
}*/
|
||||
|
||||
return;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Adds one or more uploads files.
|
||||
*
|
||||
* @param {Array<Object>} arrFiles The uploads files
|
||||
* @return {Void} Void
|
||||
*/
|
||||
addUploadsFiles(arrFiles) {
|
||||
if(_.isArray(arrFiles) || _.isPlainObject(arrFiles)) {
|
||||
//this._uploadsDb.Files.insert(arrFiles);
|
||||
}
|
||||
return;
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the uploads files.
|
||||
*
|
||||
* @param {String} cat Category type
|
||||
* @param {String} fld Folder
|
||||
* @return {Array<Object>} The files matching the query
|
||||
*/
|
||||
getUploadsFiles(cat, fld) {
|
||||
|
||||
return /*this._uploadsDb.Files.chain().find({
|
||||
'$and': [{ 'category' : cat },{ 'folder' : fld }]
|
||||
}).simplesort('filename').data()*/;
|
||||
|
||||
},
|
||||
|
||||
deleteUploadsFile(fldName, f) {
|
||||
|
||||
}
|
||||
|
||||
};
|
Reference in New Issue
Block a user