mirror of
https://github.com/strapi/strapi.git
synced 2025-09-02 05:13:03 +00:00
wip
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
parent
b084cb1c65
commit
7cd34b725a
@ -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",
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user