mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
⚡ Get rid of mmmagic for mime-type detection
This commit is contained in:
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user