import { BadRequestException, Controller, Delete, Get, HttpCode, Param, ParseUUIDPipe, Post, Res, UploadedFile, UseInterceptors, } from '@nestjs/common'; import { Throttle } from '@nestjs/throttler'; import { FileInterceptor } from '@nestjs/platform-express'; import { diskStorage } from 'multer'; import { Response } from 'express'; import path from 'node:path'; import fs from 'node:fs'; import { v4 as uuidv4 } from 'uuid'; import { DocumentsService } from '../application/documents.service'; import { PPTX_MIME } from '../application/documents.service'; import { CurrentUser } from '../auth/auth.decorators'; import { CONVERTIBLE_EXTENSIONS } from '../domain/models'; import type { PublicUser } from '../domain/models'; // The acting user's id, or null when the auth gate is disabled (single-user). const ownerOf = (u: PublicUser | undefined): string | null => u?.id ?? null; const UPLOAD_DIR = path.join(process.cwd(), 'uploads'); const storage = diskStorage({ destination: (_req, _file, cb) => { if (!fs.existsSync(UPLOAD_DIR)) fs.mkdirSync(UPLOAD_DIR, { recursive: true }); cb(null, UPLOAD_DIR); }, filename: (_req, file, cb) => cb(null, `${uuidv4()}${path.extname(file.originalname) || '.pdf'}`), }); const ACCEPTED_MIMES = new Set(['application/pdf', PPTX_MIME]); // PDF/PPTX have native pipelines; everything else goes through pandoc. const ACCEPTED_EXTS = new Set(['.pdf', '.pptx', ...CONVERTIBLE_EXTENSIONS]); @Controller('api/documents') export class DocumentsController { constructor(private readonly service: DocumentsService) {} @Throttle({ default: { ttl: 60_000, limit: 60 } }) @Post('upload') @UseInterceptors( FileInterceptor('file', { storage, limits: { fileSize: (parseInt(process.env.MAX_UPLOAD_MB || '50', 10) || 50) * 1024 * 1024 }, fileFilter: (_req, file, cb) => { const ext = path.extname(file.originalname).toLowerCase(); if (ACCEPTED_MIMES.has(file.mimetype) || ACCEPTED_EXTS.has(ext)) return cb(null, true); cb( new BadRequestException( `Unsupported file type. Accepted: PDF, PPTX and pandoc-convertible documents (${[...CONVERTIBLE_EXTENSIONS].join(', ')})`, ), false, ); }, }), ) upload(@UploadedFile() file: Express.Multer.File | undefined, @CurrentUser() user?: PublicUser) { return this.service.upload(file, ownerOf(user)); } @Get(':id') meta(@Param('id', ParseUUIDPipe) id: string, @CurrentUser() user?: PublicUser) { return this.service.getSummary(id, ownerOf(user)); } @Get(':id/file') async file( @Param('id', ParseUUIDPipe) id: string, @Res() res: Response, @CurrentUser() user?: PublicUser, ) { const { filepath, contentType } = await this.service.resolveFile(id, ownerOf(user)); res.setHeader('Content-Type', contentType); res.sendFile(filepath); } @Throttle({ default: { ttl: 60_000, limit: 30 } }) @Post(':id/ocr') @HttpCode(200) reocr(@Param('id', ParseUUIDPipe) id: string, @CurrentUser() user?: PublicUser) { return this.service.reocr(id, ownerOf(user)); } @Delete(':id') @HttpCode(204) delete(@Param('id', ParseUUIDPipe) id: string, @CurrentUser() user?: PublicUser) { return this.service.delete(id, ownerOf(user)); } }