mirror of
https://github.com/strapi/strapi.git
synced 2025-12-04 19:13:20 +00:00
feat(menu-logo): file upload and storing data in corestore
This commit is contained in:
parent
3451aceaf5
commit
5663e5065d
@ -8,7 +8,10 @@ const { ValidationError } = require('@strapi/utils').errors;
|
||||
// eslint-disable-next-line node/no-extraneous-require
|
||||
const ee = require('@strapi/strapi/lib/utils/ee');
|
||||
|
||||
const { validateUpdateProjectSettings } = require('../validation/project-settings');
|
||||
const {
|
||||
validateUpdateProjectSettings,
|
||||
validateUpdateProjectSettingsFiles,
|
||||
} = require('../validation/project-settings');
|
||||
const { getService } = require('../utils');
|
||||
|
||||
const PLUGIN_NAME_REGEX = /^[A-Za-z][A-Za-z0-9-_]+$/;
|
||||
@ -46,14 +49,21 @@ module.exports = {
|
||||
|
||||
async updateProjectSettings(ctx) {
|
||||
const {
|
||||
request: { files },
|
||||
request: { files, body },
|
||||
} = ctx;
|
||||
|
||||
await validateUpdateProjectSettings(files);
|
||||
await validateUpdateProjectSettings(body);
|
||||
await validateUpdateProjectSettingsFiles(files);
|
||||
|
||||
const uploadedFiles = await getService('project-settings').uploadProjectSettingsFiles(files);
|
||||
const projectSettingsService = getService('project-settings');
|
||||
|
||||
console.log(uploadedFiles);
|
||||
const uploadedFiles = await projectSettingsService.uploadFiles(files);
|
||||
const updatedProjectSettings = await projectSettingsService.updateProjectSettings(
|
||||
body,
|
||||
uploadedFiles
|
||||
);
|
||||
|
||||
console.log(updatedProjectSettings);
|
||||
},
|
||||
|
||||
async information() {
|
||||
|
||||
@ -3,6 +3,8 @@
|
||||
const fs = require('fs');
|
||||
const { transform } = require('lodash');
|
||||
|
||||
const PROJECT_SETTINGS_FILE_INPUTS = ['menuLogo'];
|
||||
|
||||
const getFormatedFileData = data => ({
|
||||
path: data.path,
|
||||
...strapi
|
||||
@ -15,7 +17,7 @@ const getFormatedFileData = data => ({
|
||||
}),
|
||||
});
|
||||
|
||||
const uploadProjectSettingsFiles = async files => {
|
||||
const uploadFiles = async files => {
|
||||
const formatedFilesData = transform(files, (result, value, key) => {
|
||||
if (value) {
|
||||
result[key] = getFormatedFileData(value);
|
||||
@ -33,6 +35,40 @@ const uploadProjectSettingsFiles = async files => {
|
||||
return formatedFilesData;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
uploadProjectSettingsFiles,
|
||||
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 } });
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
uploadFiles,
|
||||
updateProjectSettings,
|
||||
};
|
||||
|
||||
@ -6,6 +6,12 @@ const MAX_LOGO_SIZE = 1024 * 1024; // 1Mo
|
||||
const ALLOWED_LOGO_FILE_TYPES = ['image/jpeg', 'image/png', 'image/svg+xml'];
|
||||
|
||||
const updateProjectSettings = yup
|
||||
.object({
|
||||
menuLogo: yup.string(),
|
||||
})
|
||||
.noUnknown();
|
||||
|
||||
const updateProjectSettingsFiles = yup
|
||||
.object({
|
||||
menuLogo: yup.object({
|
||||
name: yup.string(),
|
||||
@ -17,4 +23,5 @@ const updateProjectSettings = yup
|
||||
|
||||
module.exports = {
|
||||
validateUpdateProjectSettings: validateYupSchemaSync(updateProjectSettings),
|
||||
validateUpdateProjectSettingsFiles: validateYupSchemaSync(updateProjectSettingsFiles),
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user