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

@@ -44,14 +44,9 @@ import * as express from 'express';
import * as path from 'path';
import { OptionsWithUrl, OptionsWithUri } from 'request';
import * as requestPromise from 'request-promise-native';
import { Magic, MAGIC_MIME_TYPE } from 'mmmagic';
import { createHmac } from 'crypto';
const magic = new Magic(MAGIC_MIME_TYPE);
import { fromBuffer } from 'file-type';
import { lookup } from 'mime-types';
/**
@@ -66,18 +61,28 @@ const magic = new Magic(MAGIC_MIME_TYPE);
*/
export async function prepareBinaryData(binaryData: Buffer, filePath?: string, mimeType?: string): Promise<IBinaryData> {
if (!mimeType) {
// If not 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);
}
// If no mime type is given figure it out
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 = {