2020-07-02 18:49:20 +02:00
|
|
|
'use strict';
|
|
|
|
|
2021-09-20 18:50:48 +02:00
|
|
|
const _ = require('lodash');
|
2021-11-05 10:40:11 +01:00
|
|
|
const utils = require('@strapi/utils');
|
2021-09-20 18:50:48 +02:00
|
|
|
const { getService } = require('../utils');
|
|
|
|
const validateSettings = require('./validation/settings');
|
|
|
|
const validateUploadBody = require('./validation/upload');
|
2020-07-02 18:49:20 +02:00
|
|
|
|
2021-11-05 10:40:11 +01:00
|
|
|
const { sanitize } = utils;
|
|
|
|
const { ValidationError } = utils.errors;
|
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
const sanitizeOutput = (data, ctx) => {
|
|
|
|
const schema = strapi.getModel('plugin::upload.file');
|
|
|
|
const { auth } = ctx.state;
|
|
|
|
|
|
|
|
return sanitize.contentAPI.output(data, schema, { auth });
|
2020-10-01 17:47:08 +02:00
|
|
|
};
|
|
|
|
|
2020-07-02 18:49:20 +02:00
|
|
|
module.exports = {
|
|
|
|
async find(ctx) {
|
2021-10-07 17:23:42 +02:00
|
|
|
const files = await getService('upload').findMany(ctx.query);
|
2020-10-01 17:47:08 +02:00
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
ctx.body = await sanitizeOutput(files, ctx);
|
2020-07-02 18:49:20 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
async findOne(ctx) {
|
|
|
|
const {
|
|
|
|
params: { id },
|
|
|
|
} = ctx;
|
|
|
|
|
2021-09-24 15:40:02 +02:00
|
|
|
const file = await getService('upload').findOne(id);
|
2020-07-02 18:49:20 +02:00
|
|
|
|
2020-10-01 17:47:08 +02:00
|
|
|
if (!file) {
|
2020-07-02 18:49:20 +02:00
|
|
|
return ctx.notFound('file.notFound');
|
|
|
|
}
|
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
ctx.body = await sanitizeOutput(file, ctx);
|
2020-07-02 18:49:20 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
async destroy(ctx) {
|
|
|
|
const {
|
|
|
|
params: { id },
|
|
|
|
} = ctx;
|
|
|
|
|
2021-09-24 15:40:02 +02:00
|
|
|
const file = await getService('upload').findOne(id);
|
2020-07-02 18:49:20 +02:00
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
return ctx.notFound('file.notFound');
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:27:00 +02:00
|
|
|
await getService('upload').remove(file);
|
2020-07-02 18:49:20 +02:00
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
ctx.body = await sanitizeOutput(file, ctx);
|
2020-07-02 18:49:20 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
async updateSettings(ctx) {
|
|
|
|
const {
|
|
|
|
request: { body },
|
|
|
|
} = ctx;
|
|
|
|
|
|
|
|
const data = await validateSettings(body);
|
|
|
|
|
2021-07-28 21:03:32 +02:00
|
|
|
await getService('upload').setSettings(data);
|
2020-07-02 18:49:20 +02:00
|
|
|
|
|
|
|
ctx.body = { data };
|
|
|
|
},
|
|
|
|
|
|
|
|
async getSettings(ctx) {
|
2021-07-28 21:03:32 +02:00
|
|
|
const data = await getService('upload').getSettings();
|
2020-07-02 18:49:20 +02:00
|
|
|
|
|
|
|
ctx.body = { data };
|
|
|
|
},
|
|
|
|
|
|
|
|
async updateFileInfo(ctx) {
|
|
|
|
const {
|
|
|
|
query: { id },
|
|
|
|
request: { body },
|
|
|
|
} = ctx;
|
|
|
|
const data = await validateUploadBody(body);
|
|
|
|
|
2021-07-28 21:03:32 +02:00
|
|
|
const result = await getService('upload').updateFileInfo(id, data.fileInfo);
|
2020-10-01 17:47:08 +02:00
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
ctx.body = await sanitizeOutput(result, ctx);
|
2020-07-02 18:49:20 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
async replaceFile(ctx) {
|
|
|
|
const {
|
2020-07-23 14:21:45 +02:00
|
|
|
query: { id },
|
2020-07-02 18:49:20 +02:00
|
|
|
request: { body, files: { files } = {} },
|
2020-07-23 14:21:45 +02:00
|
|
|
} = ctx;
|
2020-07-02 18:49:20 +02:00
|
|
|
|
|
|
|
// cannot replace with more than one file
|
|
|
|
if (Array.isArray(files)) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ValidationError('Cannot replace a file with multiple ones');
|
2020-07-02 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 21:03:32 +02:00
|
|
|
const replacedFiles = await getService('upload').replace(id, {
|
2020-07-02 18:49:20 +02:00
|
|
|
data: await validateUploadBody(body),
|
|
|
|
file: files,
|
|
|
|
});
|
2020-10-01 17:47:08 +02:00
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
ctx.body = await sanitizeOutput(replacedFiles, ctx);
|
2020-07-02 18:49:20 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
async uploadFiles(ctx) {
|
|
|
|
const {
|
|
|
|
request: { body, files: { files } = {} },
|
2020-07-23 14:21:45 +02:00
|
|
|
} = ctx;
|
2020-07-02 18:49:20 +02:00
|
|
|
|
2021-07-28 21:03:32 +02:00
|
|
|
const uploadedFiles = await getService('upload').upload({
|
2020-07-02 18:49:20 +02:00
|
|
|
data: await validateUploadBody(body),
|
|
|
|
files,
|
|
|
|
});
|
2020-10-01 17:47:08 +02:00
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
ctx.body = await sanitizeOutput(uploadedFiles, ctx);
|
2020-07-02 18:49:20 +02:00
|
|
|
},
|
2021-09-20 18:50:48 +02:00
|
|
|
|
|
|
|
async upload(ctx) {
|
|
|
|
const {
|
|
|
|
query: { id },
|
|
|
|
request: { files: { files } = {} },
|
|
|
|
} = ctx;
|
|
|
|
|
|
|
|
if (id && (_.isEmpty(files) || files.size === 0)) {
|
|
|
|
return this.updateFileInfo(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isEmpty(files) || files.size === 0) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ValidationError('Files are empty');
|
2021-09-20 18:50:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await (id ? this.replaceFile : this.uploadFiles)(ctx);
|
|
|
|
},
|
|
|
|
|
|
|
|
async search(ctx) {
|
|
|
|
const { id } = ctx.params;
|
|
|
|
const entries = await strapi.query('plugin::upload.file').findMany({
|
|
|
|
where: {
|
|
|
|
$or: [{ hash: { $contains: id } }, { name: { $contains: id } }],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
ctx.body = await sanitizeOutput(entries, ctx);
|
2021-09-20 18:50:48 +02:00
|
|
|
},
|
2020-07-02 18:49:20 +02:00
|
|
|
};
|