2017-11-29 15:00:50 +01:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2019-08-05 10:31:18 +02:00
|
|
|
const parseMultipartBody = require('../../utils/parse-multipart');
|
|
|
|
const uploadFiles = require('../../utils/upload-files');
|
|
|
|
|
2017-11-29 15:00:50 +01:00
|
|
|
module.exports = async (ctx, next) => {
|
2019-08-21 11:05:33 +02:00
|
|
|
const { model } = ctx.params;
|
2017-11-29 15:00:50 +01:00
|
|
|
|
2019-11-15 11:49:32 +01:00
|
|
|
const ct = strapi.contentTypes[model];
|
|
|
|
|
2019-11-26 17:18:43 +01:00
|
|
|
if (!ct) {
|
|
|
|
return ctx.send({ error: 'contentType.notFound' }, 404);
|
|
|
|
}
|
|
|
|
|
2019-11-15 11:49:32 +01:00
|
|
|
const target =
|
|
|
|
ct.plugin === 'admin' ? strapi.admin : strapi.plugins[ct.plugin];
|
|
|
|
|
|
|
|
const actionPath = [
|
|
|
|
'config',
|
|
|
|
'layout',
|
|
|
|
ct.modelName,
|
|
|
|
'actions',
|
|
|
|
ctx.request.route.action,
|
|
|
|
];
|
|
|
|
|
|
|
|
if (_.has(target, actionPath)) {
|
|
|
|
const [controller, action] = _.get(target, actionPath, []).split('.');
|
2017-11-29 15:00:50 +01:00
|
|
|
|
2017-11-29 16:10:13 +01:00
|
|
|
if (controller && action) {
|
2019-08-05 10:31:18 +02:00
|
|
|
if (ctx.is('multipart')) {
|
|
|
|
const { data, files } = parseMultipartBody(ctx);
|
|
|
|
ctx.request.body = data;
|
|
|
|
ctx.request.files = {};
|
|
|
|
|
|
|
|
await target.controllers[controller.toLowerCase()][action](ctx);
|
|
|
|
const resBody = ctx.body;
|
|
|
|
|
|
|
|
if (ctx.status >= 300) return;
|
|
|
|
|
2019-11-15 11:49:32 +01:00
|
|
|
await uploadFiles(resBody, files, {
|
|
|
|
model: ct.modelName,
|
|
|
|
source: ct.plugin,
|
|
|
|
});
|
2019-08-05 10:31:18 +02:00
|
|
|
|
|
|
|
return ctx.send(resBody);
|
|
|
|
}
|
|
|
|
|
2019-04-09 12:09:03 +02:00
|
|
|
return await target.controllers[controller.toLowerCase()][action](ctx);
|
2017-11-29 16:10:13 +01:00
|
|
|
}
|
2017-11-29 15:00:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
await next();
|
|
|
|
};
|