refactor upload bootstrap

This commit is contained in:
Pierre Noël 2022-03-15 18:51:52 +01:00
parent 091a3d3d85
commit aea7e9fccf
2 changed files with 23 additions and 22 deletions

View File

@ -49,34 +49,35 @@ const createProvider = config => {
const providerInstance = provider.init(providerOptions); const providerInstance = provider.init(providerOptions);
return Object.assign(Object.create(baseProvider), { if (!providerInstance.delete) {
...providerInstance, throw new Error(`The upload provider "${providerName}" didn't implement the delete method.`);
original: providerInstance, }
uploadStream(file, options = actionOptions.upload) {
return providerInstance.uploadStream(file, options); if (!providerInstance.upload && !providerInstance.uploadStream) {
}, throw new Error(
upload(file, options = actionOptions.upload) { `The upload provider "${providerName}" didn't implement the uploadStream nor the upload method.`
return providerInstance.upload(file, options); );
}, }
delete(file, options = actionOptions.delete) {
return providerInstance.delete(file, options); if (!providerInstance.uploadStream) {
}, process.emitWarning(
`The upload provider "${providerName}" didn't implement the uploadStream function. Strapi will fallback on the upload method. Some performance issues may occur.`
);
}
const wrappedProvider = _.mapValues(providerInstance, (method, methodName) => {
return async function(file, options = actionOptions[methodName]) {
return providerInstance[methodName](file, options);
};
}); });
return Object.assign(Object.create(baseProvider), wrappedProvider);
}; };
const baseProvider = { const baseProvider = {
extend(obj) { extend(obj) {
Object.assign(this, obj); Object.assign(this, obj);
}, },
uploadStream() {
throw new Error('Provider uploadStream method is not implemented');
},
upload() {
throw new Error('Provider upload method is not implemented');
},
delete() {
throw new Error('Provider delete method is not implemented');
},
}; };
const registerPermissionActions = async () => { const registerPermissionActions = async () => {

View File

@ -5,7 +5,7 @@ const { streamToBuffer } = require('../utils/file');
module.exports = ({ strapi }) => ({ module.exports = ({ strapi }) => ({
async upload(file) { async upload(file) {
if (isFunction(strapi.plugin('upload').provider.original.uploadStream)) { if (isFunction(strapi.plugin('upload').provider.uploadStream)) {
file.stream = file.getStream(); file.stream = file.getStream();
await strapi.plugin('upload').provider.uploadStream(file); await strapi.plugin('upload').provider.uploadStream(file);
delete file.stream; delete file.stream;