From 1ab2a365c8a23d936d4a57d808eb02545de8fb5f Mon Sep 17 00:00:00 2001 From: Charlie Hsieh Date: Sat, 7 Jan 2023 15:28:05 +0800 Subject: [PATCH] fix: handle error when check is image valid Signed-off-by: Charlie Hsieh --- lib/imageRouter/index.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/imageRouter/index.js b/lib/imageRouter/index.js index 5e2d3dfbb3..abe044ed2c 100644 --- a/lib/imageRouter/index.js +++ b/lib/imageRouter/index.js @@ -16,13 +16,18 @@ const response = require('../response') const imageRouter = module.exports = Router() function checkImageValid (filepath) { - const buffer = readChunk.sync(filepath, 0, 12) - /** @type {{ ext: string, mime: string } | null} */ - const mimetypeFromBuf = imageType(buffer) - const mimeTypeFromExt = mime.lookup(path.extname(filepath)) + try { + const buffer = readChunk.sync(filepath, 0, 12) + /** @type {{ ext: string, mime: string } | null} */ + const mimetypeFromBuf = imageType(buffer) + const mimeTypeFromExt = mime.lookup(path.extname(filepath)) - return mimetypeFromBuf && config.allowedUploadMimeTypes.includes(mimetypeFromBuf.mime) && - mimeTypeFromExt && config.allowedUploadMimeTypes.includes(mimeTypeFromExt) + return mimetypeFromBuf && config.allowedUploadMimeTypes.includes(mimetypeFromBuf.mime) && + mimeTypeFromExt && config.allowedUploadMimeTypes.includes(mimeTypeFromExt) + } catch (err) { + logger.error(err) + return false + } } // upload image