2022-04-04 15:09:08 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const { transform } = require('lodash');
|
|
|
|
|
2022-04-06 09:09:04 +02:00
|
|
|
const PROJECT_SETTINGS_FILE_INPUTS = ['menuLogo'];
|
|
|
|
|
2022-04-04 15:09:08 +02:00
|
|
|
const getFormatedFileData = data => ({
|
|
|
|
path: data.path,
|
|
|
|
...strapi
|
|
|
|
.plugin('upload')
|
|
|
|
.service('upload')
|
|
|
|
.formatFileInfo({
|
|
|
|
filename: data.name,
|
|
|
|
type: data.type,
|
|
|
|
size: data.size,
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2022-04-06 09:09:04 +02:00
|
|
|
const uploadFiles = async files => {
|
2022-04-04 15:09:08 +02:00
|
|
|
const formatedFilesData = transform(files, (result, value, key) => {
|
|
|
|
if (value) {
|
|
|
|
result[key] = getFormatedFileData(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
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',
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return store.set({ key: 'project-settings', value: { ...previousSettings, ...newSettings } });
|
|
|
|
};
|
|
|
|
|
2022-04-04 15:09:08 +02:00
|
|
|
module.exports = {
|
2022-04-06 09:09:04 +02:00
|
|
|
uploadFiles,
|
|
|
|
updateProjectSettings,
|
2022-04-04 15:09:08 +02:00
|
|
|
};
|