2013-11-07 20:00:39 +04:00
|
|
|
// # Local File System Image Storage module
|
|
|
|
// The (default) module for storing images, using the local file system
|
|
|
|
|
2015-09-27 22:14:09 +03:00
|
|
|
var serveStatic = require('express').static,
|
2016-08-23 15:07:25 +03:00
|
|
|
fs = require('fs-extra'),
|
2016-08-25 10:22:22 +03:00
|
|
|
os = require('os'),
|
2016-08-23 15:07:25 +03:00
|
|
|
path = require('path'),
|
|
|
|
util = require('util'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
errors = require('../errors'),
|
|
|
|
config = require('../config'),
|
|
|
|
utils = require('../utils'),
|
2016-08-25 10:22:22 +03:00
|
|
|
BaseStore = require('./base'),
|
|
|
|
remove = Promise.promisify(fs.remove);
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
function LocalFileStore() {
|
2016-08-23 00:51:42 +03:00
|
|
|
BaseStore.call(this);
|
2014-08-29 07:48:49 +04:00
|
|
|
}
|
2016-08-23 15:07:25 +03:00
|
|
|
|
2016-08-23 00:51:42 +03:00
|
|
|
util.inherits(LocalFileStore, BaseStore);
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2014-08-29 07:48:49 +04: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 14:31:00 +03:00
|
|
|
LocalFileStore.prototype.save = function (image, targetDir) {
|
2016-09-13 23:24:57 +03:00
|
|
|
targetDir = targetDir || this.getTargetDir(config.getContentPath('images'));
|
2014-12-14 14:31:00 +03:00
|
|
|
var targetFilename;
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2014-08-29 07:48:49 +04: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
|
2016-09-13 23:24:57 +03:00
|
|
|
var fullUrl = (
|
|
|
|
utils.url.getSubdir() + '/' +
|
|
|
|
config.get('paths').imagesRelPath +
|
|
|
|
'/' +
|
|
|
|
path.relative(config.getContentPath('images'), targetFilename)
|
|
|
|
).replace(new RegExp('\\' + path.sep, 'g'), '/');
|
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
return fullUrl;
|
|
|
|
}).catch(function (e) {
|
|
|
|
errors.logError(e);
|
|
|
|
return Promise.reject(e);
|
|
|
|
});
|
|
|
|
};
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
LocalFileStore.prototype.exists = function (filename) {
|
|
|
|
return new Promise(function (resolve) {
|
2015-03-12 23:12:56 +03:00
|
|
|
fs.stat(filename, function (err) {
|
|
|
|
var exists = !err;
|
2014-08-29 07:48:49 +04:00
|
|
|
resolve(exists);
|
2013-11-07 20:00:39 +04:00
|
|
|
});
|
2014-08-29 07:48:49 +04:00
|
|
|
});
|
|
|
|
};
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
// middleware for serving the files
|
2016-08-23 15:07:25 +03:00
|
|
|
LocalFileStore.prototype.serve = function (options) {
|
|
|
|
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,
|
2016-09-13 23:24:57 +03:00
|
|
|
themePath = path.join(config.getContentPath('themes'), themeName),
|
2016-08-23 15:07:25 +03:00
|
|
|
zipName = themeName + '.zip',
|
2016-08-25 10:22:22 +03:00
|
|
|
// store this in a unique temporary folder
|
|
|
|
zipBasePath = path.join(os.tmpdir(), utils.uid(10)),
|
|
|
|
zipPath = path.join(zipBasePath, zipName),
|
2016-08-23 15:07:25 +03:00
|
|
|
stream;
|
|
|
|
|
2016-08-25 10:22:22 +03:00
|
|
|
Promise.promisify(fs.ensureDir)(zipBasePath)
|
2016-08-23 15:07:25 +03:00
|
|
|
.then(function () {
|
2016-08-25 10:22:22 +03:00
|
|
|
return Promise.promisify(utils.zipFolder)(themePath, zipPath);
|
|
|
|
})
|
|
|
|
.then(function (length) {
|
2016-08-23 15:07:25 +03:00
|
|
|
res.set({
|
|
|
|
'Content-disposition': 'attachment; filename={themeName}.zip'.replace('{themeName}', themeName),
|
2016-08-25 10:22:22 +03:00
|
|
|
'Content-Type': 'application/zip',
|
|
|
|
'Content-Length': length
|
2016-08-23 15:07:25 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
stream = fs.createReadStream(zipPath);
|
|
|
|
stream.pipe(res);
|
|
|
|
})
|
|
|
|
.catch(function (err) {
|
|
|
|
next(err);
|
2016-08-25 10:22:22 +03:00
|
|
|
})
|
|
|
|
.finally(function () {
|
|
|
|
remove(zipBasePath);
|
2016-08-23 15:07:25 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
} 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
|
2016-09-13 23:24:57 +03:00
|
|
|
return serveStatic(config.getContentPath('images'), {maxAge: utils.ONE_YEAR_MS, fallthrough: false});
|
2016-08-23 15:07:25 +03:00
|
|
|
}
|
2014-08-29 07:48:49 +04:00
|
|
|
};
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2016-08-23 15:07:25 +03:00
|
|
|
LocalFileStore.prototype.delete = function (fileName, targetDir) {
|
2016-09-13 23:24:57 +03:00
|
|
|
targetDir = targetDir || this.getTargetDir(config.getContentPath('images'));
|
2016-08-23 15:07:25 +03:00
|
|
|
|
2016-08-25 10:22:22 +03:00
|
|
|
var pathToDelete = path.join(targetDir, fileName);
|
|
|
|
return remove(pathToDelete);
|
2016-08-23 00:51:42 +03:00
|
|
|
};
|
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
module.exports = LocalFileStore;
|