mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-23 03:27:05 -04:00
Merge pull request #1079 from hedgedoc/fix/mimeTypes
This commit is contained in:
commit
1534d7029b
4 changed files with 26 additions and 12 deletions
|
@ -7,6 +7,7 @@ const FileType = require('file-type')
|
|||
const fs = require('fs')
|
||||
const os = require('os')
|
||||
const rimraf = require('rimraf')
|
||||
const isSvg = require('is-svg')
|
||||
|
||||
const config = require('../../config')
|
||||
const logger = require('../../logger')
|
||||
|
@ -15,12 +16,26 @@ const errors = require('../../errors')
|
|||
const imageRouter = (module.exports = Router())
|
||||
|
||||
async function checkUploadType (filePath) {
|
||||
const typeFromMagic = await FileType.fromFile(filePath)
|
||||
const extension = path.extname(filePath).toLowerCase()
|
||||
let typeFromMagic = await FileType.fromFile(filePath)
|
||||
if (extension === '.svg' && (typeFromMagic === undefined || typeFromMagic.mime === 'application/xml')) {
|
||||
const fileContent = fs.readFileSync(filePath)
|
||||
if (isSvg(fileContent)) {
|
||||
typeFromMagic = {
|
||||
ext: 'svg',
|
||||
mime: 'image/svg+xml'
|
||||
}
|
||||
}
|
||||
}
|
||||
if (typeFromMagic === undefined) {
|
||||
logger.error('Image upload error: Could not determine MIME-type')
|
||||
return false
|
||||
}
|
||||
if (path.extname(filePath) !== '.' + typeFromMagic.ext) {
|
||||
// .jpeg, .jfif, .jpe files are identified by FileType to have the extension jpg
|
||||
if (['.jpeg', '.jfif', '.jpe'].includes(extension) && typeFromMagic.ext === 'jpg') {
|
||||
typeFromMagic.ext = extension.substr(1)
|
||||
}
|
||||
if (extension !== '.' + typeFromMagic.ext) {
|
||||
logger.error(
|
||||
'Image upload error: Provided file extension does not match MIME-type'
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue