Prepojenie na externu DB, projekt-zakazky

This commit is contained in:
2026-02-03 11:20:17 +01:00
parent e4f63a135e
commit cbdd952bc1
37 changed files with 2641 additions and 149 deletions

View File

@@ -0,0 +1,164 @@
import multer from 'multer';
import path from 'path';
import { Request } from 'express';
import { env } from '../config/env';
// Povolené MIME typy
const ALLOWED_MIME_TYPES = [
// Obrázky
'image/jpeg',
'image/png',
'image/gif',
'image/webp',
// Dokumenty
'application/pdf',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'text/plain',
'text/csv',
];
// Povolené prípony
const ALLOWED_EXTENSIONS = [
'.jpg', '.jpeg', '.png', '.gif', '.webp',
'.pdf', '.doc', '.docx', '.xls', '.xlsx',
'.txt', '.csv',
];
// Maximálna veľkosť súboru (default 10MB)
const MAX_FILE_SIZE = parseInt(process.env.MAX_FILE_SIZE || '10485760', 10);
// Storage konfigurácia
const storage = multer.diskStorage({
destination: (req: Request, _file, cb) => {
// Určenie cieľového priečinka podľa entity type
const entityType = req.params.entityType || req.body.entityType || 'general';
const uploadPath = path.join(env.UPLOAD_DIR || 'uploads', entityType);
cb(null, uploadPath);
},
filename: (_req, file, cb) => {
// Generovanie unikátneho názvu: timestamp-random.extension
const uniqueSuffix = `${Date.now()}-${Math.round(Math.random() * 1e9)}`;
const ext = path.extname(file.originalname).toLowerCase();
cb(null, `${uniqueSuffix}${ext}`);
},
});
// File filter
const fileFilter = (
_req: Request,
file: Express.Multer.File,
cb: multer.FileFilterCallback
) => {
const ext = path.extname(file.originalname).toLowerCase();
// Kontrola MIME typu
if (!ALLOWED_MIME_TYPES.includes(file.mimetype)) {
cb(new Error(`Nepodporovaný typ súboru: ${file.mimetype}`));
return;
}
// Kontrola prípony
if (!ALLOWED_EXTENSIONS.includes(ext)) {
cb(new Error(`Nepodporovaná prípona súboru: ${ext}`));
return;
}
cb(null, true);
};
// Základná multer inštancia
export const upload = multer({
storage,
fileFilter,
limits: {
fileSize: MAX_FILE_SIZE,
files: 10, // Max počet súborov naraz
},
});
// Pre-configured uploaders pre rôzne entity
export const equipmentUpload = multer({
storage: multer.diskStorage({
destination: (_req, _file, cb) => {
cb(null, path.join(env.UPLOAD_DIR || 'uploads', 'equipment'));
},
filename: (_req, file, cb) => {
const uniqueSuffix = `${Date.now()}-${Math.round(Math.random() * 1e9)}`;
const ext = path.extname(file.originalname).toLowerCase();
cb(null, `${uniqueSuffix}${ext}`);
},
}),
fileFilter,
limits: {
fileSize: MAX_FILE_SIZE,
files: 10,
},
});
export const rmaUpload = multer({
storage: multer.diskStorage({
destination: (_req, _file, cb) => {
cb(null, path.join(env.UPLOAD_DIR || 'uploads', 'rma'));
},
filename: (_req, file, cb) => {
const uniqueSuffix = `${Date.now()}-${Math.round(Math.random() * 1e9)}`;
const ext = path.extname(file.originalname).toLowerCase();
cb(null, `${uniqueSuffix}${ext}`);
},
}),
fileFilter,
limits: {
fileSize: MAX_FILE_SIZE,
files: 10,
},
});
// Pending upload - for files before entity is created
export const pendingUpload = multer({
storage: multer.diskStorage({
destination: (_req, _file, cb) => {
cb(null, path.join(env.UPLOAD_DIR || 'uploads', 'pending'));
},
filename: (_req, file, cb) => {
const uniqueSuffix = `${Date.now()}-${Math.round(Math.random() * 1e9)}`;
const ext = path.extname(file.originalname).toLowerCase();
cb(null, `${uniqueSuffix}${ext}`);
},
}),
fileFilter,
limits: {
fileSize: MAX_FILE_SIZE,
files: 10,
},
});
export const taskUpload = multer({
storage: multer.diskStorage({
destination: (_req, _file, cb) => {
cb(null, path.join(env.UPLOAD_DIR || 'uploads', 'task'));
},
filename: (_req, file, cb) => {
const uniqueSuffix = `${Date.now()}-${Math.round(Math.random() * 1e9)}`;
const ext = path.extname(file.originalname).toLowerCase();
cb(null, `${uniqueSuffix}${ext}`);
},
}),
fileFilter,
limits: {
fileSize: MAX_FILE_SIZE,
files: 10,
},
});
// Helper pre získanie URL súboru
export const getFileUrl = (entityType: string, filename: string): string => {
return `/uploads/${entityType}/${filename}`;
};
// Helper pre získanie cesty súboru
export const getFilePath = (entityType: string, filename: string): string => {
return path.join(env.UPLOAD_DIR || 'uploads', entityType, filename);
};