import { Injectable, Logger } from '@nestjs/common'; import * as fs from 'fs'; import * as path from 'path'; import { randomUUID } from 'crypto'; const UPLOAD_DIR = path.resolve(process.cwd(), 'uploads', 'media'); const MIME_TO_EXT: Record = { 'image/jpeg': '.jpg', 'image/png': '.png', 'image/webp': '.webp', 'audio/ogg': '.ogg', 'audio/mpeg': '.mp3', 'audio/mp4': '.m4a', 'audio/amr': '.amr', 'video/mp4': '.mp4', 'video/3gpp': '.3gp', 'application/pdf': '.pdf', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': '.docx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': '.xlsx', 'application/vnd.openxmlformats-officedocument.presentationml.presentation': '.pptx', }; @Injectable() export class LocalDiskAdapter { private readonly logger = new Logger(LocalDiskAdapter.name); constructor() { fs.mkdirSync(UPLOAD_DIR, { recursive: true }); } async upload(buffer: Buffer, mimeType: string): Promise { const ext = MIME_TO_EXT[mimeType] ?? '.bin'; const filename = `${randomUUID()}${ext}`; const filepath = path.join(UPLOAD_DIR, filename); fs.writeFileSync(filepath, buffer); this.logger.log(`Mídia salva: ${filepath}`); return `/uploads/media/${filename}`; } delete(relativePath: string): void { try { fs.unlinkSync(path.join(process.cwd(), relativePath)); } catch { this.logger.warn(`Falha ao deletar mídia: ${relativePath}`); } } }