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