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);
return Object.assign(Object.create(baseProvider), {
...providerInstance,
original: providerInstance,
uploadStream(file, options = actionOptions.upload) {
return providerInstance.uploadStream(file, options);
},
upload(file, options = actionOptions.upload) {
return providerInstance.upload(file, options);
},
delete(file, options = actionOptions.delete) {
return providerInstance.delete(file, options);
},
if (!providerInstance.delete) {
throw new Error(`The upload provider "${providerName}" didn't implement the delete method.`);
}
if (!providerInstance.upload && !providerInstance.uploadStream) {
throw new Error(
`The upload provider "${providerName}" didn't implement the uploadStream nor the upload method.`
);
}
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 = {
extend(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 () => {

View File

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