diff --git a/mqtt/client.js b/mqtt/client.js index 88dc895..58677ae 100644 --- a/mqtt/client.js +++ b/mqtt/client.js @@ -2,6 +2,7 @@ const MQTT = require('async-mqtt') const get = require('get-value') const fetch = require('node-fetch') const winston = require('winston') +const { validateBufferMIMEType } = require('validate-image-type') const { sleep } = require('eufy-node-client') const DB = require('../db') const config = require('../config') @@ -112,6 +113,7 @@ class MqttClient { } if (attributes.thumbnail && attributes.thumbnail.length > 0) { + await sleep(100) await this.uploadThumbnail(deviceSN, attributes.thumbnail) } } @@ -125,7 +127,27 @@ class MqttClient { async uploadThumbnail(deviceSN, thumbnailUrl) { winston.debug(`Uploading new thumbnail for ${deviceSN} from ${thumbnailUrl}`) const response = await fetch(thumbnailUrl) - const image = await response.buffer() + let image = await response.buffer() + + const result = validateBufferMIMEType(image, { + allowMimeTypes: ['image/jpeg', 'image/gif', 'image/png', 'image/svg+xml'] + }) + + winston.debug(`Image validation result: `, result) + + if (!result.ok) { + winston.error(`Image seems to be invalid. URL: ${thumbnailUrl}`, result) + await sleep(1000) + winston.info(`Retrying image ${thumbnailUrl} after waiting 1 second...`) + const response = await fetch(thumbnailUrl) + image = await response.buffer() + + const result = validateBufferMIMEType(image, { + allowMimeTypes: ['image/jpeg', 'image/gif', 'image/png', 'image/svg+xml'] + }) + + winston.info(`Retry - Image validation result: `, result) + } const topic = HaDiscovery.baseTopicForCapability(NotificationType.THUMBNAIL, deviceSN) diff --git a/package-lock.json b/package-lock.json index 673d79e..2dda350 100644 --- a/package-lock.json +++ b/package-lock.json @@ -825,11 +825,21 @@ "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz", "integrity": "sha512-Utm6CdzT+6xsDk2m8S6uL8VHxNwI6Jub+e9NYTcAms28T84pTa25GJQV9j0CY0N1rM8hK4x6grpF2BQf+2qwVA==" }, + "fast-xml-parser": { + "version": "3.19.0", + "resolved": "https://registry.npmjs.org/fast-xml-parser/-/fast-xml-parser-3.19.0.tgz", + "integrity": "sha512-4pXwmBplsCPv8FOY1WRakF970TjNGnGnfbOnLqjlYvMiF1SR3yOHyxMR/YCXpPTOspNF5gwudqktIP4VsWkvBg==" + }, "fecha": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/fecha/-/fecha-4.2.0.tgz", "integrity": "sha512-aN3pcx/DSmtyoovUudctc8+6Hl4T+hI9GBBHLjA76jdZl7+b1sgh5g4k+u/GL3dTy1/pnYzKp69FpJ0OicE3Wg==" }, + "file-type": { + "version": "10.11.0", + "resolved": "https://registry.npmjs.org/file-type/-/file-type-10.11.0.tgz", + "integrity": "sha512-uzk64HRpUZyTGZtVuvrjP0FYxzQrBf4rojot6J65YMEbwBLB0CWm0CLojVpwpmFmxcE/lkvYICgfcGozbBq6rw==" + }, "fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -1172,6 +1182,14 @@ "minimatch": "^3.0.4" } }, + "image-type": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/image-type/-/image-type-4.1.0.tgz", + "integrity": "sha512-CFJMJ8QK8lJvRlTCEgarL4ro6hfDQKif2HjSvYCdQZESaIPV4v9imrf7BQHK+sQeTeNeMpWciR9hyC/g8ybXEg==", + "requires": { + "file-type": "^10.10.0" + } + }, "import-lazy": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/import-lazy/-/import-lazy-2.1.0.tgz", @@ -1296,6 +1314,14 @@ "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.0.tgz", "integrity": "sha512-XCoy+WlUr7d1+Z8GgSuXmpuUFC9fOhRXglJMx+dwLKTkL44Cjd4W1Z5P+BQZpr+cR93aGP4S/s7Ftw6Nd/kiEw==" }, + "is-svg": { + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/is-svg/-/is-svg-4.3.1.tgz", + "integrity": "sha512-h2CGs+yPUyvkgTJQS9cJzo9lYK06WgRiXUqBBHtglSzVKAuH4/oWsqk7LGfbSa1hGk9QcZ0SyQtVggvBA8LZXA==", + "requires": { + "fast-xml-parser": "^3.19.0" + } + }, "is-typedarray": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-typedarray/-/is-typedarray-1.0.0.tgz", @@ -1843,6 +1869,16 @@ "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-2.1.0.tgz", "integrity": "sha512-HAZyB3ZodPo+BDpb4/Iu7Jv4P6cSazBz9ZM0ChhEXp70scx834aWCEjQRwgt41UzzejUAPdbqqONfRWTPYrPAQ==" }, + "p-finally": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/p-finally/-/p-finally-1.0.0.tgz", + "integrity": "sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=" + }, + "p-try": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/p-try/-/p-try-2.2.0.tgz", + "integrity": "sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==" + }, "package-json": { "version": "6.5.0", "resolved": "https://registry.npmjs.org/package-json/-/package-json-6.5.0.tgz", @@ -1994,6 +2030,11 @@ "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.2.2.tgz", "integrity": "sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==" }, + "pify": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/pify/-/pify-4.0.1.tgz", + "integrity": "sha512-uB80kBFb/tfd68bVleG9T5GGsGPjJrLAUpR5PZIrhBnIaRTQRjqdJSsIKkOP6OAIFbj7GOrcudc5pNjZ+geV2g==" + }, "prepend-http": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/prepend-http/-/prepend-http-2.0.0.tgz", @@ -2108,6 +2149,15 @@ "strip-json-comments": "~2.0.1" } }, + "read-chunk": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/read-chunk/-/read-chunk-3.2.0.tgz", + "integrity": "sha512-CEjy9LCzhmD7nUpJ1oVOE6s/hBkejlcJEgLQHVnQznOSilOPb+kpKktlLfFDK3/WP43+F80xkUTM2VOkYoSYvQ==", + "requires": { + "pify": "^4.0.1", + "with-open-file": "^0.1.6" + } + }, "readable-stream": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", @@ -2607,6 +2657,16 @@ "integrity": "sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==", "optional": true }, + "validate-image-type": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/validate-image-type/-/validate-image-type-1.1.1.tgz", + "integrity": "sha512-HHdBX4fzY8IfVOMFLEyRNSevj6fyPrZjnCiyzBnB8KH1YUDlUL9CLyEu8MpX2DMrAEjkOm9ogygkx0WLZT4JBw==", + "requires": { + "image-type": "^4.1.0", + "is-svg": "^4.2.1", + "read-chunk": "^3.2.0" + } + }, "verror": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/verror/-/verror-1.10.0.tgz", @@ -2721,6 +2781,16 @@ } } }, + "with-open-file": { + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/with-open-file/-/with-open-file-0.1.7.tgz", + "integrity": "sha512-ecJS2/oHtESJ1t3ZfMI3B7KIDKyfN0O16miWxdn30zdh66Yd3LsRFebXZXq6GU4xfxLf6nVxp9kIqElb5fqczA==", + "requires": { + "p-finally": "^1.0.0", + "p-try": "^2.1.0", + "pify": "^4.0.1" + } + }, "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/package.json b/package.json index f4b6337..f4262d9 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "js-yaml": "^3.14.1", "node-fetch": "^2.6.1", "sqlite3": "^5.0.1", + "validate-image-type": "^1.1.1", "winston": "^3.3.3" } }