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');
|
2022-05-13 16:10:18 +02:00
|
|
|
const { FILE_MODEL_UID } = require('../constants');
|
2022-04-06 12:02:47 +02:00
|
|
|
const validateUploadBody = require('./validation/content-api/upload');
|
2020-07-02 18:49:20 +02:00
|
|
|
|
2023-08-10 15:24:35 +02:00
|
|
|
const { sanitize, validate } = utils;
|
2021-11-05 10:40:11 +01:00
|
|
|
const { ValidationError } = utils.errors;
|
|
|
|
|
2023-08-17 17:24:11 +02:00
|
|
|
const sanitizeOutput = async (data, ctx) => {
|
2022-05-13 16:10:18 +02:00
|
|
|
const schema = strapi.getModel(FILE_MODEL_UID);
|
2021-11-04 15:47:53 +01:00
|
|
|
const { auth } = ctx.state;
|
|
|
|
|
2022-05-05 14:32:09 +02:00
|
|
|
return sanitize.contentAPI.output(data, schema, { auth });
|
2020-10-01 17:47:08 +02:00
|
|
|
};
|
2023-08-11 13:13:44 +02:00
|
|
|
|
2023-08-17 17:24:11 +02:00
|
|
|
const validateQuery = async (data, ctx) => {
|
2023-02-09 11:35:50 +01:00
|
|
|
const schema = strapi.getModel(FILE_MODEL_UID);
|
|
|
|
const { auth } = ctx.state;
|
|
|
|
|
2023-08-10 15:24:35 +02:00
|
|
|
return validate.contentAPI.query(data, schema, { auth });
|
2023-02-09 11:35:50 +01:00
|
|
|
};
|
2020-10-01 17:47:08 +02:00
|
|
|
|
2023-08-17 17:24:11 +02:00
|
|
|
const sanitizeQuery = async (data, ctx) => {
|
2023-08-11 13:13:44 +02:00
|
|
|
const schema = strapi.getModel(FILE_MODEL_UID);
|
|
|
|
const { auth } = ctx.state;
|
|
|
|
|
2023-08-11 17:04:11 +02:00
|
|
|
return sanitize.contentAPI.query(data, schema, { auth });
|
2023-08-11 13:13:44 +02:00
|
|
|
};
|
|
|
|
|
2020-07-02 18:49:20 +02:00
|
|
|
module.exports = {
|
|
|
|
async find(ctx) {
|
2023-08-11 13:13:44 +02:00
|
|
|
await validateQuery(ctx.query, ctx);
|
|
|
|
const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
|
2023-02-09 11:35:50 +01:00
|
|
|
|
2023-08-11 13:13:44 +02:00
|
|
|
const files = await getService('upload').findMany(sanitizedQuery);
|
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;
|
|
|
|
|
2023-08-11 13:13:44 +02:00
|
|
|
await validateQuery(ctx.query, ctx);
|
|
|
|
const sanitizedQuery = await sanitizeQuery(ctx.query, ctx);
|
|
|
|
|
|
|
|
const file = await getService('upload').findOne(id, sanitizedQuery.populate);
|
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 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
|
|
|
|
2022-08-08 15:26:09 +02:00
|
|
|
const data = await validateUploadBody(body, Array.isArray(files));
|
2022-05-03 20:33:09 +02:00
|
|
|
|
|
|
|
const apiUploadFolderService = getService('api-upload-folder');
|
|
|
|
|
|
|
|
const apiUploadFolder = await apiUploadFolderService.getAPIUploadFolder();
|
2022-08-06 15:02:01 +02:00
|
|
|
|
2022-08-08 15:08:26 +02:00
|
|
|
if (Array.isArray(files)) {
|
|
|
|
data.fileInfo = data.fileInfo || [];
|
|
|
|
data.fileInfo = files.map((_f, i) => ({ ...data.fileInfo[i], folder: apiUploadFolder.id }));
|
|
|
|
} else {
|
|
|
|
data.fileInfo = { ...data.fileInfo, folder: apiUploadFolder.id };
|
|
|
|
}
|
2022-05-03 20:33:09 +02:00
|
|
|
|
2021-07-28 21:03:32 +02:00
|
|
|
const uploadedFiles = await getService('upload').upload({
|
2022-05-03 20:33:09 +02:00
|
|
|
data,
|
2020-07-02 18:49:20 +02:00
|
|
|
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 (_.isEmpty(files) || files.size === 0) {
|
2022-05-03 20:33:09 +02:00
|
|
|
if (id) {
|
|
|
|
return this.updateFileInfo(ctx);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
},
|
2020-07-02 18:49:20 +02:00
|
|
|
};
|