@digicms/cms
Version:
An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MySQL, MariaDB, PostgreSQL, SQLite
83 lines (68 loc) • 1.99 kB
JavaScript
;
const fse = require('fs-extra');
const { defaultsDeep, get } = require('lodash/fp');
const body = require('koa-body');
const mime = require('mime-types');
const defaults = {
multipart: true,
patchKoa: true,
};
function ensureFileMimeType(file) {
if (!file.type) {
file.type = mime.lookup(file.name) || 'application/octet-stream';
}
}
function getFiles(ctx) {
return get('request.files.files', ctx);
}
/**
* @type {import('./').MiddlewareFactory}
*/
module.exports = (config, { strapi }) => {
const bodyConfig = defaultsDeep(defaults, config);
let gqlEndpoint;
if (strapi.plugin('graphql')) {
const { config: gqlConfig } = strapi.plugin('graphql');
gqlEndpoint = gqlConfig('endpoint');
}
return async (ctx, next) => {
// TODO: find a better way later
if (gqlEndpoint && ctx.url === gqlEndpoint) {
await next();
} else {
try {
await body({ patchKoa: true, ...bodyConfig })(ctx, () => {});
const files = getFiles(ctx);
/**
* in case the mime-type wasn't sent, Strapi tries to guess it
* from the file extension, to avoid a corrupt database state
*/
if (files) {
if (Array.isArray(files)) {
files.forEach(ensureFileMimeType);
} else {
ensureFileMimeType(files);
}
}
await next();
} catch (e) {
if ((e || {}).message && e.message.includes('maxFileSize exceeded')) {
return ctx.payloadTooLarge('FileTooBig');
}
throw e;
}
}
const files = getFiles(ctx);
// clean any file that was uploaded
if (files) {
if (Array.isArray(files)) {
// not awaiting to not slow the request
Promise.all(files.map((file) => fse.remove(file.path)));
} else if (files && files.path) {
// not awaiting to not slow the request
fse.remove(files.path);
}
delete ctx.request.files;
}
};
};