mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
2957b0175e
closes #3252 - added `/ghost/api/v0.1/uploads/` endpoint - removed upload method from `controller/admin.js` - moved removal of temporary files from storage to endpoint (needed to account for failed uploads) - changed and moved tests - Oversight: I think that we use `.otherwise()` and `.catch()` a bit too extensive and mask the real error objects. We probably need an error handling strategy at some point in the future.
65 lines
2.3 KiB
JavaScript
65 lines
2.3 KiB
JavaScript
// # Local File System Image Storage module
|
|
// The (default) module for storing images, using the local file system
|
|
|
|
var _ = require('lodash'),
|
|
express = require('express'),
|
|
fs = require('fs-extra'),
|
|
nodefn = require('when/node'),
|
|
path = require('path'),
|
|
when = require('when'),
|
|
errors = require('../errors'),
|
|
config = require('../config'),
|
|
baseStore = require('./base'),
|
|
|
|
localFileStore;
|
|
|
|
localFileStore = _.extend(baseStore, {
|
|
// ### 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
|
|
'save': function (image) {
|
|
var saved = when.defer(),
|
|
targetDir = this.getTargetDir(config().paths.imagesPath),
|
|
targetFilename;
|
|
|
|
this.getUniqueFileName(this, image, targetDir).then(function (filename) {
|
|
targetFilename = filename;
|
|
return nodefn.call(fs.mkdirs, targetDir);
|
|
}).then(function () {
|
|
return nodefn.call(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 + '/' + path.relative(config().paths.imagesPath, targetFilename)).replace(new RegExp('\\' + path.sep, 'g'), '/');
|
|
return saved.resolve(fullUrl);
|
|
}).otherwise(function (e) {
|
|
errors.logError(e);
|
|
return saved.reject(e);
|
|
});
|
|
|
|
return saved.promise;
|
|
},
|
|
|
|
'exists': function (filename) {
|
|
// fs.exists does not play nicely with nodefn because the callback doesn't have an error argument
|
|
var done = when.defer();
|
|
|
|
fs.exists(filename, function (exists) {
|
|
done.resolve(exists);
|
|
});
|
|
|
|
return done.promise;
|
|
},
|
|
|
|
// middleware for serving the files
|
|
'serve': function () {
|
|
var ONE_HOUR_MS = 60 * 60 * 1000,
|
|
ONE_YEAR_MS = 365 * 24 * ONE_HOUR_MS;
|
|
|
|
// For some reason send divides the max age number by 1000
|
|
return express['static'](config().paths.imagesPath, {maxAge: ONE_YEAR_MS});
|
|
}
|
|
});
|
|
|
|
module.exports = localFileStore;
|