mirror of
https://github.com/TryGhost/Ghost.git
synced 2023-12-13 21:00:40 +01:00
6ff51ad44e
fixes #4607 - moves file checks from db and upload API endpoints to api utils - adds code to accept and then extract a zip and pull out a JSON file - zip handling requires a lot of dependencies - this needs a good refactor
49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
var config = require('../config'),
|
|
Promise = require('bluebird'),
|
|
fs = require('fs-extra'),
|
|
storage = require('../storage'),
|
|
errors = require('../errors'),
|
|
utils = require('./utils'),
|
|
|
|
upload;
|
|
|
|
/**
|
|
* ## 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(),
|
|
filepath;
|
|
|
|
// Check if a file was provided
|
|
if (!utils.checkFileExists(options, 'uploadimage')) {
|
|
return Promise.reject(new errors.NoPermissionError('Please select an image.'));
|
|
}
|
|
|
|
// Check if the file is valid
|
|
if (!utils.checkFileIsValid(options.uploadimage, config.uploads.contentTypes, config.uploads.extensions)) {
|
|
return Promise.reject(new errors.UnsupportedMediaTypeError('Please select a valid image.'));
|
|
}
|
|
|
|
filepath = options.uploadimage.path;
|
|
|
|
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;
|