mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
66845def85
addresses #2852 - Moves storage modules to use prototypes and to create prototypes that inherit from the base storage ctor. - Makes storage/base conform to an all Promise interface.
60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
var Promise = require('bluebird'),
|
|
path = require('path'),
|
|
fs = require('fs-extra'),
|
|
storage = require('../storage'),
|
|
errors = require('../errors'),
|
|
|
|
upload;
|
|
|
|
function isImage(type, ext) {
|
|
if ((type === 'image/jpeg' || type === 'image/png' || type === 'image/gif' || type === 'image/svg+xml')
|
|
&& (ext === '.jpg' || ext === '.jpeg' || ext === '.png' || ext === '.gif' || ext === '.svg' || ext === '.svgz')) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* ## Upload API Methods
|
|
*
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
*/
|
|
upload = {
|
|
|
|
/**
|
|
* ### Add Image
|
|
*
|
|
* @public
|
|
* @param {{context}} options
|
|
* @returns {Promise} Success
|
|
*/
|
|
add: function (options) {
|
|
var store = storage.getStorage(),
|
|
type,
|
|
ext,
|
|
filepath;
|
|
|
|
if (!options.uploadimage || !options.uploadimage.type || !options.uploadimage.path) {
|
|
return Promise.reject(new errors.NoPermissionError('Please select an image.'));
|
|
}
|
|
|
|
type = options.uploadimage.type;
|
|
ext = path.extname(options.uploadimage.name).toLowerCase();
|
|
filepath = options.uploadimage.path;
|
|
|
|
return Promise.resolve(isImage(type, ext)).then(function (result) {
|
|
if (!result) {
|
|
return Promise.reject(new errors.UnsupportedMediaTypeError('Please select a valid image.'));
|
|
}
|
|
}).then(function () {
|
|
return store.save(options.uploadimage);
|
|
}).then(function (url) {
|
|
return url;
|
|
}).finally(function () {
|
|
// Remove uploaded file from tmp location
|
|
return Promise.promisify(fs.unlink)(filepath);
|
|
});
|
|
}
|
|
};
|
|
|
|
module.exports = upload;
|