Files
ontime/apps/server/src/utils/upload.js
T
Carlos Valente de9a7a87fd V2 monorepo (#285)
* refactor(project structure): UI

* refactor(project structure): extract utilities

* refactor(project structure): remove unused

* refactor(project structure): electron

* refactor(project structure): server

refactor: migrate to vitest

refactor: monorepo config

* refactor: extract application menu

* refactor: exit process

* refactor: extract tray menu

* chore: electron build

* Added Seconds in studio clock #282
---------

Co-authored-by: Fabian Posenau <fabian@fphome.de>

---------

Co-authored-by: Fabian Posenau <fabian.p99@gmx.de>
Co-authored-by: Fabian Posenau <fabian@fphome.de>
2023-02-14 22:02:15 +01:00

47 lines
1.3 KiB
JavaScript

import multer from 'multer';
import * as path from 'path';
import { EXCEL_MIME, JSON_MIME } from './parser.js';
import { ensureDirectory } from './fileManagement.js';
import { getAppDataPath } from '../setup.js';
// Define multer storage object
const storage = multer.diskStorage({
destination: function (req, file, cb) {
// get platform path
const appDataPath = getAppDataPath();
if (appDataPath === '') {
throw new Error('Could not resolve public folder for platform');
}
// append uploads folder
const newDestination = path.join(appDataPath, 'uploads');
// Create directory if not exist
ensureDirectory(newDestination);
cb(null, newDestination);
},
filename: function (req, file, cb) {
cb(null, `${Date.now()}--${file.originalname}`);
},
});
/**
* @description Middleware function to filter allowed file types
* @argument file - reference to file
* @return {boolean} - file allowed
*/
const filterAllowed = (req, file, cb) => {
if (file.mimetype.includes(JSON_MIME) || file.mimetype.includes(EXCEL_MIME)) {
cb(null, true);
} else {
console.log('ERROR: Unrecognised file type');
cb(null, false);
}
};
// Build multer uploader for a single file
export const uploadFile = multer({
storage: storage,
fileFilter: filterAllowed,
}).single('userFile');