2016-08-18 22:25:51 +03:00
|
|
|
var Promise = require('bluebird'),
|
2017-09-12 18:31:14 +03:00
|
|
|
fs = require('fs-extra'),
|
2016-03-30 06:31:31 +03:00
|
|
|
pUnlink = Promise.promisify(fs.unlink),
|
2017-05-15 13:52:01 +03:00
|
|
|
storage = require('../adapters/storage'),
|
2014-07-15 14:40:14 +04:00
|
|
|
upload;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ## Upload API Methods
|
|
|
|
*
|
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
|
|
*/
|
|
|
|
upload = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Add Image
|
|
|
|
*
|
|
|
|
* @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
|
|
|
*/
|
2016-03-30 06:31:31 +03:00
|
|
|
add: Promise.method(function (options) {
|
|
|
|
var store = storage.getStorage();
|
|
|
|
|
|
|
|
return store.save(options).finally(function () {
|
2014-07-15 14:40:14 +04:00
|
|
|
// Remove uploaded file from tmp location
|
2016-03-30 06:31:31 +03:00
|
|
|
return pUnlink(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;
|