mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 02:16:03 +00:00
refactor upload bootstrap
This commit is contained in:
parent
091a3d3d85
commit
aea7e9fccf
43
packages/core/upload/server/bootstrap.js
vendored
43
packages/core/upload/server/bootstrap.js
vendored
@ -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 () => {
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user