import fs from 'node:fs'; import path from 'node:path'; import crypto from 'node:crypto'; import multer from 'multer'; import { env } from '../config/env.js'; const ALLOWED_MIME = new Set(['image/png', 'image/jpeg', 'image/webp', 'image/svg+xml']); export const logoDir = path.join(env.storageRoot, 'branding'); fs.mkdirSync(logoDir, { recursive: true }); const storage = multer.diskStorage({ destination: (req, file, cb) => cb(null, logoDir), // Nombre generado por el servidor (evita colisiones/path traversal) y sirve de version para // el cache-busting del logo publico (cambia en cada subida). filename: (req, file, cb) => { const ext = path.extname(file.originalname).slice(0, 10); cb(null, `logo-${Date.now()}-${crypto.randomBytes(6).toString('hex')}${ext}`); }, }); export const uploadLogo = multer({ storage, limits: { fileSize: 3 * 1024 * 1024 }, fileFilter: (req, file, cb) => { if (!ALLOWED_MIME.has(file.mimetype)) { return cb(new Error('Tipo de archivo no permitido (usa PNG, JPG, WEBP o SVG)')); } cb(null, true); }, });