Allow file info edit whitout re upload

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-03-24 12:21:51 +01:00
parent 57fe38c2bb
commit a17770293d
2 changed files with 35 additions and 13 deletions

View File

@ -47,13 +47,21 @@ module.exports = {
throw disabledPluginError();
}
const { id } = ctx.query;
const files = _.get(ctx.request.files, 'files');
// update only fileInfo if not file content sent
if (id && (_.isEmpty(files) || files.size === 0)) {
const data = await validateUploadBody(uploadSchema, ctx.request.body);
ctx.body = await strapi.plugins.upload.services.upload.updateFileInfo(id, data.fileInfo);
return;
}
if (_.isEmpty(files) || files.size === 0) {
throw emptyFileError();
}
const { id } = ctx.query;
const uploadService = strapi.plugins.upload.services.upload;
const validationSchema = Array.isArray(files) ? multiUploadSchema : uploadSchema;

View File

@ -139,10 +139,23 @@ module.exports = {
height,
});
const res = await this.add(fileData);
return this.add(fileData);
},
strapi.eventHub.emit('media.create', { media: res });
return res;
async updateFileInfo(id, { name, alternativeText, caption }) {
const dbFile = await this.fetch({ id });
if (!dbFile) {
throw strapi.errors.notFound('file not found');
}
const newInfos = _.assign({}, dbFile, {
name: _.isNil(name) ? dbFile.name : name,
alternativeText: _.isNil(alternativeText) ? dbFile.alternativeText : alternativeText,
caption: _.isNil(caption) ? dbFile.caption : caption,
});
return this.update({ id }, newInfos);
},
async replace(id, { data, file }) {
@ -214,18 +227,19 @@ module.exports = {
height,
});
const res = await this.update({ id }, fileData);
strapi.eventHub.emit('media.update', { media: res });
return this.update({ id }, fileData);
},
async update(params, values) {
const res = await strapi.query('file', 'upload').update(params, values);
strapi.eventHub.emit('media.update', { media: res });
return res;
},
update(params, values) {
return strapi.query('file', 'upload').update(params, values);
},
add(values) {
return strapi.query('file', 'upload').create(values);
async add(values) {
const res = await strapi.query('file', 'upload').create(values);
strapi.eventHub.emit('media.create', { media: res });
return res;
},
fetch(params) {