2013-11-07 11:00:39 -05:00
|
|
|
// # Local File System Image Storage module
|
|
|
|
// The (default) module for storing images, using the local file system
|
|
|
|
|
2015-09-27 14:14:09 -05:00
|
|
|
var serveStatic = require('express').static,
|
2016-08-23 07:07:25 -05:00
|
|
|
fs = require('fs-extra'),
|
|
|
|
path = require('path'),
|
|
|
|
util = require('util'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
execFileAsPromise = Promise.promisify(require('child_process').execFile),
|
|
|
|
errors = require('../errors'),
|
|
|
|
config = require('../config'),
|
|
|
|
utils = require('../utils'),
|
|
|
|
BaseStore = require('./base');
|
2013-11-07 11:00:39 -05:00
|
|
|
|
2014-08-28 22:48:49 -05:00
|
|
|
function LocalFileStore() {
|
2016-08-22 16:51:42 -05:00
|
|
|
BaseStore.call(this);
|
2014-08-28 22:48:49 -05:00
|
|
|
}
|
2016-08-23 07:07:25 -05:00
|
|
|
|
2016-08-22 16:51:42 -05:00
|
|
|
util.inherits(LocalFileStore, BaseStore);
|
2013-11-07 11:00:39 -05:00
|
|
|
|
2014-08-28 22:48:49 -05:00
|
|
|
// ### Save
|
|
|
|
// Saves the image to storage (the file system)
|
|
|
|
// - image is the express image object
|
|
|
|
// - returns a promise which ultimately returns the full url to the uploaded image
|
2014-12-14 06:31:00 -05:00
|
|
|
LocalFileStore.prototype.save = function (image, targetDir) {
|
|
|
|
targetDir = targetDir || this.getTargetDir(config.paths.imagesPath);
|
|
|
|
var targetFilename;
|
2013-11-07 11:00:39 -05:00
|
|
|
|
2014-08-28 22:48:49 -05:00
|
|
|
return this.getUniqueFileName(this, image, targetDir).then(function (filename) {
|
|
|
|
targetFilename = filename;
|
|
|
|
return Promise.promisify(fs.mkdirs)(targetDir);
|
|
|
|
}).then(function () {
|
|
|
|
return Promise.promisify(fs.copy)(image.path, targetFilename);
|
|
|
|
}).then(function () {
|
|
|
|
// The src for the image must be in URI format, not a file system path, which in Windows uses \
|
|
|
|
// For local file system storage can use relative path so add a slash
|
|
|
|
var fullUrl = (config.paths.subdir + '/' + config.paths.imagesRelPath + '/' +
|
2016-08-23 07:07:25 -05:00
|
|
|
path.relative(config.paths.imagesPath, targetFilename)).replace(new RegExp('\\' + path.sep, 'g'), '/');
|
2014-08-28 22:48:49 -05:00
|
|
|
return fullUrl;
|
|
|
|
}).catch(function (e) {
|
|
|
|
errors.logError(e);
|
|
|
|
return Promise.reject(e);
|
|
|
|
});
|
|
|
|
};
|
2013-11-07 11:00:39 -05:00
|
|
|
|
2014-08-28 22:48:49 -05:00
|
|
|
LocalFileStore.prototype.exists = function (filename) {
|
|
|
|
return new Promise(function (resolve) {
|
2015-03-12 15:12:56 -05:00
|
|
|
fs.stat(filename, function (err) {
|
|
|
|
var exists = !err;
|
2014-08-28 22:48:49 -05:00
|
|
|
resolve(exists);
|
2013-11-07 11:00:39 -05:00
|
|
|
});
|
2014-08-28 22:48:49 -05:00
|
|
|
});
|
|
|
|
};
|
2013-11-07 11:00:39 -05:00
|
|
|
|
2014-08-28 22:48:49 -05:00
|
|
|
// middleware for serving the files
|
2016-08-23 07:07:25 -05:00
|
|
|
LocalFileStore.prototype.serve = function (options) {
|
|
|
|
var self = this;
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
// CASE: serve themes
|
|
|
|
// serveStatic can't be used to serve themes, because
|
|
|
|
// download files depending on the route (see `send` npm module)
|
|
|
|
if (options.isTheme) {
|
|
|
|
return function downloadTheme(req, res, next) {
|
|
|
|
var themeName = options.name,
|
|
|
|
zipName = themeName + '.zip',
|
|
|
|
zipPath = config.paths.themePath + '/' + zipName,
|
|
|
|
stream;
|
|
|
|
|
|
|
|
self.exists(zipPath)
|
|
|
|
.then(function (zipExists) {
|
|
|
|
if (!zipExists) {
|
|
|
|
return execFileAsPromise('zip', ['-r', zipName, themeName], {cwd: config.paths.themePath});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
res.set({
|
|
|
|
'Content-disposition': 'attachment; filename={themeName}.zip'.replace('{themeName}', themeName),
|
|
|
|
'Content-Type': 'application/zip'
|
|
|
|
});
|
|
|
|
|
|
|
|
stream = fs.createReadStream(zipPath);
|
|
|
|
stream.pipe(res);
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// CASE: serve images
|
|
|
|
// For some reason send divides the max age number by 1000
|
|
|
|
// Fallthrough: false ensures that if an image isn't found, it automatically 404s
|
|
|
|
return serveStatic(config.paths.imagesPath, {maxAge: utils.ONE_YEAR_MS, fallthrough: false});
|
|
|
|
}
|
2014-08-28 22:48:49 -05:00
|
|
|
};
|
2013-11-07 11:00:39 -05:00
|
|
|
|
2016-08-23 07:07:25 -05:00
|
|
|
LocalFileStore.prototype.delete = function (fileName, targetDir) {
|
|
|
|
targetDir = targetDir || this.getTargetDir(config.paths.imagesPath);
|
|
|
|
|
|
|
|
var path = targetDir + '/' + fileName;
|
|
|
|
return Promise.promisify(fs.remove)(path);
|
2016-08-22 16:51:42 -05:00
|
|
|
};
|
|
|
|
|
2014-08-28 22:48:49 -05:00
|
|
|
module.exports = LocalFileStore;
|