2019-05-14 11:18:15 +02:00
|
|
|
'use strict';
|
2018-03-24 12:08:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Public node modules.
|
2018-05-04 18:27:39 +02:00
|
|
|
/* eslint-disable prefer-template */
|
2019-05-14 11:18:15 +02:00
|
|
|
const cloudinary = require('cloudinary').v2;
|
|
|
|
const intoStream = require('into-stream');
|
2018-03-24 12:08:07 +01:00
|
|
|
|
|
|
|
module.exports = {
|
2019-05-14 11:18:15 +02:00
|
|
|
provider: 'cloudinary',
|
|
|
|
name: 'Cloudinary',
|
2018-03-24 12:08:07 +01:00
|
|
|
auth: {
|
|
|
|
cloud_name: {
|
2019-05-14 11:18:15 +02:00
|
|
|
label: 'Cloud name',
|
|
|
|
type: 'text',
|
2018-03-24 12:08:07 +01:00
|
|
|
},
|
|
|
|
api_key: {
|
2019-05-14 11:18:15 +02:00
|
|
|
label: 'API Key',
|
|
|
|
type: 'text',
|
2018-03-24 12:08:07 +01:00
|
|
|
},
|
|
|
|
api_secret: {
|
2019-05-14 11:18:15 +02:00
|
|
|
label: 'API Secret',
|
|
|
|
type: 'password',
|
2019-04-27 12:09:07 +02:00
|
|
|
},
|
2018-03-24 12:08:07 +01:00
|
|
|
},
|
2019-04-27 12:09:07 +02:00
|
|
|
init: config => {
|
2018-03-24 12:08:07 +01:00
|
|
|
cloudinary.config({
|
|
|
|
cloud_name: config.cloud_name,
|
|
|
|
api_key: config.api_key,
|
2019-04-27 12:09:07 +02:00
|
|
|
api_secret: config.api_secret,
|
2018-03-24 12:08:07 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
2019-04-27 12:09:07 +02:00
|
|
|
upload(file) {
|
2018-03-24 12:08:07 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-04-27 12:09:07 +02:00
|
|
|
const upload_stream = cloudinary.uploader.upload_stream(
|
2019-05-14 11:18:15 +02:00
|
|
|
{ resource_type: 'auto' },
|
2019-04-27 12:09:07 +02:00
|
|
|
(err, image) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
file.url = image.secure_url;
|
2019-08-05 13:05:54 +02:00
|
|
|
file.provider_metadata = {
|
|
|
|
public_id: image.public_id,
|
|
|
|
resource_type: image.resource_type,
|
|
|
|
};
|
2019-04-27 12:09:07 +02:00
|
|
|
resolve();
|
2019-07-29 15:45:46 +02:00
|
|
|
}
|
2019-04-27 12:09:07 +02:00
|
|
|
);
|
2018-03-24 12:08:07 +01:00
|
|
|
intoStream(file.buffer).pipe(upload_stream);
|
2018-05-04 18:27:39 +02:00
|
|
|
});
|
2018-03-24 12:08:07 +01:00
|
|
|
},
|
2019-04-27 12:09:07 +02:00
|
|
|
async delete(file) {
|
2018-03-24 12:08:07 +01:00
|
|
|
try {
|
2019-08-05 13:05:54 +02:00
|
|
|
const { resource_type, public_id } = file.provider_metadata;
|
|
|
|
const response = await cloudinary.uploader.destroy(public_id, {
|
2019-04-27 12:09:07 +02:00
|
|
|
invalidate: true,
|
2019-08-05 13:05:54 +02:00
|
|
|
resource_type: resource_type || 'image',
|
2018-03-24 12:08:07 +01:00
|
|
|
});
|
2019-05-14 11:18:15 +02:00
|
|
|
if (response.result !== 'ok') {
|
2018-03-24 12:08:07 +01:00
|
|
|
throw {
|
2019-04-27 12:09:07 +02:00
|
|
|
error: new Error(response.result),
|
2018-05-04 18:27:39 +02:00
|
|
|
};
|
2018-03-24 12:08:07 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
throw error.error;
|
|
|
|
}
|
2019-04-27 12:09:07 +02:00
|
|
|
},
|
2018-03-24 12:08:07 +01:00
|
|
|
};
|
2019-04-27 12:09:07 +02:00
|
|
|
},
|
2018-03-24 12:08:07 +01:00
|
|
|
};
|