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'),
|
|
|
|
config = require('../config'),
|
2016-10-04 18:33:43 +03:00
|
|
|
errors = require('../errors'),
|
|
|
|
i18n = require('../i18n'),
|
2016-08-23 15:07:25 +03:00
|
|
|
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
|
2016-11-09 11:17:27 +03:00
|
|
|
LocalFileStore.prototype.save = function save(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 = (
|
2016-11-14 17:38:55 +03:00
|
|
|
utils.url.urlJoin('/', utils.url.getSubdir(),
|
2017-02-03 17:42:05 +03:00
|
|
|
utils.url.STATIC_IMAGE_URL_PREFIX,
|
2016-11-14 17:38:55 +03:00
|
|
|
path.relative(config.getContentPath('images'), targetFilename))
|
2016-09-13 23:24:57 +03:00
|
|
|
).replace(new RegExp('\\' + path.sep, 'g'), '/');
|
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
return fullUrl;
|
|
|
|
}).catch(function (e) {
|
|
|
|
return Promise.reject(e);
|
|
|
|
});
|
|
|
|
};
|
2013-11-07 20:00:39 +04:00
|
|
|
|
2016-11-09 11:17:27 +03:00
|
|
|
LocalFileStore.prototype.exists = function exists(filename) {
|
2014-08-29 07:48:49 +04:00
|
|
|
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-11-09 11:17:27 +03:00
|
|
|
LocalFileStore.prototype.serve = function serve(options) {
|
2016-08-23 15:07:25 +03:00
|
|
|
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-10-04 18:33:43 +03:00
|
|
|
// Wrap server static errors
|
|
|
|
return function serveStaticContent(req, res, next) {
|
|
|
|
return serveStatic(config.getContentPath('images'), {maxAge: utils.ONE_YEAR_MS, fallthrough: false})(req, res, function (err) {
|
|
|
|
if (err) {
|
|
|
|
if (err.statusCode === 404) {
|
2016-10-06 15:27:35 +03:00
|
|
|
return next(new errors.NotFoundError({message: i18n.t('errors.errors.pageNotFound')}));
|
2016-10-04 18:33:43 +03:00
|
|
|
}
|
|
|
|
|
2016-10-06 15:27:35 +03:00
|
|
|
return next(new errors.GhostError({err: err}));
|
2016-10-04 18:33:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
};
|
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-11-09 11:17:27 +03:00
|
|
|
LocalFileStore.prototype.delete = function deleteFile(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
|
|
|
};
|
|
|
|
|
2016-11-09 11:17:27 +03:00
|
|
|
/**
|
|
|
|
* Reads bytes from disk for a target image
|
|
|
|
* path: path of target image (without content path!)
|
|
|
|
*/
|
|
|
|
LocalFileStore.prototype.read = function read(options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
// remove trailing slashes
|
|
|
|
options.path = (options.path || '').replace(/\/$|\\$/, '');
|
|
|
|
|
|
|
|
var targetPath = path.join(config.getContentPath('images'), options.path);
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
fs.readFile(targetPath, function (err, bytes) {
|
|
|
|
if (err) {
|
|
|
|
return reject(new errors.GhostError({
|
|
|
|
err: err,
|
|
|
|
message: 'Could not read image: ' + targetPath
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(bytes);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-08-29 07:48:49 +04:00
|
|
|
module.exports = LocalFileStore;
|