diff --git a/packages/strapi-plugin-upload/config/routes.json b/packages/strapi-plugin-upload/config/routes.json index a23e3578d4..d47777ce26 100644 --- a/packages/strapi-plugin-upload/config/routes.json +++ b/packages/strapi-plugin-upload/config/routes.json @@ -1,18 +1,5 @@ { "routes": [ - { - "method": "POST", - "path": "/", - "handler": "Upload.upload", - "config": { - "policies": [], - "description": "Upload a file", - "tag": { - "plugin": "upload", - "name": "File" - } - } - }, { "method": "GET", "path": "/settings", @@ -29,6 +16,28 @@ "policies": [] } }, + { + "method": "POST", + "path": "/", + "handler": "Upload.upload", + "config": { + "policies": [], + "description": "Upload a file", + "tag": { + "plugin": "upload", + "name": "File" + } + } + }, + { + "method": "POST", + "path": "/files/replace/:id", + "handler": "Upload.replaceFile", + "config": { + "policies": [], + "description": "Replace a file" + } + }, { "method": "GET", "path": "/files/count", diff --git a/packages/strapi-plugin-upload/controllers/Upload.js b/packages/strapi-plugin-upload/controllers/Upload.js index d0f5eff68c..0c004d5115 100644 --- a/packages/strapi-plugin-upload/controllers/Upload.js +++ b/packages/strapi-plugin-upload/controllers/Upload.js @@ -106,6 +106,44 @@ module.exports = { ctx.body = { data }; }, + async replaceFile(ctx) { + const { id } = ctx.params; + + const uploadService = strapi.plugins.upload.services.upload; + + // Retrieve provider configuration. + const { enabled } = strapi.plugins.upload.config; + + // Verify if the file upload is enable. + if (enabled === false) { + throw strapi.errors.badRequest(null, { + errors: [{ id: 'Upload.status.disabled', message: 'File upload is disabled' }], + }); + } + + const data = await strapi.plugins['upload'].services.upload.fetch({ id }); + + if (!data) { + return ctx.notFound('file.notFound'); + } + + const { fileInfo } = await validateUploadBody(uploadSchema, ctx.request.body); + + const { file = {} } = ctx.request.files || {}; + + if (_.isUndefined(file)) { + throw strapi.errors.badRequest(null, { + errors: [{ id: 'Upload.status.empty', message: 'File is missing' }], + }); + } + + const enhancedFile = uploadService.enhanceFile(file, fileInfo); + + const updatedFile = await uploadService.update(id, enhancedFile); + + ctx.send(updatedFile); + }, + async find(ctx) { const data = await strapi.plugins['upload'].services.upload.fetchAll(ctx.query); diff --git a/packages/strapi-plugin-upload/services/Upload.js b/packages/strapi-plugin-upload/services/Upload.js index 67e34c3769..0a65b41438 100644 --- a/packages/strapi-plugin-upload/services/Upload.js +++ b/packages/strapi-plugin-upload/services/Upload.js @@ -99,6 +99,35 @@ module.exports = { return Promise.all(files.map(file => uploadFile(file))); }, + async replace(dbFile, file) { + const config = strapi.plugins.upload.config; + + // keep a constant hash + _.assign(file, { + hash: dbFile.hash, + ext: dbFile.ext, + }); + + // execute delete function of the provider + if (dbFile.provider === config.provider) { + await strapi.plugins.upload.provider.delete(dbFile); + } + + await strapi.plugins.upload.provider.upload(file); + + delete file.buffer; + file.provider = config.provider; + + const res = await this.update({ id: dbFile.id }, {}); + strapi.eventHub.emit('media.update', { media: res }); + + return res; + }, + + update(id, values) { + return strapi.query('file', 'upload').update({ id }, values); + }, + add(values) { return strapi.query('file', 'upload').create(values); },