Signed-off-by: Edaly Ahmed <ahmed.edaly@oyez.fr>

adding getConfig service and casting params before updating database
This commit is contained in:
Edaly Ahmed 2020-02-24 17:39:15 +01:00
parent 67f4712fa7
commit 129edd66ee
2 changed files with 21 additions and 10 deletions

View File

@ -13,13 +13,7 @@ module.exports = {
const uploadService = strapi.plugins.upload.services.upload; const uploadService = strapi.plugins.upload.services.upload;
// Retrieve provider configuration. // Retrieve provider configuration.
const config = await strapi const config = await uploadService.getConfig();
.store({
environment: strapi.config.environment,
type: 'plugin',
name: 'upload',
})
.get({ key: 'provider' });
// Verify if the file upload is enable. // Verify if the file upload is enable.
if (config.enabled === false) { if (config.enabled === false) {
@ -55,7 +49,7 @@ module.exports = {
const buffers = await uploadService.bufferize(files); const buffers = await uploadService.bufferize(files);
const enhancedFiles = buffers.map(file => { const enhancedFiles = buffers.map(file => {
if (parseFloat(file.size) > parseFloat(config.sizeLimit)) { if (file.size > config.sizeLimit) {
return ctx.badRequest(null, [ return ctx.badRequest(null, [
{ {
messages: [ messages: [
@ -131,13 +125,19 @@ module.exports = {
}, },
async updateSettings(ctx) { async updateSettings(ctx) {
const {
request: { body: newSettings },
} = ctx;
await strapi await strapi
.store({ .store({
environment: ctx.params.environment, environment: ctx.params.environment,
type: 'plugin', type: 'plugin',
name: 'upload', name: 'upload',
}) })
.set({ key: 'provider', value: ctx.request.body }); .set({
key: 'provider',
value: { ...newSettings, sizeLimit: parseFloat(newSettings.sizeLimit) },
});
ctx.send({ ok: true }); ctx.send({ ok: true });
}, },

View File

@ -50,7 +50,7 @@ module.exports = {
: '', : '',
buffer, buffer,
mime: stream.type, mime: stream.type,
size: (stream.size / 1000).toFixed(2), size: parseFloat((stream.size / 1000).toFixed(2)),
}; };
}; };
@ -176,4 +176,15 @@ module.exports = {
}) })
); );
}, },
async getConfig() {
const config = await strapi
.store({
environment: strapi.config.environment,
type: 'plugin',
name: 'upload',
})
.get({ key: 'provider' });
return { ...config, sizeLimit: parseFloat(config.sizeLimit) };
},
}; };