Get rid of mmmagic for mime-type detection

This commit is contained in:
Jan Oberhauser 2020-07-08 09:40:47 +02:00
parent 6fed70e26b
commit 224842c790
2 changed files with 25 additions and 19 deletions

View file

@ -30,7 +30,7 @@
"@types/express": "^4.16.1", "@types/express": "^4.16.1",
"@types/jest": "^24.0.18", "@types/jest": "^24.0.18",
"@types/lodash.get": "^4.4.6", "@types/lodash.get": "^4.4.6",
"@types/mmmagic": "^0.4.29", "@types/mime-types": "^2.1.0",
"@types/node": "^10.10.1", "@types/node": "^10.10.1",
"@types/request-promise-native": "^1.0.15", "@types/request-promise-native": "^1.0.15",
"jest": "^24.9.0", "jest": "^24.9.0",
@ -43,8 +43,9 @@
"client-oauth2": "^4.2.5", "client-oauth2": "^4.2.5",
"cron": "^1.7.2", "cron": "^1.7.2",
"crypto-js": "3.1.9-1", "crypto-js": "3.1.9-1",
"file-type": "^14.6.2",
"lodash.get": "^4.4.2", "lodash.get": "^4.4.2",
"mmmagic": "^0.5.2", "mime-types": "^2.1.27",
"n8n-workflow": "~0.33.0", "n8n-workflow": "~0.33.0",
"p-cancelable": "^2.0.0", "p-cancelable": "^2.0.0",
"request": "^2.88.2", "request": "^2.88.2",

View file

@ -44,14 +44,9 @@ import * as express from 'express';
import * as path from 'path'; import * as path from 'path';
import { OptionsWithUrl, OptionsWithUri } from 'request'; import { OptionsWithUrl, OptionsWithUri } from 'request';
import * as requestPromise from 'request-promise-native'; import * as requestPromise from 'request-promise-native';
import { Magic, MAGIC_MIME_TYPE } from 'mmmagic';
import { createHmac } from 'crypto'; import { createHmac } from 'crypto';
import { fromBuffer } from 'file-type';
import { lookup } from 'mime-types';
const magic = new Magic(MAGIC_MIME_TYPE);
/** /**
@ -66,18 +61,28 @@ const magic = new Magic(MAGIC_MIME_TYPE);
*/ */
export async function prepareBinaryData(binaryData: Buffer, filePath?: string, mimeType?: string): Promise<IBinaryData> { export async function prepareBinaryData(binaryData: Buffer, filePath?: string, mimeType?: string): Promise<IBinaryData> {
if (!mimeType) { if (!mimeType) {
// If not mime type is given figure it out // If no mime type is given figure it out
mimeType = await new Promise<string>(
(resolve, reject) => {
magic.detect(binaryData, (err: Error, mimeType: string) => {
if (err) {
return reject(err);
}
return resolve(mimeType); if (filePath) {
}); // Use file path to guess mime type
const mimeTypeLookup = lookup(filePath);
if (mimeTypeLookup) {
mimeType = mimeTypeLookup;
} }
); }
if (!mimeType) {
// Use buffer to guess mime type
const fileTypeData = await fromBuffer(binaryData);
if (fileTypeData) {
mimeType = fileTypeData.mime;
}
}
if (!mimeType) {
// Fall back to text
mimeType = 'text/plain';
}
} }
const returnData: IBinaryData = { const returnData: IBinaryData = {