refactor(logo-customization): cleaned updateProjectSettings service

This commit is contained in:
vincentbpro 2022-04-06 12:23:17 +02:00
parent d4c5e294a0
commit d66bde83ce
2 changed files with 44 additions and 45 deletions

View File

@ -58,13 +58,12 @@ module.exports = {
await validateUpdateProjectSettings(body); await validateUpdateProjectSettings(body);
await validateUpdateProjectSettingsFiles(files); await validateUpdateProjectSettingsFiles(files);
const formatedFiles = await projectSettingsService.getFormatedFilesData(files); const formatedFiles = await projectSettingsService.parseFilesData(files);
await validateUpdateProjectSettingsImagesDimensions(formatedFiles); await validateUpdateProjectSettingsImagesDimensions(formatedFiles);
const uploadedFiles = await projectSettingsService.uploadFiles(files);
const updatedProjectSettings = await projectSettingsService.updateProjectSettings( const updatedProjectSettings = await projectSettingsService.updateProjectSettings(
body, body,
uploadedFiles formatedFiles
); );
console.log(updatedProjectSettings); console.log(updatedProjectSettings);

View File

@ -4,70 +4,67 @@ const fs = require('fs');
const PROJECT_SETTINGS_FILE_INPUTS = ['menuLogo']; const PROJECT_SETTINGS_FILE_INPUTS = ['menuLogo'];
const getFormatedFilesData = async files => { const parseFilesData = async files => {
const formatedFilesData = {}; const formatedFilesData = {};
const results = PROJECT_SETTINGS_FILE_INPUTS.map(async inputName => { await Promise.all(
if (!files[inputName]) { PROJECT_SETTINGS_FILE_INPUTS.map(async inputName => {
return; // Do not parse empty file inputs
} if (!files[inputName]) {
return;
}
formatedFilesData[inputName] = { const getStream = () => fs.createReadStream(files[inputName].path);
path: files[inputName].path,
// Get file info formatedFilesData[inputName] = {
...strapi path: files[inputName].path,
.plugin('upload') stream: getStream(),
.service('upload') };
.formatFileInfo({
filename: files[inputName].name,
type: files[inputName].type,
size: files[inputName].size,
}),
// Get image file dimensions // Add formated data for the upload provider
...(await strapi Object.assign(
.plugin('upload') formatedFilesData[inputName],
.service('image-manipulation') strapi
.getDimensions({ getStream: () => fs.createReadStream(files[inputName].path) })), .plugin('upload')
}; .service('upload')
}); .formatFileInfo({
filename: files[inputName].name,
type: files[inputName].type,
size: files[inputName].size,
})
);
await Promise.all(results); // Add image dimensions
Object.assign(
return formatedFilesData; formatedFilesData[inputName],
}; await strapi
.plugin('upload')
const uploadFiles = async files => { .service('image-manipulation')
const formatedFilesData = getFormatedFilesData(files); .getDimensions({ getStream })
);
Object.values(formatedFilesData).map(data =>
// Do not await to upload asynchronously
strapi.plugin('upload').provider.uploadStream({
...data,
stream: fs.createReadStream(data.path),
}) })
); );
return formatedFilesData; return formatedFilesData;
}; };
const updateProjectSettings = async (body, uploadedFiles) => { const uploadFiles = async files => {
const store = await strapi.store({ type: 'core', name: 'admin' }); return Promise.all(Object.values(files).map(strapi.plugin('upload').provider.uploadStream));
};
const updateProjectSettings = async (body, files) => {
const store = await strapi.store({ type: 'core', name: 'admin' });
const previousSettings = await store.get({ key: 'project-settings' }); const previousSettings = await store.get({ key: 'project-settings' });
const newSettings = { const newSettings = {
...body, ...body,
...uploadedFiles, ...files,
}; };
PROJECT_SETTINGS_FILE_INPUTS.forEach(inputName => { PROJECT_SETTINGS_FILE_INPUTS.forEach(inputName => {
if (newSettings[inputName] !== undefined && !(typeof newSettings[inputName] === 'object')) { if (newSettings[inputName] !== undefined && !(typeof newSettings[inputName] === 'object')) {
// If the user input exists but is not a formdata "file" remove the file // If the user input exists but is not a formdata "file" remove the file
newSettings[inputName] = null; newSettings[inputName] = null;
// TODO unlink the file
} else if (!newSettings[inputName]) { } else if (!newSettings[inputName]) {
// If the user input is undefined reuse previous setting (do not update field) // If the user input is undefined reuse previous setting (do not update field)
newSettings[inputName] = previousSettings[inputName]; newSettings[inputName] = previousSettings[inputName];
@ -82,11 +79,14 @@ const updateProjectSettings = async (body, uploadedFiles) => {
} }
}); });
// No await to proceed asynchronously
uploadFiles(files);
return store.set({ key: 'project-settings', value: { ...previousSettings, ...newSettings } }); return store.set({ key: 'project-settings', value: { ...previousSettings, ...newSettings } });
}; };
module.exports = { module.exports = {
getFormatedFilesData, parseFilesData,
uploadFiles, uploadFiles,
updateProjectSettings, updateProjectSettings,
}; };