Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-03-04 10:31:32 +01:00
parent b084cb1c65
commit 7cd34b725a
3 changed files with 89 additions and 13 deletions

View File

@ -1,18 +1,5 @@
{ {
"routes": [ "routes": [
{
"method": "POST",
"path": "/",
"handler": "Upload.upload",
"config": {
"policies": [],
"description": "Upload a file",
"tag": {
"plugin": "upload",
"name": "File"
}
}
},
{ {
"method": "GET", "method": "GET",
"path": "/settings", "path": "/settings",
@ -29,6 +16,28 @@
"policies": [] "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", "method": "GET",
"path": "/files/count", "path": "/files/count",

View File

@ -106,6 +106,44 @@ module.exports = {
ctx.body = { data }; 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) { async find(ctx) {
const data = await strapi.plugins['upload'].services.upload.fetchAll(ctx.query); const data = await strapi.plugins['upload'].services.upload.fetchAll(ctx.query);

View File

@ -99,6 +99,35 @@ module.exports = {
return Promise.all(files.map(file => uploadFile(file))); 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) { add(values) {
return strapi.query('file', 'upload').create(values); return strapi.query('file', 'upload').create(values);
}, },