0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/controllers/storage/localfilesystem.js
jamesbloomer 6e44280b96 Moving file system storage to a module
issue #635

- refactored file system storage into module
- convert save to return a promise
- convert admin controller to use storage module
2013-10-31 08:25:24 +00:00

86 lines
No EOL
2.9 KiB
JavaScript

// # Local File System Image Storage module
// The (default) module for storing images, using the local file system
var errors = require('../../errorHandling'),
fs = require('fs-extra'),
moment = require('moment'),
nodefn = require('when/node/function'),
path = require('path'),
when = require('when');
var localfilesystem;
// TODO: this could be a separate module
function getUniqueFileName(dir, name, ext, i, done) {
var filename,
append = '';
if (i) {
append = '-' + i;
}
filename = path.join(dir, name + append + ext);
fs.exists(filename, function (exists) {
if (exists) {
setImmediate(function () {
i = i + 1;
return getUniqueFileName(dir, name, ext, i, done);
});
} else {
return done(filename);
}
});
}
// ## Module interface
localfilesystem = {
// TODO use promises!!
// QUESTION pass date or month and year? And should the date be ticks or an object? Gone with ticks.
// QUESTION feels wrong to pass in the ghostUrl, the local file system needs it but something like S3 won't?
// ### Save
// Saves the image to storage (the file system)
// - date is current date in ticks
// - image is the express image object
// - ghosturl is thr base url for the site
// - returns a promise which ultimately returns the full url to the uploaded image
'save': function (date, image, ghostUrl) {
// QUESTION is it okay for this module to know about content/images?
var saved = when.defer(),
m = moment(date),
month = m.format('MMM'),
year = m.format('YYYY'),
target_dir = path.join('content/images', year, month),
target_path = path.join(target_dir, image.name),
ext = path.extname(image.name),
basename = path.basename(image.name, ext).replace(/[\W]/gi, '_');
getUniqueFileName(target_dir, basename, ext, null, function (filename) {
fs.mkdirs(target_dir, function (err) {
if (err) {
errors.logError(err);
return saved.reject();
}
fs.copy(image.path, target_path, function (err) {
if (err) {
errors.logError(err);
return saved.reject();
}
// NOTE as every upload will need to delete the tmp file make this the admin controllers job
// The src for the image must be in URI format, not a file system path, which in Windows uses \
var fullUrl = path.join(ghostUrl, filename).replace(new RegExp('\\' + path.sep, 'g'), '/');
return saved.resolve(fullUrl);
});
});
});
return saved.promise;
}
};
module.exports = localfilesystem;