mirror of
https://github.com/strapi/strapi.git
synced 2025-11-11 15:49:50 +00:00
Allow file info edit whitout re upload
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
parent
57fe38c2bb
commit
a17770293d
@ -47,13 +47,21 @@ module.exports = {
|
|||||||
throw disabledPluginError();
|
throw disabledPluginError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { id } = ctx.query;
|
||||||
const files = _.get(ctx.request.files, 'files');
|
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) {
|
if (_.isEmpty(files) || files.size === 0) {
|
||||||
throw emptyFileError();
|
throw emptyFileError();
|
||||||
}
|
}
|
||||||
|
|
||||||
const { id } = ctx.query;
|
|
||||||
|
|
||||||
const uploadService = strapi.plugins.upload.services.upload;
|
const uploadService = strapi.plugins.upload.services.upload;
|
||||||
|
|
||||||
const validationSchema = Array.isArray(files) ? multiUploadSchema : uploadSchema;
|
const validationSchema = Array.isArray(files) ? multiUploadSchema : uploadSchema;
|
||||||
|
|||||||
@ -139,10 +139,23 @@ module.exports = {
|
|||||||
height,
|
height,
|
||||||
});
|
});
|
||||||
|
|
||||||
const res = await this.add(fileData);
|
return this.add(fileData);
|
||||||
|
},
|
||||||
|
|
||||||
strapi.eventHub.emit('media.create', { media: res });
|
async updateFileInfo(id, { name, alternativeText, caption }) {
|
||||||
return res;
|
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 }) {
|
async replace(id, { data, file }) {
|
||||||
@ -214,18 +227,19 @@ module.exports = {
|
|||||||
height,
|
height,
|
||||||
});
|
});
|
||||||
|
|
||||||
const res = await this.update({ id }, fileData);
|
return this.update({ id }, fileData);
|
||||||
strapi.eventHub.emit('media.update', { media: res });
|
},
|
||||||
|
|
||||||
|
async update(params, values) {
|
||||||
|
const res = await strapi.query('file', 'upload').update(params, values);
|
||||||
|
strapi.eventHub.emit('media.update', { media: res });
|
||||||
return res;
|
return res;
|
||||||
},
|
},
|
||||||
|
|
||||||
update(params, values) {
|
async add(values) {
|
||||||
return strapi.query('file', 'upload').update(params, values);
|
const res = await strapi.query('file', 'upload').create(values);
|
||||||
},
|
strapi.eventHub.emit('media.create', { media: res });
|
||||||
|
return res;
|
||||||
add(values) {
|
|
||||||
return strapi.query('file', 'upload').create(values);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
fetch(params) {
|
fetch(params) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user