hedgedoc/lib/web/imageRouter/filesystem.js
Sheogorath c025d6abee
fix(image-upload): Fix swallowing of errors for filesystem
This patch fixes the swollowing of the actual error message that appears
when a file fails to move, after being uploaded to Hedgedoc on an
instance that is using the upload-method `filesystem` active.

This became apparent when the error messages provided by some users,
where less than helpful.

As a solution the error message of the copy command was added to the
error that is output to the console.

https://community.hedgedoc.org/t/image-upload-fail-docker/439

Signed-off-by: Sheogorath <sheogorath@shivering-isles.com>
2021-08-14 20:04:08 +02:00

29 lines
844 B
JavaScript

'use strict'
const URL = require('url').URL
const path = require('path')
const fs = require('fs')
const config = require('../../config')
const logger = require('../../logger')
exports.uploadImage = function (imagePath, callback) {
if (!callback || typeof callback !== 'function') {
logger.error('Callback has to be a function')
return
}
if (!imagePath || typeof imagePath !== 'string') {
callback(new Error('Image path is missing or wrong'), null)
return
}
const fileName = path.basename(imagePath)
// move image from temporary path to upload directory
try {
fs.copyFileSync(imagePath, path.join(config.uploadsPath, fileName))
} catch (e) {
callback(new Error(`Error while moving file: ${e.message}`), null)
return
}
callback(null, (new URL(fileName, config.serverURL + '/uploads/')).href)
}