2016-10-09 05:26:25 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var path = require('path'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
fs = Promise.promisifyAll(require('fs-extra')),
|
|
|
|
readChunk = require('read-chunk'),
|
|
|
|
fileType = require('file-type'),
|
2016-12-22 01:38:12 +00:00
|
|
|
mime = require('mime-types'),
|
2016-10-09 05:26:25 +00:00
|
|
|
farmhash = require('farmhash'),
|
|
|
|
moment = require('moment'),
|
|
|
|
chokidar = require('chokidar'),
|
2016-10-16 23:09:43 +00:00
|
|
|
sharp = require('sharp'),
|
2016-10-09 05:26:25 +00:00
|
|
|
_ = require('lodash');
|
|
|
|
|
|
|
|
/**
|
2017-01-10 03:45:56 +00:00
|
|
|
* Uploads - Agent
|
2016-10-09 05:26:25 +00:00
|
|
|
*/
|
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
_uploadsPath: './repo/uploads',
|
|
|
|
_uploadsThumbsPath: './data/thumbs',
|
|
|
|
|
|
|
|
_watcher: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Uploads model
|
|
|
|
*
|
|
|
|
* @return {Object} Uploads model instance
|
|
|
|
*/
|
2017-01-10 03:45:56 +00:00
|
|
|
init() {
|
2016-10-09 05:26:25 +00:00
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
self._uploadsPath = path.resolve(ROOTPATH, appconfig.paths.repo, 'uploads');
|
|
|
|
self._uploadsThumbsPath = path.resolve(ROOTPATH, appconfig.paths.data, 'thumbs');
|
2016-10-09 05:26:25 +00:00
|
|
|
|
2016-10-16 23:09:43 +00:00
|
|
|
// Disable Sharp cache, as it cause file locks issues when deleting uploads.
|
|
|
|
sharp.cache(false);
|
|
|
|
|
2016-10-09 05:26:25 +00:00
|
|
|
return self;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-10-16 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Watch the uploads folder for changes
|
|
|
|
*
|
|
|
|
* @return {Void} Void
|
|
|
|
*/
|
2016-10-09 05:26:25 +00:00
|
|
|
watch() {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
self._watcher = chokidar.watch(self._uploadsPath, {
|
|
|
|
persistent: true,
|
|
|
|
ignoreInitial: true,
|
|
|
|
cwd: self._uploadsPath,
|
|
|
|
depth: 1,
|
|
|
|
awaitWriteFinish: true
|
|
|
|
});
|
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
//-> Add new upload file
|
|
|
|
|
2016-10-09 05:26:25 +00:00
|
|
|
self._watcher.on('add', (p) => {
|
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
let pInfo = self.parseUploadsRelPath(p);
|
2016-10-09 05:26:25 +00:00
|
|
|
return self.processFile(pInfo.folder, pInfo.filename).then((mData) => {
|
2016-10-17 03:24:19 +00:00
|
|
|
return db.UplFile.findByIdAndUpdate(mData._id, mData, { upsert: true });
|
2016-10-09 05:26:25 +00:00
|
|
|
}).then(() => {
|
2016-10-15 03:31:15 +00:00
|
|
|
return git.commitUploads('Uploaded ' + p);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
//-> Remove upload file
|
|
|
|
|
|
|
|
self._watcher.on('unlink', (p) => {
|
|
|
|
|
|
|
|
let pInfo = self.parseUploadsRelPath(p);
|
2016-10-17 03:24:19 +00:00
|
|
|
return git.commitUploads('Deleted/Renamed ' + p);
|
2016-10-09 05:26:25 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
/**
|
|
|
|
* Initial Uploads scan
|
|
|
|
*
|
|
|
|
* @return {Promise<Void>} Promise of the scan operation
|
|
|
|
*/
|
|
|
|
initialScan() {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
return fs.readdirAsync(self._uploadsPath).then((ls) => {
|
|
|
|
|
|
|
|
// Get all folders
|
|
|
|
|
|
|
|
return Promise.map(ls, (f) => {
|
|
|
|
return fs.statAsync(path.join(self._uploadsPath, f)).then((s) => { return { filename: f, stat: s }; });
|
|
|
|
}).filter((s) => { return s.stat.isDirectory(); }).then((arrDirs) => {
|
|
|
|
|
|
|
|
let folderNames = _.map(arrDirs, 'filename');
|
|
|
|
folderNames.unshift('');
|
|
|
|
|
|
|
|
// Add folders to DB
|
|
|
|
|
|
|
|
return db.UplFolder.remove({}).then(() => {
|
|
|
|
return db.UplFolder.insertMany(_.map(folderNames, (f) => {
|
2016-10-17 03:24:19 +00:00
|
|
|
return {
|
|
|
|
_id: 'f:' + f,
|
|
|
|
name: f
|
|
|
|
};
|
2016-10-15 03:31:15 +00:00
|
|
|
}));
|
|
|
|
}).then(() => {
|
|
|
|
|
|
|
|
// Travel each directory and scan files
|
|
|
|
|
|
|
|
let allFiles = [];
|
|
|
|
|
|
|
|
return Promise.map(folderNames, (fldName) => {
|
|
|
|
|
|
|
|
let fldPath = path.join(self._uploadsPath, fldName);
|
|
|
|
return fs.readdirAsync(fldPath).then((fList) => {
|
|
|
|
return Promise.map(fList, (f) => {
|
|
|
|
return upl.processFile(fldName, f).then((mData) => {
|
|
|
|
if(mData) {
|
|
|
|
allFiles.push(mData);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}, {concurrency: 3});
|
|
|
|
});
|
|
|
|
}, {concurrency: 1}).finally(() => {
|
|
|
|
|
|
|
|
// Add files to DB
|
|
|
|
|
|
|
|
return db.UplFile.remove({}).then(() => {
|
|
|
|
if(_.isArray(allFiles) && allFiles.length > 0) {
|
|
|
|
return db.UplFile.insertMany(allFiles);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}).then(() => {
|
|
|
|
|
|
|
|
// Watch for new changes
|
|
|
|
|
|
|
|
return upl.watch();
|
|
|
|
|
2016-10-17 23:52:04 +00:00
|
|
|
});
|
2016-10-15 03:31:15 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse relative Uploads path
|
|
|
|
*
|
|
|
|
* @param {String} f Relative Uploads path
|
|
|
|
* @return {Object} Parsed path (folder and filename)
|
|
|
|
*/
|
|
|
|
parseUploadsRelPath(f) {
|
|
|
|
|
|
|
|
let fObj = path.parse(f);
|
|
|
|
return {
|
|
|
|
folder: fObj.dir,
|
|
|
|
filename: fObj.base
|
|
|
|
};
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-10-16 23:09:43 +00:00
|
|
|
/**
|
|
|
|
* Get metadata from file and generate thumbnails if necessary
|
|
|
|
*
|
|
|
|
* @param {String} fldName The folder name
|
|
|
|
* @param {String} f The filename
|
|
|
|
* @return {Promise<Object>} Promise of the file metadata
|
|
|
|
*/
|
2016-10-09 05:26:25 +00:00
|
|
|
processFile(fldName, f) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
let fldPath = path.join(self._uploadsPath, fldName);
|
|
|
|
let fPath = path.join(fldPath, f);
|
|
|
|
let fPathObj = path.parse(fPath);
|
|
|
|
let fUid = farmhash.fingerprint32(fldName + '/' + f);
|
|
|
|
|
|
|
|
return fs.statAsync(fPath).then((s) => {
|
|
|
|
|
|
|
|
if(!s.isFile()) { return false; }
|
|
|
|
|
|
|
|
// Get MIME info
|
|
|
|
|
|
|
|
let mimeInfo = fileType(readChunk.sync(fPath, 0, 262));
|
2016-12-22 01:38:12 +00:00
|
|
|
if(_.isNil(mimeInfo)) {
|
|
|
|
mimeInfo = {
|
|
|
|
mime: mime.lookup(fPathObj.ext) || 'application/octet-stream'
|
|
|
|
};
|
|
|
|
}
|
2016-10-09 05:26:25 +00:00
|
|
|
|
|
|
|
// Images
|
|
|
|
|
|
|
|
if(s.size < 3145728) { // ignore files larger than 3MB
|
|
|
|
if(_.includes(['image/png', 'image/jpeg', 'image/gif', 'image/webp'], mimeInfo.mime)) {
|
2016-10-15 03:31:15 +00:00
|
|
|
return self.getImageMetadata(fPath).then((mImgData) => {
|
2016-10-09 05:26:25 +00:00
|
|
|
|
|
|
|
let cacheThumbnailPath = path.parse(path.join(self._uploadsThumbsPath, fUid + '.png'));
|
|
|
|
let cacheThumbnailPathStr = path.format(cacheThumbnailPath);
|
|
|
|
|
2016-10-15 03:31:15 +00:00
|
|
|
let mData = {
|
|
|
|
_id: fUid,
|
|
|
|
category: 'image',
|
|
|
|
mime: mimeInfo.mime,
|
|
|
|
extra: _.pick(mImgData, ['format', 'width', 'height', 'density', 'hasAlpha', 'orientation']),
|
2016-10-16 05:34:34 +00:00
|
|
|
folder: 'f:' + fldName,
|
2016-10-15 03:31:15 +00:00
|
|
|
filename: f,
|
|
|
|
basename: fPathObj.name,
|
|
|
|
filesize: s.size
|
2016-10-17 23:52:04 +00:00
|
|
|
};
|
2016-10-09 05:26:25 +00:00
|
|
|
|
|
|
|
// Generate thumbnail
|
|
|
|
|
|
|
|
return fs.statAsync(cacheThumbnailPathStr).then((st) => {
|
|
|
|
return st.isFile();
|
|
|
|
}).catch((err) => {
|
|
|
|
return false;
|
|
|
|
}).then((thumbExists) => {
|
|
|
|
|
|
|
|
return (thumbExists) ? mData : fs.ensureDirAsync(cacheThumbnailPath.dir).then(() => {
|
|
|
|
return self.generateThumbnail(fPath, cacheThumbnailPathStr);
|
|
|
|
}).return(mData);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-10-17 23:56:05 +00:00
|
|
|
});
|
2016-10-09 05:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other Files
|
|
|
|
|
|
|
|
return {
|
2016-10-15 03:31:15 +00:00
|
|
|
_id: fUid,
|
|
|
|
category: 'binary',
|
2016-10-09 05:26:25 +00:00
|
|
|
mime: mimeInfo.mime,
|
2016-12-22 01:38:12 +00:00
|
|
|
folder: 'f:' + fldName,
|
2016-10-09 05:26:25 +00:00
|
|
|
filename: f,
|
|
|
|
basename: fPathObj.name,
|
2016-10-15 03:31:15 +00:00
|
|
|
filesize: s.size
|
2016-10-09 05:26:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate thumbnail of image
|
|
|
|
*
|
|
|
|
* @param {String} sourcePath The source path
|
2016-10-17 23:52:04 +00:00
|
|
|
* @param {String} destPath The destination path
|
2016-10-09 05:26:25 +00:00
|
|
|
* @return {Promise<Object>} Promise returning the resized image info
|
|
|
|
*/
|
|
|
|
generateThumbnail(sourcePath, destPath) {
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
return sharp(sourcePath).metadata();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|