36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { Router } from 'express';
|
|
import multer from 'multer';
|
|
import path from 'path';
|
|
import crypto from 'crypto';
|
|
import { authenticateToken } from '../middlewares/auth.middleware';
|
|
import { asyncHandler } from '../middlewares/errorHandler.middleware';
|
|
import { DocumentController } from '../controllers/document.controller';
|
|
import { ensureUploadDir, UPLOAD_DIR } from '../config/storage';
|
|
|
|
ensureUploadDir();
|
|
|
|
const storage = multer.diskStorage({
|
|
destination: (_req, _file, cb) => cb(null, UPLOAD_DIR),
|
|
filename: (_req, file, cb) => {
|
|
const safeBase = path.basename(file.originalname).replace(/[^a-zA-Z0-9._-]/g, '_');
|
|
const hash = crypto.randomBytes(6).toString('hex');
|
|
const name = `${Date.now()}-${hash}-${safeBase}`;
|
|
cb(null, name);
|
|
}
|
|
});
|
|
|
|
const upload = multer({
|
|
storage,
|
|
limits: { fileSize: 100 * 1024 * 1024 }, // 100MB - actual limit enforced by controller using database config
|
|
});
|
|
|
|
const router = Router();
|
|
const controller = new DocumentController();
|
|
|
|
// multipart/form-data: file, requestId, optional category
|
|
router.post('/', authenticateToken, upload.single('file'), asyncHandler(controller.upload.bind(controller)));
|
|
|
|
export default router;
|
|
|
|
|