File size: 2,802 Bytes
5d53367
 
 
 
 
 
 
036d801
5d53367
 
036d801
5d53367
 
d201b18
5d53367
 
 
d201b18
5d53367
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
036d801
01841a1
 
 
5d53367
036d801
 
 
 
 
 
5d53367
036d801
5d53367
036d801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d53367
 
 
 
 
 
 
 
 
 
 
 
 
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
import {
  Controller,
  Get,
  Delete,
  Query,
  HttpCode,
  HttpStatus,
  Res,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiQuery } from '@nestjs/swagger';
import type { Response } from 'express';
import { IdriveE2StorageService } from './idrive-e2-storage.service';
import { FileType } from './enums/file-type.enum';
import { LogToDiscord } from 'src/shared/decorators/log-to-discord.decorator';

@ApiTags('Upload: IDrive e2 Storage')
@Controller('idrive-e2')
@LogToDiscord()
export class IdriveE2StorageController {
  constructor(
    private readonly idriveE2StorageService: IdriveE2StorageService,
  ) {}

  @Get('presigned-upload')
  @ApiOperation({ summary: 'Get presigned upload URL' })
  @ApiQuery({ name: 'fileName', required: true })
  @ApiQuery({ name: 'fileType', enum: FileType, required: false })
  async getPresignedUploadUrl(
    @Query('fileName') fileName: string,
    @Query('fileType') fileType?: FileType,
  ) {
    return this.idriveE2StorageService.getPresignedUploadUrl(
      fileName,
      fileType,
    );
  }

  @Get('presigned-url')
  @ApiOperation({
    summary: 'Get presigned download URL (called by Cloudflare Worker)',
  })
  @ApiQuery({ name: 'filePath', required: true })
  async getPresignedUrl(@Query('filePath') filePath: string) {
    if (!filePath) {
      throw new Error('filePath is required');
    }

    const result = await this.idriveE2StorageService.getPresignedDownloadUrl(
      filePath,
      3600, // 1 hour
    );

    return {
      presignedUrl: result.downloadUrl,
      filePath: result.filePath,
      expiresIn: result.expiresIn,
    };
  }

  @Get('resize')
  @ApiOperation({ summary: 'Resize image (called by Cloudflare Worker)' })
  @ApiQuery({ name: 'path', required: true })
  @ApiQuery({ name: 'w', required: false })
  @ApiQuery({ name: 'h', required: false })
  @ApiQuery({ name: 'q', required: false })
  async resizeImage(
    @Res() res: Response,
    @Query('path') path: string,
    @Query('w') width?: string,
    @Query('h') height?: string,
    @Query('q') quality?: string,
  ) {
    const w = width ? parseInt(width, 10) : undefined;
    const h = height ? parseInt(height, 10) : undefined;
    const q = quality ? parseInt(quality, 10) : undefined;

    const { buffer, contentType } =
      await this.idriveE2StorageService.resizeImage(path, w, h, q);

    res.set({
      'Content-Type': contentType,
      'Cache-Control': 'public, max-age=31536000',
    });
    res.send(buffer);
  }

  @Delete('by-url')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ summary: 'Delete file by URL' })
  async deleteFileByUrl(@Query('url') url: string) {
    const result = await this.idriveE2StorageService.deleteFileByUrl(url);
    return {
      message: 'File deleted successfully',
      ...result,
    };
  }
}