0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/api/upload.js
Nazar Gargol 0faf89b5ab Added ability to resize and compress images on upload (#9837)
refs #4453

* On by default

* Added config to disable resizing

* Added basic image optimization processing

* Added dep: sharp (optional dep)

* Added resize middleware

* Take care of rotation based on EXIF information

* Removed all meta data from optimised image

* Added handling if sharp could not get installed

* Do not read ext twice - optimisation

* Do not call sharp if config is disabled

* Do not remove the original image which was uploaded (store 2 images)

* Support of `req.files` for internal logic

* Disabled cache to enable file removal on Windows
2018-08-30 17:30:36 +01:00

46 lines
1.2 KiB
JavaScript

const Promise = require('bluebird'),
fs = require('fs-extra'),
storage = require('../adapters/storage');
let upload;
/**
* ## Upload API Methods
*
* **See:** [API Methods](constants.js.html#api%20methods)
*/
upload = {
/**
* ### Add Image
*
* We only allow multiple uploads internally - see images middlewares.
*
* @public
* @param {{context}} options
* @returns {Promise<String>} location of uploaded file
*/
add: Promise.method(function (options) {
const store = storage.getStorage();
if (options.files) {
return Promise.map(options.files, (file) => {
return store
.save(file)
.finally(function () {
// Remove uploaded file from tmp location
return fs.unlink(file.path);
});
}).then((paths) => {
return paths[0];
});
}
return store.save(options).finally(function () {
// Remove uploaded file from tmp location
return fs.unlink(options.path);
});
})
};
module.exports = upload;