2016-08-22 03:18:31 +00:00
|
|
|
"use strict";
|
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
var path = require('path'),
|
2016-09-25 04:08:49 +00:00
|
|
|
loki = require('lokijs'),
|
|
|
|
Promise = require('bluebird'),
|
2016-09-29 01:58:18 +00:00
|
|
|
fs = Promise.promisifyAll(require('fs-extra')),
|
2016-10-03 04:12:29 +00:00
|
|
|
multer = require('multer'),
|
2016-08-22 03:18:31 +00:00
|
|
|
_ = require('lodash');
|
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
var regFolderName = new RegExp("^[a-z0-9][a-z0-9\-]*[a-z0-9]$");
|
|
|
|
|
2016-08-22 03:18:31 +00:00
|
|
|
/**
|
|
|
|
* Local Data Storage
|
|
|
|
*
|
|
|
|
* @param {Object} appconfig The application configuration
|
|
|
|
*/
|
2016-09-19 03:03:19 +00:00
|
|
|
module.exports = {
|
2016-08-22 03:18:31 +00:00
|
|
|
|
2016-09-19 03:03:19 +00:00
|
|
|
_uploadsPath: './repo/uploads',
|
2016-09-25 04:08:49 +00:00
|
|
|
_uploadsThumbsPath: './data/thumbs',
|
2016-09-19 03:03:19 +00:00
|
|
|
_uploadsFolders: [],
|
2016-09-25 04:08:49 +00:00
|
|
|
_uploadsDb: null,
|
2016-08-22 03:18:31 +00:00
|
|
|
|
2016-10-03 04:12:29 +00:00
|
|
|
uploadImgHandler: null,
|
|
|
|
|
2016-09-19 03:03:19 +00:00
|
|
|
/**
|
|
|
|
* Initialize Local Data Storage model
|
|
|
|
*
|
|
|
|
* @param {Object} appconfig The application config
|
|
|
|
* @return {Object} Local Data Storage model instance
|
|
|
|
*/
|
2016-09-25 04:08:49 +00:00
|
|
|
init(appconfig, mode = 'server') {
|
2016-09-19 03:03:19 +00:00
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
2016-09-25 04:08:49 +00:00
|
|
|
self._uploadsPath = path.resolve(ROOTPATH, appconfig.datadir.repo, 'uploads');
|
|
|
|
self._uploadsThumbsPath = path.resolve(ROOTPATH, appconfig.datadir.db, 'thumbs');
|
2016-09-19 03:03:19 +00:00
|
|
|
|
2016-10-03 04:12:29 +00:00
|
|
|
// Finish initialization tasks
|
2016-09-25 04:08:49 +00:00
|
|
|
|
|
|
|
switch(mode) {
|
|
|
|
case 'agent':
|
|
|
|
//todo
|
|
|
|
break;
|
|
|
|
case 'server':
|
|
|
|
self.createBaseDirectories(appconfig);
|
2016-10-03 04:12:29 +00:00
|
|
|
self.initMulter(appconfig);
|
2016-09-25 04:08:49 +00:00
|
|
|
break;
|
|
|
|
case 'ws':
|
|
|
|
self.initDb(appconfig);
|
|
|
|
break;
|
2016-09-19 03:03:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-09-25 04:08:49 +00:00
|
|
|
/**
|
|
|
|
* Initialize Uploads DB
|
|
|
|
*
|
|
|
|
* @param {Object} appconfig The application config
|
|
|
|
* @return {boolean} Void
|
|
|
|
*/
|
|
|
|
initDb(appconfig) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
let dbReadyResolve;
|
|
|
|
let dbReady = new Promise((resolve, reject) => {
|
|
|
|
dbReadyResolve = resolve;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Initialize Loki.js
|
|
|
|
|
|
|
|
let dbModel = {
|
|
|
|
Store: new loki(path.join(appconfig.datadir.db, 'uploads.db'), {
|
|
|
|
env: 'NODEJS',
|
|
|
|
autosave: true,
|
|
|
|
autosaveInterval: 15000
|
|
|
|
}),
|
|
|
|
onReady: dbReady
|
|
|
|
};
|
|
|
|
|
|
|
|
// Load Models
|
|
|
|
|
|
|
|
dbModel.Store.loadDatabase({}, () => {
|
|
|
|
|
|
|
|
dbModel.Files = dbModel.Store.getCollection('Files');
|
|
|
|
if(!dbModel.Files) {
|
|
|
|
dbModel.Files = dbModel.Store.addCollection('Files', {
|
|
|
|
indices: ['category', 'folder']
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
dbReadyResolve();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
self._uploadsDb = dbModel;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-10-03 04:12:29 +00:00
|
|
|
/**
|
|
|
|
* Init Multer upload handlers
|
|
|
|
*
|
|
|
|
* @param {Object} appconfig The application config
|
|
|
|
* @return {boolean} Void
|
|
|
|
*/
|
|
|
|
initMulter(appconfig) {
|
|
|
|
|
|
|
|
this.uploadImgHandler = multer({
|
|
|
|
storage: multer.diskStorage({
|
|
|
|
destination: (req, f, cb) => {
|
|
|
|
cb(null, path.resolve(ROOTPATH, appconfig.datadir.db, 'temp-upload'))
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
fileFilter: (req, f, cb) => {
|
|
|
|
|
|
|
|
//-> Check filesize (3 MB max)
|
|
|
|
|
|
|
|
if(f.size > 3145728) {
|
|
|
|
return cb(null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-> Check MIME type (quick check only)
|
|
|
|
|
|
|
|
if(!_.includes(['image/png', 'image/jpeg', 'image/gif', 'image/webp'], f.mimetype)) {
|
|
|
|
return cb(null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, true);
|
|
|
|
}
|
|
|
|
}).array('imgfile', 20);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-09-25 04:08:49 +00:00
|
|
|
/**
|
|
|
|
* Gets the thumbnails folder path.
|
|
|
|
*
|
|
|
|
* @return {String} The thumbs path.
|
|
|
|
*/
|
|
|
|
getThumbsPath() {
|
|
|
|
return this._uploadsThumbsPath;
|
|
|
|
},
|
|
|
|
|
2016-09-19 03:03:19 +00:00
|
|
|
/**
|
|
|
|
* Creates a base directories (Synchronous).
|
|
|
|
*
|
|
|
|
* @param {Object} appconfig The application config
|
|
|
|
* @return {Void} Void
|
|
|
|
*/
|
|
|
|
createBaseDirectories(appconfig) {
|
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.info('[SERVER] Checking data directories...');
|
2016-09-19 03:03:19 +00:00
|
|
|
|
|
|
|
try {
|
2016-09-29 01:58:18 +00:00
|
|
|
fs.ensureDirSync(path.resolve(ROOTPATH, appconfig.datadir.db));
|
|
|
|
fs.ensureDirSync(path.resolve(ROOTPATH, appconfig.datadir.db, './cache'));
|
|
|
|
fs.ensureDirSync(path.resolve(ROOTPATH, appconfig.datadir.db, './thumbs'));
|
2016-10-03 04:12:29 +00:00
|
|
|
fs.ensureDirSync(path.resolve(ROOTPATH, appconfig.datadir.db, './temp-upload'));
|
2016-09-19 03:03:19 +00:00
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
fs.ensureDirSync(path.resolve(ROOTPATH, appconfig.datadir.repo));
|
|
|
|
fs.ensureDirSync(path.resolve(ROOTPATH, appconfig.datadir.repo, './uploads'));
|
2016-09-19 03:03:19 +00:00
|
|
|
} catch (err) {
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.error(err);
|
2016-08-22 03:18:31 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
winston.info('[SERVER] Data and Repository directories are OK.');
|
2016-09-19 03:03:19 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2016-09-25 04:08:49 +00:00
|
|
|
},
|
|
|
|
|
2016-09-29 01:58:18 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-10-03 04:12:29 +00:00
|
|
|
/**
|
|
|
|
* Check if folder is valid and exists
|
|
|
|
*
|
|
|
|
* @param {String} folderName The folder name
|
|
|
|
* @return {Boolean} True if valid
|
|
|
|
*/
|
|
|
|
validateUploadsFolder(folderName) {
|
|
|
|
|
|
|
|
folderName = (_.includes(this._uploadsFolders, folderName)) ? folderName : '';
|
|
|
|
return path.resolve(this._uploadsPath, folderName);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if filename is valid and unique
|
|
|
|
*
|
|
|
|
* @param {String} f The filename
|
|
|
|
* @param {String} fld The containing folder
|
|
|
|
* @return {Promise<String>} Promise of the accepted filename
|
|
|
|
*/
|
|
|
|
validateUploadsFilename(f, fld) {
|
|
|
|
|
|
|
|
let fObj = path.parse(f);
|
|
|
|
let fname = _.chain(fObj.name).trim().toLower().kebabCase().value().replace(/[^a-z0-9\-]+/g, '');
|
|
|
|
let fext = _.toLower(fObj.ext);
|
|
|
|
|
|
|
|
if(!_.includes(['.jpg', '.jpeg', '.png', '.gif', '.webp'], fext)) {
|
|
|
|
fext = '.png';
|
|
|
|
}
|
|
|
|
|
|
|
|
f = fname + fext;
|
|
|
|
let fpath = path.resolve(this._uploadsPath, fld, f);
|
|
|
|
|
|
|
|
return fs.statAsync(fpath).then((s) => {
|
|
|
|
throw new Error('File ' + f + ' already exists.');
|
|
|
|
}).catch((err) => {
|
|
|
|
if(err.code === 'ENOENT') {
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-09-25 04:08:49 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the uploads files.
|
|
|
|
*
|
|
|
|
* @param {String} cat Category type
|
|
|
|
* @param {String} fld Folder
|
|
|
|
* @return {Array<Object>} The files matching the query
|
|
|
|
*/
|
|
|
|
getUploadsFiles(cat, fld) {
|
|
|
|
|
2016-09-25 21:32:39 +00:00
|
|
|
return this._uploadsDb.Files.chain().find({
|
2016-09-25 04:08:49 +00:00
|
|
|
'$and': [{ 'category' : cat },{ 'folder' : fld }]
|
2016-09-25 21:32:39 +00:00
|
|
|
}).simplesort('filename').data();
|
2016-09-25 04:08:49 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate thumbnail of image
|
|
|
|
*
|
|
|
|
* @param {String} sourcePath The source path
|
|
|
|
* @return {Promise<Object>} Promise returning the resized image info
|
|
|
|
*/
|
|
|
|
generateThumbnail(sourcePath, destPath) {
|
|
|
|
|
|
|
|
let sharp = require('sharp');
|
|
|
|
|
|
|
|
return sharp(sourcePath)
|
|
|
|
.withoutEnlargement()
|
|
|
|
.resize(150,150)
|
|
|
|
.background('white')
|
|
|
|
.embed()
|
|
|
|
.flatten()
|
|
|
|
.toFormat('png')
|
|
|
|
.toFile(destPath);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the image metadata.
|
|
|
|
*
|
|
|
|
* @param {String} sourcePath The source path
|
|
|
|
* @return {Object} The image metadata.
|
|
|
|
*/
|
|
|
|
getImageMetadata(sourcePath) {
|
|
|
|
|
|
|
|
let sharp = require('sharp');
|
|
|
|
|
|
|
|
return sharp(sourcePath).metadata();
|
|
|
|
|
2016-08-22 03:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|