File size: 2,704 Bytes
ef99e8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d201b18
ef99e8a
 
 
d201b18
ef99e8a
6f84fbe
ef99e8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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,
    };
  }
}