import { Controller, Get, Post, Param, Res, NotFoundException, UseInterceptors, UploadedFile, Body } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { Response } from 'express'; import { StorageService } from '@devstroupe/devkit-nest'; @Controller('storage') export class StorageController { constructor( private readonly storageService: StorageService, ) {} @Post('upload') @UseInterceptors(FileInterceptor('file')) async upload( @UploadedFile() file: any, @Body('path') path?: string, ) { if (!file) { throw new NotFoundException('Nenhum arquivo enviado.'); } const result = await this.storageService.upload( { originalname: file.originalname, mimetype: file.mimetype, buffer: file.buffer, size: file.size, }, { path } ); return result; } @Get('download/*key') async download(@Param('key') key: string, @Res() res: Response) { try { const result = await this.storageService.download(key); if (result.type === 'file') { return res.sendFile(result.filePath!); } else { return res.redirect(result.url!); } } catch (err: any) { throw new NotFoundException(err.message || 'Arquivo não encontrado.'); } } }