Init optimization func and clean up code

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-03-08 12:59:21 +01:00
parent e6322e64dc
commit 8d025b89f3
3 changed files with 41 additions and 27 deletions

View File

@ -76,29 +76,15 @@ module.exports = {
},
async getSettings(ctx) {
const config = await strapi
.store({
type: 'plugin',
name: 'upload',
key: 'settings',
})
.get();
const data = await strapi.plugins.upload.services.upload.getSettings();
ctx.send({
data: config,
});
ctx.body = { data };
},
async updateSettings(ctx) {
const configurator = strapi.store({
type: 'plugin',
name: 'upload',
key: 'settings',
});
const data = await validateSettings(ctx.request.body);
await configurator.set({ key: 'settings', value: data });
await strapi.plugins.upload.services.upload.setSettings(data);
ctx.body = { data };
},
@ -138,14 +124,13 @@ module.exports = {
const updatedFile = await uploadService.update(id, enhancedFile);
ctx.send(updatedFile);
ctx.body = updatedFile;
},
async find(ctx) {
const data = await strapi.plugins['upload'].services.upload.fetchAll(ctx.query);
// Send 200 `ok`
ctx.send(data);
ctx.body = data;
},
async findOne(ctx) {
@ -155,13 +140,13 @@ module.exports = {
return ctx.notFound('file.notFound');
}
ctx.send(data);
ctx.body = data;
},
async count(ctx) {
const data = await strapi.plugins['upload'].services.upload.count(ctx.query);
ctx.send({ count: data });
ctx.body = { count: data };
},
async destroy(ctx) {
@ -175,7 +160,7 @@ module.exports = {
await strapi.plugins['upload'].services.upload.remove(file);
ctx.send(file);
ctx.body = file;
},
async search(ctx) {
@ -185,7 +170,7 @@ module.exports = {
id,
});
ctx.send(data);
ctx.body = data;
},
};

View File

@ -245,15 +245,26 @@ module.exports = {
).then(files => this.uploadFileAndPersist(files));
},
async getConfig() {
async getSettings() {
const config = await strapi
.store({
environment: strapi.config.environment,
type: 'plugin',
name: 'upload',
key: 'settings',
})
.get({ key: 'provider' });
.get();
return { ...config, sizeLimit: parseFloat(config.sizeLimit) };
return config;
},
async setSettings(value) {
await strapi
.store({
type: 'plugin',
name: 'upload',
key: 'settings',
})
.set({ value });
},
};

View File

@ -52,8 +52,26 @@ const generateThumbnail = async file => {
return null;
};
// TODO: return new size
const optimize = async buffer => {
const { sizeOptimization = false } = strapi.plugins.upload.services.upload.getSettings();
if (!sizeOptimization) return buffer;
const { format } = await getMetadatas(buffer);
if (format !== undefined) {
return sharp(buffer)
.toFormat(format, { quality: 100 })
.toBuffer({ resolveWithObject: true });
}
return buffer;
};
module.exports = {
getDimensions,
generateThumbnail,
bytesToKbytes,
optimize,
};