From c8e4704daf90e87294f969173c50339ee33baf9e Mon Sep 17 00:00:00 2001 From: cv <34649812+cpvalente@users.noreply.github.com> Date: Thu, 28 Oct 2021 14:43:34 +0200 Subject: [PATCH] fix: upload in electron - fixed issue where upload would fail if folder did not exist --- server/src/utils/upload.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/server/src/utils/upload.js b/server/src/utils/upload.js index e489ac26f..d58703050 100644 --- a/server/src/utils/upload.js +++ b/server/src/utils/upload.js @@ -1,8 +1,23 @@ import multer from 'multer'; +import { statSync, mkdirSync } from 'fs'; const storage = multer.diskStorage({ destination: function (req, file, cb) { - cb(null, 'uploads/'); + let newDestination = 'uploads/'; + let stat = null; + try { + stat = statSync(newDestination); + } catch (err) { + mkdirSync(newDestination); + } + if (stat && !stat.isDirectory()) { + throw new Error( + 'Directory cannot be created because an inode of a different type exists at "' + + dest + + '"' + ); + } + cb(null, newDestination); }, filename: function (req, file, cb) { cb(null, Date.now() + '--' + file.originalname);