mirror of
https://github.com/strapi/strapi.git
synced 2025-08-01 05:17:14 +00:00
50 lines
1.2 KiB
JavaScript
50 lines
1.2 KiB
JavaScript
const _ = require('lodash');
|
|
|
|
const parseMultipartBody = require('../../utils/parse-multipart');
|
|
const uploadFiles = require('../../utils/upload-files');
|
|
|
|
module.exports = async (ctx, next) => {
|
|
const { model } = ctx.params;
|
|
|
|
const ct = strapi.contentTypes[model];
|
|
|
|
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('.');
|
|
|
|
if (controller && action) {
|
|
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;
|
|
|
|
await uploadFiles(resBody, files, {
|
|
model: ct.modelName,
|
|
source: ct.plugin,
|
|
});
|
|
|
|
return ctx.send(resBody);
|
|
}
|
|
|
|
return await target.controllers[controller.toLowerCase()][action](ctx);
|
|
}
|
|
}
|
|
|
|
await next();
|
|
};
|