mirror of
https://github.com/strapi/strapi.git
synced 2025-11-24 14:11:05 +00:00
refactor(logo-customization): cleaned updateProjectSettings service
This commit is contained in:
parent
d4c5e294a0
commit
d66bde83ce
@ -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);
|
||||||
|
|||||||
@ -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(
|
||||||
|
PROJECT_SETTINGS_FILE_INPUTS.map(async inputName => {
|
||||||
|
// Do not parse empty file inputs
|
||||||
if (!files[inputName]) {
|
if (!files[inputName]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getStream = () => fs.createReadStream(files[inputName].path);
|
||||||
|
|
||||||
formatedFilesData[inputName] = {
|
formatedFilesData[inputName] = {
|
||||||
path: files[inputName].path,
|
path: files[inputName].path,
|
||||||
|
stream: getStream(),
|
||||||
|
};
|
||||||
|
|
||||||
// Get file info
|
// Add formated data for the upload provider
|
||||||
...strapi
|
Object.assign(
|
||||||
|
formatedFilesData[inputName],
|
||||||
|
strapi
|
||||||
.plugin('upload')
|
.plugin('upload')
|
||||||
.service('upload')
|
.service('upload')
|
||||||
.formatFileInfo({
|
.formatFileInfo({
|
||||||
filename: files[inputName].name,
|
filename: files[inputName].name,
|
||||||
type: files[inputName].type,
|
type: files[inputName].type,
|
||||||
size: files[inputName].size,
|
size: files[inputName].size,
|
||||||
}),
|
})
|
||||||
|
);
|
||||||
|
|
||||||
// Get image file dimensions
|
// Add image dimensions
|
||||||
...(await strapi
|
Object.assign(
|
||||||
|
formatedFilesData[inputName],
|
||||||
|
await strapi
|
||||||
.plugin('upload')
|
.plugin('upload')
|
||||||
.service('image-manipulation')
|
.service('image-manipulation')
|
||||||
.getDimensions({ getStream: () => fs.createReadStream(files[inputName].path) })),
|
.getDimensions({ getStream })
|
||||||
};
|
);
|
||||||
});
|
|
||||||
|
|
||||||
await Promise.all(results);
|
|
||||||
|
|
||||||
return formatedFilesData;
|
|
||||||
};
|
|
||||||
|
|
||||||
const uploadFiles = async files => {
|
|
||||||
const formatedFilesData = getFormatedFilesData(files);
|
|
||||||
|
|
||||||
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,
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user