2019-05-14 11:18:15 +02:00
|
|
|
'use strict';
|
2018-03-24 12:08:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Public node modules.
|
2019-05-14 11:18:15 +02:00
|
|
|
const cloudinary = require('cloudinary').v2;
|
|
|
|
const intoStream = require('into-stream');
|
2021-04-29 13:51:12 +02:00
|
|
|
const { errors } = require('@strapi/plugin-upload');
|
2018-03-24 12:08:07 +01:00
|
|
|
|
|
|
|
module.exports = {
|
2020-02-27 19:34:14 +01:00
|
|
|
init(config) {
|
|
|
|
cloudinary.config(config);
|
2018-03-24 12:08:07 +01:00
|
|
|
|
|
|
|
return {
|
2020-02-27 19:34:14 +01:00
|
|
|
upload(file, customConfig = {}) {
|
2018-03-24 12:08:07 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-01-20 16:10:29 +01:00
|
|
|
const config = {
|
|
|
|
resource_type: 'auto',
|
|
|
|
public_id: file.hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (file.ext) {
|
|
|
|
config.filename = `${file.hash}${file.ext}`;
|
|
|
|
}
|
|
|
|
|
2019-04-27 12:09:07 +02:00
|
|
|
const upload_stream = cloudinary.uploader.upload_stream(
|
2021-01-20 16:10:29 +01:00
|
|
|
{ ...config, ...customConfig },
|
2019-04-27 12:09:07 +02:00
|
|
|
(err, image) => {
|
|
|
|
if (err) {
|
2020-09-29 12:00:25 +02:00
|
|
|
if (err.message.includes('File size too large')) {
|
|
|
|
return reject(errors.entityTooLarge());
|
|
|
|
}
|
|
|
|
return reject(errors.unknownError(`Error uploading to cloudinary: ${err.message}`));
|
2019-04-27 12:09:07 +02:00
|
|
|
}
|
2020-03-16 14:09:35 +01:00
|
|
|
|
|
|
|
if (image.resource_type === 'video') {
|
|
|
|
file.previewUrl = cloudinary.url(`${image.public_id}.gif`, {
|
|
|
|
video_sampling: 6,
|
|
|
|
delay: 200,
|
|
|
|
width: 250,
|
|
|
|
crop: 'scale',
|
|
|
|
resource_type: 'video',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-27 12:09:07 +02:00
|
|
|
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
|
|
|
);
|
2020-02-27 19:34:14 +01: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
|
|
|
},
|
2020-02-27 19:34:14 +01:00
|
|
|
async delete(file, customConfig = {}) {
|
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',
|
2020-02-27 19:34:14 +01:00
|
|
|
...customConfig,
|
2018-03-24 12:08:07 +01:00
|
|
|
});
|
2020-02-27 19:34:14 +01:00
|
|
|
|
2020-11-10 16:55:50 +08:00
|
|
|
if (response.result !== 'ok' && response.result !== 'not found') {
|
2020-09-29 12:00:25 +02:00
|
|
|
throw errors.unknownError(`Error deleting on cloudinary: ${response.result}`);
|
2018-03-24 12:08:07 +01:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2020-10-09 14:50:13 +02:00
|
|
|
throw errors.unknownError(`Error deleting on cloudinary: ${error.message}`);
|
2018-03-24 12:08:07 +01:00
|
|
|
}
|
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
|
|
|
};
|