refactor: Extract shared multer/upload config from routes

Create src/config/upload.js with createUpload() factory and shared
ALLOWED_FILE_TYPES constant. Replace duplicated multer configs in 5
route files with calls to the shared factory.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
richardtekula
2026-01-28 07:21:35 +01:00
parent 01ce2fc7ad
commit f463467264
6 changed files with 86 additions and 112 deletions

View File

@@ -1,37 +1,21 @@
import express from 'express';
import multer from 'multer';
import path from 'path';
import { fileURLToPath } from 'url';
import * as timesheetController from '../controllers/timesheet.controller.js';
import { authenticate } from '../middlewares/auth/authMiddleware.js';
import { requireAdmin } from '../middlewares/auth/roleMiddleware.js';
import { validateBody, validateParams } from '../middlewares/security/validateInput.js';
import { z } from 'zod';
import fs from 'fs/promises';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
import { createUpload } from '../config/upload.js';
const router = express.Router();
// Create uploads directory if it doesn't exist
const uploadsDir = path.join(process.cwd(), 'uploads', 'timesheets');
await fs.mkdir(uploadsDir, { recursive: true });
// Configure multer for file uploads - use memory storage first
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024, // 5MB limit
},
fileFilter: (req, file, cb) => {
const allowedTypes = ['application/pdf', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel'];
if (allowedTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error('Neplatný typ súboru. Povolené sú iba PDF a Excel súbory.'), false);
}
}
const upload = createUpload({
maxSizeMB: 5,
allowedTypes: [
'application/pdf',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.ms-excel',
],
errorMessage: 'Neplatný typ súboru. Povolené sú iba PDF a Excel súbory.',
});
/**