2018-02-08 12:01:06 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload.js controller
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-02-21 14:06:57 +01:00
|
|
|
const _ = require('lodash');
|
2020-02-26 18:34:45 +01:00
|
|
|
const validateSettings = require('./validation/settings');
|
2020-03-03 15:54:59 +01:00
|
|
|
const { yup, formatYupErrors } = require('strapi-utils');
|
|
|
|
|
|
|
|
const fileInfoSchema = yup.object({
|
|
|
|
name: yup.string().nullable(),
|
|
|
|
alternativeText: yup.string().nullable(),
|
|
|
|
caption: yup.string().nullable(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const uploadSchema = yup.object({
|
|
|
|
fileInfo: fileInfoSchema,
|
|
|
|
});
|
|
|
|
|
|
|
|
const multiUploadSchema = yup.object({
|
|
|
|
fileInfo: yup.array().of(fileInfoSchema),
|
|
|
|
});
|
|
|
|
|
|
|
|
const validateUploadBody = (schema, data = {}) => {
|
|
|
|
return schema.validate(data, { abortEarly: false }).catch(err => {
|
|
|
|
throw strapi.errors.badRequest('ValidationError', { errors: formatYupErrors(err) });
|
|
|
|
});
|
|
|
|
};
|
2018-02-21 14:06:57 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const isUploadDisabled = () => _.get(strapi.plugins, 'upload.config.enabled', true) === false;
|
|
|
|
|
|
|
|
const disabledPluginError = () =>
|
|
|
|
strapi.errors.badRequest(null, {
|
|
|
|
errors: [{ id: 'Upload.status.disabled', message: 'File upload is disabled' }],
|
|
|
|
});
|
2019-08-01 07:48:52 +02:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const emptyFileError = () =>
|
|
|
|
strapi.errors.badRequest(null, {
|
|
|
|
errors: [{ id: 'Upload.status.empty', message: 'Files are empty' }],
|
|
|
|
});
|
2018-02-20 17:10:25 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
module.exports = {
|
|
|
|
async upload(ctx) {
|
|
|
|
if (isUploadDisabled()) {
|
|
|
|
throw disabledPluginError();
|
2018-02-20 17:10:25 +01:00
|
|
|
}
|
|
|
|
|
2020-03-03 15:54:59 +01:00
|
|
|
const files = _.get(ctx.request.files, 'files');
|
2020-03-08 18:43:50 +01:00
|
|
|
if (_.isEmpty(files) || files.size === 0) {
|
2020-03-05 13:51:15 +01:00
|
|
|
throw emptyFileError();
|
2018-02-21 14:46:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const { id } = ctx.query;
|
2020-03-03 15:54:59 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const uploadService = strapi.plugins.upload.services.upload;
|
2020-03-03 15:54:59 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
const validationSchema = Array.isArray(files) ? multiUploadSchema : uploadSchema;
|
|
|
|
const data = await validateUploadBody(validationSchema, ctx.request.body);
|
2020-03-03 15:54:59 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
if (id) {
|
|
|
|
// cannot replace with more than one file
|
|
|
|
if (Array.isArray(files)) {
|
|
|
|
throw strapi.errors.badRequest(null, {
|
|
|
|
errors: [
|
|
|
|
{ id: 'Upload.replace.single', message: 'Cannot replace a file with multiple ones' },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
2018-02-08 12:01:06 +01:00
|
|
|
|
2020-03-05 13:51:15 +01:00
|
|
|
ctx.body = await uploadService.replace(id, { data, file: files });
|
|
|
|
} else {
|
|
|
|
ctx.body = await uploadService.upload({ data, files });
|
|
|
|
}
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async getSettings(ctx) {
|
2020-03-08 12:59:21 +01:00
|
|
|
const data = await strapi.plugins.upload.services.upload.getSettings();
|
|
|
|
|
|
|
|
ctx.body = { data };
|
2018-02-20 17:10:25 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async updateSettings(ctx) {
|
2020-02-26 18:34:45 +01:00
|
|
|
const data = await validateSettings(ctx.request.body);
|
2018-02-20 17:10:25 +01:00
|
|
|
|
2020-03-08 12:59:21 +01:00
|
|
|
await strapi.plugins.upload.services.upload.setSettings(data);
|
2020-02-26 18:34:45 +01:00
|
|
|
|
|
|
|
ctx.body = { data };
|
2018-02-20 17:10:25 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async find(ctx) {
|
2020-02-26 14:56:27 +01:00
|
|
|
const data = await strapi.plugins['upload'].services.upload.fetchAll(ctx.query);
|
2018-02-19 15:41:26 +01:00
|
|
|
|
2020-03-08 12:59:21 +01:00
|
|
|
ctx.body = data;
|
2018-02-19 15:41:26 +01:00
|
|
|
},
|
2018-02-19 16:00:37 +01:00
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async findOne(ctx) {
|
2020-03-06 19:16:23 +01:00
|
|
|
const { id } = ctx.params;
|
|
|
|
const data = await strapi.plugins['upload'].services.upload.fetch({ id });
|
2018-04-09 19:31:49 +02:00
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
if (!data) {
|
|
|
|
return ctx.notFound('file.notFound');
|
|
|
|
}
|
2018-04-09 19:31:49 +02:00
|
|
|
|
2020-03-08 12:59:21 +01:00
|
|
|
ctx.body = data;
|
2018-04-09 19:31:49 +02:00
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async count(ctx) {
|
2020-02-26 14:56:27 +01:00
|
|
|
const data = await strapi.plugins['upload'].services.upload.count(ctx.query);
|
2018-02-19 19:54:45 +01:00
|
|
|
|
2020-03-08 12:59:21 +01:00
|
|
|
ctx.body = { count: data };
|
2018-02-19 19:54:45 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async destroy(ctx) {
|
|
|
|
const { id } = ctx.params;
|
2018-02-20 17:10:25 +01:00
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
const file = await strapi.plugins['upload'].services.upload.fetch({ id });
|
2018-02-19 16:00:37 +01:00
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
if (!file) {
|
|
|
|
return ctx.notFound('file.notFound');
|
|
|
|
}
|
|
|
|
|
2020-02-26 19:38:23 +01:00
|
|
|
await strapi.plugins['upload'].services.upload.remove(file);
|
2019-07-15 18:28:56 +02:00
|
|
|
|
2020-03-08 12:59:21 +01:00
|
|
|
ctx.body = file;
|
2018-02-22 17:12:03 +01:00
|
|
|
},
|
|
|
|
|
2019-07-15 18:28:56 +02:00
|
|
|
async search(ctx) {
|
|
|
|
const { id } = ctx.params;
|
|
|
|
|
|
|
|
const data = await strapi.query('file', 'upload').custom(searchQueries)({
|
|
|
|
id,
|
|
|
|
});
|
2018-02-22 17:12:03 +01:00
|
|
|
|
2020-03-08 12:59:21 +01:00
|
|
|
ctx.body = data;
|
2018-02-22 17:12:03 +01:00
|
|
|
},
|
2018-02-08 12:01:06 +01:00
|
|
|
};
|
2019-07-15 18:28:56 +02:00
|
|
|
|
|
|
|
const searchQueries = {
|
|
|
|
bookshelf({ model }) {
|
|
|
|
return ({ id }) => {
|
|
|
|
return model
|
|
|
|
.query(qb => {
|
2020-02-26 14:56:27 +01:00
|
|
|
qb.whereRaw('LOWER(hash) LIKE ?', [`%${id}%`]).orWhereRaw('LOWER(name) LIKE ?', [
|
2020-02-13 12:30:26 +01:00
|
|
|
`%${id}%`,
|
2020-02-26 14:56:27 +01:00
|
|
|
]);
|
2019-07-15 18:28:56 +02:00
|
|
|
})
|
|
|
|
.fetchAll()
|
|
|
|
.then(results => results.toJSON());
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mongoose({ model }) {
|
|
|
|
return ({ id }) => {
|
|
|
|
const re = new RegExp(id, 'i');
|
|
|
|
|
2019-12-24 17:49:17 +01:00
|
|
|
return model
|
|
|
|
.find({
|
|
|
|
$or: [{ hash: re }, { name: re }],
|
|
|
|
})
|
|
|
|
.lean();
|
2019-07-15 18:28:56 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
};
|