2022-04-04 15:09:08 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
2022-04-06 09:09:04 +02:00
|
|
|
const PROJECT_SETTINGS_FILE_INPUTS = ['menuLogo'];
|
|
|
|
|
2022-04-06 11:50:00 +02:00
|
|
|
const getFormatedFilesData = async files => {
|
|
|
|
const formatedFilesData = {};
|
2022-04-04 15:09:08 +02:00
|
|
|
|
2022-04-06 11:50:00 +02:00
|
|
|
const results = PROJECT_SETTINGS_FILE_INPUTS.map(async inputName => {
|
|
|
|
if (!files[inputName]) {
|
|
|
|
return;
|
2022-04-04 15:09:08 +02:00
|
|
|
}
|
2022-04-06 11:50:00 +02:00
|
|
|
|
|
|
|
formatedFilesData[inputName] = {
|
|
|
|
path: files[inputName].path,
|
|
|
|
|
|
|
|
// Get file info
|
|
|
|
...strapi
|
|
|
|
.plugin('upload')
|
|
|
|
.service('upload')
|
|
|
|
.formatFileInfo({
|
|
|
|
filename: files[inputName].name,
|
|
|
|
type: files[inputName].type,
|
|
|
|
size: files[inputName].size,
|
|
|
|
}),
|
|
|
|
|
|
|
|
// Get image file dimensions
|
|
|
|
...(await strapi
|
|
|
|
.plugin('upload')
|
|
|
|
.service('image-manipulation')
|
|
|
|
.getDimensions({ getStream: () => fs.createReadStream(files[inputName].path) })),
|
|
|
|
};
|
2022-04-04 15:09:08 +02:00
|
|
|
});
|
|
|
|
|
2022-04-06 11:50:00 +02:00
|
|
|
await Promise.all(results);
|
|
|
|
|
|
|
|
return formatedFilesData;
|
|
|
|
};
|
|
|
|
|
|
|
|
const uploadFiles = async files => {
|
|
|
|
const formatedFilesData = getFormatedFilesData(files);
|
|
|
|
|
2022-04-04 15:09:08 +02:00
|
|
|
Object.values(formatedFilesData).map(data =>
|
|
|
|
// Do not await to upload asynchronously
|
|
|
|
strapi.plugin('upload').provider.uploadStream({
|
|
|
|
...data,
|
|
|
|
stream: fs.createReadStream(data.path),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return formatedFilesData;
|
|
|
|
};
|
|
|
|
|
2022-04-06 09:09:04 +02:00
|
|
|
const updateProjectSettings = async (body, uploadedFiles) => {
|
|
|
|
const store = await strapi.store({ type: 'core', name: 'admin' });
|
|
|
|
|
|
|
|
const previousSettings = await store.get({ key: 'project-settings' });
|
|
|
|
|
|
|
|
const newSettings = {
|
|
|
|
...body,
|
|
|
|
...uploadedFiles,
|
|
|
|
};
|
|
|
|
|
|
|
|
PROJECT_SETTINGS_FILE_INPUTS.forEach(inputName => {
|
|
|
|
if (newSettings[inputName] !== undefined && !(typeof newSettings[inputName] === 'object')) {
|
|
|
|
// If the user input exists but is not a formdata "file" remove the file
|
|
|
|
newSettings[inputName] = null;
|
|
|
|
|
|
|
|
// TODO unlink the file
|
|
|
|
} else if (!newSettings[inputName]) {
|
|
|
|
// If the user input is undefined reuse previous setting (do not update field)
|
|
|
|
newSettings[inputName] = previousSettings[inputName];
|
|
|
|
} else {
|
|
|
|
// Update the file
|
|
|
|
newSettings[inputName] = {
|
|
|
|
name: newSettings[inputName].name,
|
|
|
|
url: 'test',
|
2022-04-06 11:50:00 +02:00
|
|
|
width: newSettings[inputName].width,
|
|
|
|
height: newSettings[inputName].height,
|
2022-04-06 09:09:04 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return store.set({ key: 'project-settings', value: { ...previousSettings, ...newSettings } });
|
|
|
|
};
|
|
|
|
|
2022-04-04 15:09:08 +02:00
|
|
|
module.exports = {
|
2022-04-06 11:50:00 +02:00
|
|
|
getFormatedFilesData,
|
2022-04-06 09:09:04 +02:00
|
|
|
uploadFiles,
|
|
|
|
updateProjectSettings,
|
2022-04-04 15:09:08 +02:00
|
|
|
};
|