mirror of
https://github.com/strapi/strapi.git
synced 2025-11-12 08:08:05 +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);
|
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 () => {
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user