import { Controller, Post, Delete, Get, Query, Body, UseInterceptors, UploadedFile, HttpCode, HttpStatus, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { ApiTags, ApiOperation, ApiConsumes, ApiQuery, ApiBody, } from '@nestjs/swagger'; import { DropboxStorageService } from './dropbox-storage.service'; import { FileType } from './enums/file-type.enum'; import { LogToDiscord } from 'src/shared/decorators/log-to-discord.decorator'; @ApiTags('Dropbox Storage') @Controller('dropbox') @LogToDiscord() export class DropboxStorageController { constructor(private readonly dropboxStorageService: DropboxStorageService) {} @Post('upload') @ApiOperation({ summary: 'Upload file to Dropbox' }) @ApiConsumes('multipart/form-data') @ApiQuery({ name: 'fileType', enum: FileType, required: false }) @ApiQuery({ name: 'customFilename', required: false, description: 'Custom filename (optional)', }) @ApiBody({ schema: { type: 'object', properties: { file: { type: 'string', format: 'binary', }, }, }, }) @UseInterceptors(FileInterceptor('file')) async uploadFile( @UploadedFile() file: any, @Query('fileType') fileType?: FileType, @Query('customFilename') customFilename?: string, ) { return this.dropboxStorageService.uploadFile({ fileBuffer: file.buffer, filename: file.originalname, fileType: fileType || 'IMAGE', customFilename: customFilename, }); } @Get('file-info') @ApiOperation({ summary: 'Get file info and shareable link by path' }) @ApiQuery({ name: 'path', required: true }) async getFileInfo(@Query('path') path: string) { return this.dropboxStorageService.getFileInfo(path); } @Get('list') @ApiOperation({ summary: 'List files in folder' }) @ApiQuery({ name: 'folderPath', required: false }) async listFiles(@Query('folderPath') folderPath?: string) { return this.dropboxStorageService.listFiles(folderPath); } @Delete('by-path') @HttpCode(HttpStatus.OK) @ApiOperation({ summary: 'Delete file by Dropbox path' }) async deleteFileByPath(@Body('path') path: string) { const result = await this.dropboxStorageService.deleteFile(path); return { message: 'File deleted successfully', ...result, }; } @Delete('by-url') @HttpCode(HttpStatus.OK) @ApiOperation({ summary: 'Delete file by public URL' }) async deleteFileByUrl(@Query('url') url: string) { const result = await this.dropboxStorageService.deleteFileByUrl(url); return { message: 'File deleted successfully', ...result, }; } }