feat(logo-customization): delete old logos and getProjectSettings service

This commit is contained in:
vincentbpro 2022-04-06 13:54:52 +02:00
parent d66bde83ce
commit 11a203459a
2 changed files with 69 additions and 26 deletions

View File

@ -61,12 +61,7 @@ module.exports = {
const formatedFiles = await projectSettingsService.parseFilesData(files);
await validateUpdateProjectSettingsImagesDimensions(formatedFiles);
const updatedProjectSettings = await projectSettingsService.updateProjectSettings(
body,
formatedFiles
);
console.log(updatedProjectSettings);
return projectSettingsService.updateProjectSettings(body, formatedFiles);
},
async information() {

View File

@ -1,6 +1,8 @@
'use strict';
const fs = require('fs');
const path = require('path');
const { pick } = require('lodash');
const PROJECT_SETTINGS_FILE_INPUTS = ['menuLogo'];
@ -9,30 +11,22 @@ const parseFilesData = async files => {
await Promise.all(
PROJECT_SETTINGS_FILE_INPUTS.map(async inputName => {
// Do not parse empty file inputs
// Skip empty file inputs
if (!files[inputName]) {
return;
}
const getStream = () => fs.createReadStream(files[inputName].path);
formatedFilesData[inputName] = {
path: files[inputName].path,
stream: getStream(),
};
// Add formated data for the upload provider
Object.assign(
formatedFilesData[inputName],
strapi
.plugin('upload')
.service('upload')
.formatFileInfo({
filename: files[inputName].name,
type: files[inputName].type,
size: files[inputName].size,
})
);
formatedFilesData[inputName] = strapi
.plugin('upload')
.service('upload')
.formatFileInfo({
filename: files[inputName].name,
type: files[inputName].type,
size: files[inputName].size,
});
// Add image dimensions
Object.assign(
@ -42,16 +36,66 @@ const parseFilesData = async files => {
.service('image-manipulation')
.getDimensions({ getStream })
);
// Add file path, url and stream
const url = `/uploads/${formatedFilesData[inputName].hash}${formatedFilesData[inputName].ext}`;
Object.assign(formatedFilesData[inputName], {
path: path.join(strapi.dirs.public, url),
stream: getStream(),
tmpPath: files[inputName].path,
url,
});
})
);
return formatedFilesData;
};
const getProjectSettings = async () => {
const store = await strapi.store({ type: 'core', name: 'admin' });
const projectSettings = await store.get({ key: 'project-settings' });
PROJECT_SETTINGS_FILE_INPUTS.forEach(inputName => {
if (projectSettings[inputName]) {
projectSettings[inputName] = pick(projectSettings[inputName], [
'name',
'url',
'width',
'height',
]);
}
});
return projectSettings;
};
const uploadFiles = async files => {
return Promise.all(Object.values(files).map(strapi.plugin('upload').provider.uploadStream));
};
const deleteOldFiles = async (previousSettings, newSettings) => {
return Promise.all(
PROJECT_SETTINGS_FILE_INPUTS.map(async inputName => {
// Skip if there was no previous file
if (!previousSettings[inputName]) {
return;
}
// Skip if the file was not changed
if (
newSettings[inputName] &&
previousSettings[inputName].path === newSettings[inputName].path
) {
return;
}
// There was a previous file and an new file was uploaded
// Remove the previous file
fs.unlinkSync(previousSettings[inputName].path);
})
);
};
const updateProjectSettings = async (body, files) => {
const store = await strapi.store({ type: 'core', name: 'admin' });
const previousSettings = await store.get({ key: 'project-settings' });
@ -72,7 +116,8 @@ const updateProjectSettings = async (body, files) => {
// Update the file
newSettings[inputName] = {
name: newSettings[inputName].name,
url: 'test',
path: newSettings[inputName].path,
url: newSettings[inputName].url,
width: newSettings[inputName].width,
height: newSettings[inputName].height,
};
@ -81,12 +126,15 @@ const updateProjectSettings = async (body, files) => {
// No await to proceed asynchronously
uploadFiles(files);
deleteOldFiles(previousSettings, newSettings);
return store.set({ key: 'project-settings', value: { ...previousSettings, ...newSettings } });
store.set({ key: 'project-settings', value: { ...previousSettings, ...newSettings } });
return getProjectSettings();
};
module.exports = {
parseFilesData,
uploadFiles,
getProjectSettings,
updateProjectSettings,
};