2022-04-04 15:09:08 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs');
|
2022-04-06 13:54:52 +02:00
|
|
|
const path = require('path');
|
|
|
|
const { pick } = require('lodash');
|
2022-04-04 15:09:08 +02:00
|
|
|
|
2022-04-06 09:09:04 +02:00
|
|
|
const PROJECT_SETTINGS_FILE_INPUTS = ['menuLogo'];
|
|
|
|
|
2022-04-06 12:23:17 +02:00
|
|
|
const parseFilesData = async files => {
|
2022-04-06 11:50:00 +02:00
|
|
|
const formatedFilesData = {};
|
2022-04-04 15:09:08 +02:00
|
|
|
|
2022-04-06 12:23:17 +02:00
|
|
|
await Promise.all(
|
|
|
|
PROJECT_SETTINGS_FILE_INPUTS.map(async inputName => {
|
2022-04-06 13:54:52 +02:00
|
|
|
// Skip empty file inputs
|
2022-04-06 12:23:17 +02:00
|
|
|
if (!files[inputName]) {
|
|
|
|
return;
|
|
|
|
}
|
2022-04-04 15:09:08 +02:00
|
|
|
|
2022-04-06 12:23:17 +02:00
|
|
|
const getStream = () => fs.createReadStream(files[inputName].path);
|
2022-04-06 11:50:00 +02:00
|
|
|
|
2022-04-06 12:23:17 +02:00
|
|
|
// Add formated data for the upload provider
|
2022-04-06 13:54:52 +02:00
|
|
|
formatedFilesData[inputName] = strapi
|
|
|
|
.plugin('upload')
|
|
|
|
.service('upload')
|
|
|
|
.formatFileInfo({
|
|
|
|
filename: files[inputName].name,
|
|
|
|
type: files[inputName].type,
|
|
|
|
size: files[inputName].size,
|
|
|
|
});
|
2022-04-06 12:23:17 +02:00
|
|
|
|
|
|
|
// Add image dimensions
|
|
|
|
Object.assign(
|
|
|
|
formatedFilesData[inputName],
|
|
|
|
await strapi
|
|
|
|
.plugin('upload')
|
|
|
|
.service('image-manipulation')
|
|
|
|
.getDimensions({ getStream })
|
|
|
|
);
|
2022-04-06 13:54:52 +02:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
});
|
2022-04-04 15:09:08 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return formatedFilesData;
|
|
|
|
};
|
|
|
|
|
2022-04-06 13:54:52 +02:00
|
|
|
const getProjectSettings = async () => {
|
|
|
|
const store = await strapi.store({ type: 'core', name: 'admin' });
|
|
|
|
const projectSettings = await store.get({ key: 'project-settings' });
|
|
|
|
|
2022-04-07 12:02:17 +02:00
|
|
|
// Filter file input fields
|
2022-04-06 13:54:52 +02:00
|
|
|
PROJECT_SETTINGS_FILE_INPUTS.forEach(inputName => {
|
|
|
|
if (projectSettings[inputName]) {
|
|
|
|
projectSettings[inputName] = pick(projectSettings[inputName], [
|
|
|
|
'name',
|
|
|
|
'url',
|
|
|
|
'width',
|
|
|
|
'height',
|
2022-04-07 12:02:17 +02:00
|
|
|
'ext',
|
|
|
|
'size',
|
2022-04-06 13:54:52 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return projectSettings;
|
|
|
|
};
|
|
|
|
|
2022-04-06 12:23:17 +02:00
|
|
|
const uploadFiles = async files => {
|
|
|
|
return Promise.all(Object.values(files).map(strapi.plugin('upload').provider.uploadStream));
|
|
|
|
};
|
2022-04-06 09:09:04 +02:00
|
|
|
|
2022-04-07 11:41:32 +02:00
|
|
|
const deleteOldFiles = async ({ previousSettings, newSettings }) => {
|
2022-04-06 13:54:52 +02:00
|
|
|
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);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-04-07 11:41:32 +02:00
|
|
|
const updateProjectSettings = async ({ body, files }) => {
|
2022-04-06 12:23:17 +02:00
|
|
|
const store = await strapi.store({ type: 'core', name: 'admin' });
|
2022-04-06 09:09:04 +02:00
|
|
|
const previousSettings = await store.get({ key: 'project-settings' });
|
|
|
|
|
|
|
|
const newSettings = {
|
|
|
|
...body,
|
2022-04-06 12:23:17 +02:00
|
|
|
...files,
|
2022-04-06 09:09:04 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
} 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,
|
2022-04-06 13:54:52 +02:00
|
|
|
path: newSettings[inputName].path,
|
|
|
|
url: newSettings[inputName].url,
|
2022-04-06 11:50:00 +02:00
|
|
|
width: newSettings[inputName].width,
|
|
|
|
height: newSettings[inputName].height,
|
2022-04-07 12:02:17 +02:00
|
|
|
ext: newSettings[inputName].ext.replace('.', ''),
|
|
|
|
size: newSettings[inputName].size,
|
2022-04-06 09:09:04 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-04-06 12:23:17 +02:00
|
|
|
// No await to proceed asynchronously
|
|
|
|
uploadFiles(files);
|
2022-04-07 11:41:32 +02:00
|
|
|
deleteOldFiles({ previousSettings, newSettings });
|
2022-04-06 13:54:52 +02:00
|
|
|
|
2022-04-07 11:41:32 +02:00
|
|
|
await store.set({
|
|
|
|
key: 'project-settings',
|
|
|
|
value: { ...previousSettings, ...newSettings },
|
|
|
|
});
|
2022-04-06 12:23:17 +02:00
|
|
|
|
2022-04-06 13:54:52 +02:00
|
|
|
return getProjectSettings();
|
2022-04-06 09:09:04 +02:00
|
|
|
};
|
|
|
|
|
2022-04-04 15:09:08 +02:00
|
|
|
module.exports = {
|
2022-04-06 17:56:41 +02:00
|
|
|
deleteOldFiles,
|
2022-04-06 12:23:17 +02:00
|
|
|
parseFilesData,
|
2022-04-06 13:54:52 +02:00
|
|
|
getProjectSettings,
|
2022-04-06 09:09:04 +02:00
|
|
|
updateProjectSettings,
|
2022-04-04 15:09:08 +02:00
|
|
|
};
|