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

View File

@ -50,7 +50,7 @@ module.exports = {
: '',
buffer,
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) };
},
};