diff --git a/package-lock.json b/package-lock.json index 9d46950cc..1fb8191a9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2835,6 +2835,11 @@ "defer-to-connect": "^2.0.0" } }, + "@tokenizer/token": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@tokenizer/token/-/token-0.3.0.tgz", + "integrity": "sha512-OvjF+z51L3ov0OyAU0duzsYuvO01PH7x4t6DJx+guahgTnBHkhJdG7soQeTSFLWN3efnHyibZ4Z8l2EuWwJN3A==" + }, "@tsconfig/node10": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.8.tgz", @@ -6714,6 +6719,16 @@ "flat-cache": "^3.0.4" } }, + "file-type": { + "version": "16.5.3", + "resolved": "https://registry.npmjs.org/file-type/-/file-type-16.5.3.tgz", + "integrity": "sha512-uVsl7iFhHSOY4bEONLlTK47iAHtNsFHWP5YE4xJfZ4rnX7S1Q3wce09XgqSC7E/xh8Ncv/be1lNoyprlUH/x6A==", + "requires": { + "readable-web-to-node-stream": "^3.0.0", + "strtok3": "^6.2.4", + "token-types": "^4.1.1" + } + }, "file-uri-to-path": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", @@ -10690,6 +10705,11 @@ "integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==", "dev": true }, + "peek-readable": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/peek-readable/-/peek-readable-4.0.1.tgz", + "integrity": "sha512-7qmhptnR0WMSpxT5rMHG9bW/mYSR1uqaPFj2MHvT+y/aOUu6msJijpKt5SkTDKySwg65OWG2JwTMBlgcbwMHrQ==" + }, "pend": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", @@ -11220,6 +11240,14 @@ "util-deprecate": "^1.0.1" } }, + "readable-web-to-node-stream": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/readable-web-to-node-stream/-/readable-web-to-node-stream-3.0.2.tgz", + "integrity": "sha512-ePeK6cc1EcKLEhJFt/AebMCLL+GgSKhuygrZ/GLaKZYEecIgIECf4UaUuaByiGtzckwR4ain9VzUh95T1exYGw==", + "requires": { + "readable-stream": "^3.6.0" + } + }, "readdirp": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-2.2.1.tgz", @@ -12597,6 +12625,15 @@ "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=" }, + "strtok3": { + "version": "6.2.4", + "resolved": "https://registry.npmjs.org/strtok3/-/strtok3-6.2.4.tgz", + "integrity": "sha512-GO8IcFF9GmFDvqduIspUBwCzCbqzegyVKIsSymcMgiZKeCfrN9SowtUoi8+b59WZMAjIzVZic/Ft97+pynR3Iw==", + "requires": { + "@tokenizer/token": "^0.3.0", + "peek-readable": "^4.0.1" + } + }, "supports-color": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", @@ -12952,6 +12989,15 @@ } } }, + "token-types": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/token-types/-/token-types-4.1.1.tgz", + "integrity": "sha512-hD+QyuUAyI2spzsI0B7gf/jJ2ggR4RjkAo37j3StuePhApJUwcWDjnHDOFdIWYSwNR28H14hpwm4EI+V1Ted1w==", + "requires": { + "@tokenizer/token": "^0.3.0", + "ieee754": "^1.2.1" + } + }, "tree-kill": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/tree-kill/-/tree-kill-1.2.2.tgz", diff --git a/package.json b/package.json index 6ba63ed60..73bde17fb 100644 --- a/package.json +++ b/package.json @@ -111,6 +111,7 @@ "chalk": "^4.0.0", "chrome-launcher": "^0.14.0", "execa": "^5.0.0", + "file-type": "^16.5.3", "fsevents": "^2.3.1", "futoin-hkdf": "^1.3.2", "latest-version": "^5.1.0", diff --git a/src/api/helpers/file-to-base64.ts b/src/api/helpers/file-to-base64.ts index 70d6cb3fa..0e7eea459 100644 --- a/src/api/helpers/file-to-base64.ts +++ b/src/api/helpers/file-to-base64.ts @@ -15,7 +15,8 @@ * along with WPPConnect. If not, see . */ -const mimeTypes = require('mime-types'); +import * as mimeTypes from 'mime-types'; +import * as fileType from 'file-type'; import * as fs from 'fs'; /** @@ -23,11 +24,18 @@ import * as fs from 'fs'; * @param path file path * @param mime Optional, will retrieve file mime automatically if not defined (Example: 'image/png') */ -export async function fileToBase64(path: string, mime?: string) { +export async function fileToBase64(path: string, mime?: string | false) { if (fs.existsSync(path)) { const base64 = fs.readFileSync(path, { encoding: 'base64' }); if (mime === undefined) { - mime = await mimeTypes.lookup(path); + mime = mimeTypes.lookup(path); + } + if (!mime) { + const result = await fileType.fromFile(path); + mime = result?.mime; + } + if (!mime) { + mime = 'application/octet-stream'; } const data = `data:${mime};base64,${base64}`; return data;