fix: upload in electron

- fixed issue where upload would fail if folder did not exist
This commit is contained in:
cv
2021-10-28 14:43:34 +02:00
parent b08bc64bd4
commit c8e4704daf
+16 -1
View File
@@ -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);