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