Add extend method in provider prototype

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-03-14 18:29:36 +01:00
parent 0137df7a7e
commit 8bac1b1835
2 changed files with 32 additions and 2 deletions

View File

@ -1,5 +1,8 @@
'use strict';
const fse = require('fs-extra');
const { join } = require('path');
/**
* An asynchronous bootstrap function that runs before
* your application gets started.
@ -8,4 +11,17 @@
* run jobs, or perform some special logic.
*/
module.exports = () => {};
module.exports = async () => {
await fse.ensureDir('./public/uploads/sub-folder');
// example of overriding the upload provider
strapi.plugins.upload.provider.extend({
async upload(file) {
const filePath = `/uploads/sub-folder/${file.hash}${file.ext}`;
// write file in public/assets folder
await fse.writeFile(join(strapi.config.public.path, filePath), file.buffer);
file.url = filePath;
},
});
};

View File

@ -30,7 +30,9 @@ module.exports = async () => {
const createProvider = ({ provider, providerOptions }) => {
try {
return require(`strapi-provider-upload-${provider}`).init(providerOptions);
const providerInstance = require(`strapi-provider-upload-${provider}`).init(providerOptions);
return Object.assign(Object.create(baseProvider), providerInstance);
} catch (err) {
strapi.log.error(err);
throw new Error(
@ -38,3 +40,15 @@ const createProvider = ({ provider, providerOptions }) => {
);
}
};
const baseProvider = {
extend(obj) {
Object.assign(this, obj);
},
upload() {
throw new Error('Provider upload method is not implemented');
},
delete() {
throw new Error('Provider delete method is not implemented');
},
};