Skip to content

Commit

Permalink
Merge pull request #12881 from strapi/fix/upload-mime-type
Browse files Browse the repository at this point in the history
ML: Apply fallback mime-type if none is set
  • Loading branch information
gu-stav committed May 11, 2022
2 parents d69b49b + e297cb7 commit 8b3ba51
Show file tree
Hide file tree
Showing 5 changed files with 6,084 additions and 691 deletions.
48 changes: 38 additions & 10 deletions packages/core/strapi/lib/middlewares/body.js
Expand Up @@ -3,12 +3,23 @@
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}
*/
Expand All @@ -18,21 +29,38 @@ module.exports = config => {
return async (ctx, next) => {
// TODO: find a better way later
if (ctx.url === '/graphql') {
return next();
}
await next();
} else {
try {
await body({ patchKoa: true, ...bodyConfig })(ctx, () => {});

try {
await body({ patchKoa: true, ...bodyConfig })(ctx, next);
} catch (e) {
if ((e || {}).message && e.message.includes('maxFileSize exceeded')) {
return ctx.payloadTooLarge('FileTooBig');
}
const files = getFiles(ctx);

throw e;
/**
* 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
const files = get('request.files.files', ctx);
if (files) {
if (Array.isArray(files)) {
// not awaiting to not slow the request
Expand Down
1 change: 1 addition & 0 deletions packages/core/strapi/package.json
Expand Up @@ -117,6 +117,7 @@
"koa-session": "6.2.0",
"koa-static": "5.0.0",
"lodash": "4.17.21",
"mime-types": "2.1.35",
"node-fetch": "2.6.7",
"node-machine-id": "1.1.12",
"node-schedule": "2.0.0",
Expand Down
1 change: 1 addition & 0 deletions packages/core/upload/package.json
Expand Up @@ -33,6 +33,7 @@
"koa-range": "0.3.0",
"koa-static": "5.0.0",
"lodash": "4.17.21",
"mime-types": "2.1.35",
"react": "^17.0.2",
"react-copy-to-clipboard": "^5.0.3",
"react-dom": "^17.0.2",
Expand Down
7 changes: 6 additions & 1 deletion packages/core/upload/server/graphql.js
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const os = require('os');
const mime = require('mime-types');
const fse = require('fs-extra');
const { getStreamSize } = require('./utils/file');

Expand Down Expand Up @@ -48,7 +49,11 @@ module.exports = ({ strapi }) => {
const currentFile = uploadService.formatFileInfo(
{
filename,
type: mimetype,
/**
* in case the mime-type wasn't sent, Strapi tries to guess it
* from the file extension, to avoid a corrupt database state
*/
type: mimetype || mime.lookup(filename) || 'application/octet-stream',
size: await getStreamSize(createReadStream()),
},
extraInfo || {},
Expand Down

0 comments on commit 8b3ba51

Please sign in to comment.