Skip to content

Commit

Permalink
fix: handle error when check is image valid
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Hsieh <[email protected]>
  • Loading branch information
galaxian85 committed Jan 13, 2023
1 parent 8efc2d0 commit 1ab2a36
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/imageRouter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1ab2a36

Please sign in to comment.