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
|
|
|
|
|
|
|
|
var _ = require('underscore'),
|
|
|
|
express = require('express'),
|
|
|
|
fs = require('fs-extra'),
|
|
|
|
nodefn = require('when/node/function'),
|
|
|
|
path = require('path'),
|
|
|
|
when = require('when'),
|
|
|
|
errors = require('../errorHandling'),
|
2013-12-09 04:49:02 +04:00
|
|
|
config = require('../config'),
|
2013-11-07 20:00:39 +04:00
|
|
|
baseStore = require('./base'),
|
|
|
|
|
2013-12-09 04:49:02 +04:00
|
|
|
localFileStore;
|
2013-11-07 20:00:39 +04:00
|
|
|
|
|
|
|
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(),
|
2013-12-09 04:49:02 +04:00
|
|
|
targetDir = this.getTargetDir(config.paths().imagesRelPath);
|
2013-11-07 20:00:39 +04:00
|
|
|
|
|
|
|
this.getUniqueFileName(this, image, targetDir).then(function (filename) {
|
|
|
|
nodefn.call(fs.mkdirs, targetDir).then(function () {
|
|
|
|
return nodefn.call(fs.copy, image.path, filename);
|
|
|
|
}).then(function () {
|
|
|
|
// we should remove the temporary image
|
|
|
|
return nodefn.call(fs.unlink, image.path).otherwise(errors.logError);
|
|
|
|
}).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 = ('/' + filename).replace(new RegExp('\\' + path.sep, 'g'), '/');
|
|
|
|
return saved.resolve(fullUrl);
|
|
|
|
}).otherwise(function (e) {
|
|
|
|
errors.logError(e);
|
|
|
|
return saved.reject(e);
|
|
|
|
});
|
|
|
|
}).otherwise(errors.logError);
|
|
|
|
|
|
|
|
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 () {
|
2013-12-09 04:49:02 +04:00
|
|
|
return express['static'](config.paths().imagesPath);
|
2013-11-07 20:00:39 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = localFileStore;
|