2018-03-17 21:14:50 -04:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const Router = require('express').Router
|
|
|
|
const formidable = require('formidable')
|
2020-12-27 05:31:01 -05:00
|
|
|
const path = require('path')
|
|
|
|
const FileType = require('file-type')
|
2020-12-27 09:52:26 -05:00
|
|
|
const fs = require('fs')
|
|
|
|
const os = require('os')
|
|
|
|
const rimraf = require('rimraf')
|
2018-03-17 21:14:50 -04:00
|
|
|
|
|
|
|
const config = require('../../config')
|
|
|
|
const logger = require('../../logger')
|
2019-10-27 08:51:53 -04:00
|
|
|
const errors = require('../../errors')
|
2018-03-17 21:14:50 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const imageRouter = (module.exports = Router())
|
2018-03-17 21:14:50 -04:00
|
|
|
|
2020-12-27 05:31:01 -05:00
|
|
|
async function checkUploadType (filePath) {
|
|
|
|
const typeFromMagic = await FileType.fromFile(filePath)
|
|
|
|
if (typeFromMagic === undefined) {
|
2021-02-15 03:42:51 -05:00
|
|
|
logger.error('Image upload error: Could not determine MIME-type')
|
2020-12-27 05:31:01 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (path.extname(filePath) !== '.' + typeFromMagic.ext) {
|
2021-02-15 03:42:51 -05:00
|
|
|
logger.error(
|
|
|
|
'Image upload error: Provided file extension does not match MIME-type'
|
|
|
|
)
|
2020-12-27 05:31:01 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (!config.allowedUploadMimeTypes.includes(typeFromMagic.mime)) {
|
2021-02-15 03:42:51 -05:00
|
|
|
logger.error(
|
|
|
|
`Image upload error: MIME-type "${
|
|
|
|
typeFromMagic.mime
|
|
|
|
}" of uploaded file not allowed, only "${config.allowedUploadMimeTypes.join(
|
|
|
|
', '
|
|
|
|
)}" are allowed`
|
|
|
|
)
|
2020-12-27 05:31:01 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-03-17 21:14:50 -04:00
|
|
|
// upload image
|
|
|
|
imageRouter.post('/uploadimage', function (req, res) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
!req.isAuthenticated() &&
|
|
|
|
!config.allowAnonymous &&
|
|
|
|
!config.allowAnonymousEdits
|
|
|
|
) {
|
|
|
|
logger.error(
|
|
|
|
'Image upload error: Anonymous edits and therefore uploads are not allowed'
|
|
|
|
)
|
2020-12-27 09:52:26 -05:00
|
|
|
return errors.errorForbidden(res)
|
|
|
|
}
|
2018-03-17 21:14:50 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const form = new formidable.IncomingForm()
|
2018-03-17 21:14:50 -04:00
|
|
|
form.keepExtensions = true
|
2020-12-27 09:52:26 -05:00
|
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'hedgedoc-'))
|
|
|
|
form.uploadDir = tmpDir
|
2018-03-17 21:14:50 -04:00
|
|
|
|
2020-12-27 05:31:01 -05:00
|
|
|
form.parse(req, async function (err, fields, files) {
|
2020-11-23 06:42:19 -05:00
|
|
|
if (err) {
|
2020-11-23 07:59:50 -05:00
|
|
|
logger.error(`Image upload error: formidable error: ${err}`)
|
2020-12-27 09:52:26 -05:00
|
|
|
rimraf(tmpDir)
|
2020-11-23 06:50:39 -05:00
|
|
|
return errors.errorForbidden(res)
|
2020-11-23 06:42:19 -05:00
|
|
|
} else if (!files.image || !files.image.path) {
|
2021-02-15 03:42:51 -05:00
|
|
|
logger.error("Image upload error: Upload didn't contain file)")
|
2020-12-27 09:52:26 -05:00
|
|
|
rimraf.sync(tmpDir)
|
2020-11-23 06:42:19 -05:00
|
|
|
return errors.errorBadRequest(res)
|
2021-02-15 03:42:51 -05:00
|
|
|
} else if (!(await checkUploadType(files.image.path))) {
|
2020-12-27 09:52:26 -05:00
|
|
|
rimraf.sync(tmpDir)
|
2020-11-23 06:42:19 -05:00
|
|
|
return errors.errorBadRequest(res)
|
2018-03-17 21:14:50 -04:00
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
logger.debug(
|
|
|
|
`SERVER received uploadimage: ${JSON.stringify(files.image)}`
|
|
|
|
)
|
2018-03-17 21:14:50 -04:00
|
|
|
|
2018-03-07 09:17:35 -05:00
|
|
|
const uploadProvider = require('./' + config.imageUploadType)
|
2021-02-15 03:42:51 -05:00
|
|
|
logger.debug(
|
|
|
|
`imageRouter: Uploading ${files.image.path} using ${config.imageUploadType}`
|
|
|
|
)
|
2018-03-17 21:14:50 -04:00
|
|
|
uploadProvider.uploadImage(files.image.path, function (err, url) {
|
2020-12-27 09:52:26 -05:00
|
|
|
rimraf.sync(tmpDir)
|
2018-03-17 21:14:50 -04:00
|
|
|
if (err !== null) {
|
|
|
|
logger.error(err)
|
|
|
|
return res.status(500).end('upload image error')
|
|
|
|
}
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug(`SERVER sending ${url} to client`)
|
2018-03-17 21:14:50 -04:00
|
|
|
res.send({
|
|
|
|
link: url
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|