2018-07-12 13:40:37 +02:00
|
|
|
const Promise = require('bluebird'),
|
2017-09-12 16:31:14 +01:00
|
|
|
fs = require('fs-extra'),
|
2018-09-27 16:06:57 +02:00
|
|
|
storage = require('../../adapters/storage');
|
2018-07-12 13:40:37 +02:00
|
|
|
|
|
|
|
let upload;
|
2014-07-15 12:40:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Upload API Methods
|
|
|
|
*
|
2017-12-14 14:13:40 +01:00
|
|
|
* **See:** [API Methods](constants.js.html#api%20methods)
|
2014-07-15 12:40:14 +02:00
|
|
|
*/
|
|
|
|
upload = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Add Image
|
|
|
|
*
|
2018-08-30 18:30:36 +02:00
|
|
|
* We only allow multiple uploads internally - see images middlewares.
|
|
|
|
*
|
2014-07-15 12:40:14 +02:00
|
|
|
* @public
|
|
|
|
* @param {{context}} options
|
2016-03-29 22:31:31 -05:00
|
|
|
* @returns {Promise<String>} location of uploaded file
|
2014-07-15 12:40:14 +02:00
|
|
|
*/
|
2018-09-18 19:29:56 +05:30
|
|
|
add: Promise.method((options) => {
|
2018-08-30 18:30:36 +02:00
|
|
|
const store = storage.getStorage();
|
|
|
|
|
|
|
|
if (options.files) {
|
|
|
|
return Promise.map(options.files, (file) => {
|
|
|
|
return store
|
|
|
|
.save(file)
|
2018-09-18 19:29:56 +05:30
|
|
|
.finally(() => {
|
2018-08-30 18:30:36 +02:00
|
|
|
// Remove uploaded file from tmp location
|
|
|
|
return fs.unlink(file.path);
|
|
|
|
});
|
|
|
|
}).then((paths) => {
|
|
|
|
return paths[0];
|
|
|
|
});
|
|
|
|
}
|
2016-03-29 22:31:31 -05:00
|
|
|
|
2018-09-18 19:29:56 +05:30
|
|
|
return store.save(options).finally(() => {
|
2014-07-15 12:40:14 +02:00
|
|
|
// Remove uploaded file from tmp location
|
2017-12-13 20:03:07 +01:00
|
|
|
return fs.unlink(options.path);
|
2014-07-15 12:40:14 +02:00
|
|
|
});
|
2016-03-29 22:31:31 -05:00
|
|
|
})
|
2014-07-15 12:40:14 +02:00
|
|
|
};
|
|
|
|
|
2014-08-17 06:17:23 +00:00
|
|
|
module.exports = upload;
|