Spaces:
Runtime error
Runtime error
| 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'; | |
| ('Upload: IDrive e2 Storage') | |
| ('idrive-e2') | |
| () | |
| export class IdriveE2StorageController { | |
| constructor( | |
| private readonly idriveE2StorageService: IdriveE2StorageService, | |
| ) {} | |
| ('presigned-upload') | |
| ({ summary: 'Get presigned upload URL' }) | |
| ({ name: 'fileName', required: true }) | |
| ({ name: 'fileType', enum: FileType, required: false }) | |
| async getPresignedUploadUrl( | |
| ('fileName') fileName: string, | |
| ('fileType') fileType?: FileType, | |
| ) { | |
| return this.idriveE2StorageService.getPresignedUploadUrl( | |
| fileName, | |
| fileType, | |
| ); | |
| } | |
| ('presigned-url') | |
| ({ | |
| summary: 'Get presigned download URL (called by Cloudflare Worker)', | |
| }) | |
| ({ name: 'filePath', required: true }) | |
| async getPresignedUrl(('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, | |
| }; | |
| } | |
| ('resize') | |
| ({ summary: 'Resize image (called by Cloudflare Worker)' }) | |
| ({ name: 'path', required: true }) | |
| ({ name: 'w', required: false }) | |
| ({ name: 'h', required: false }) | |
| ({ name: 'q', required: false }) | |
| async resizeImage( | |
| () res: Response, | |
| ('path') path: string, | |
| ('w') width?: string, | |
| ('h') height?: string, | |
| ('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); | |
| } | |
| ('by-url') | |
| (HttpStatus.OK) | |
| ({ summary: 'Delete file by URL' }) | |
| async deleteFileByUrl(('url') url: string) { | |
| const result = await this.idriveE2StorageService.deleteFileByUrl(url); | |
| return { | |
| message: 'File deleted successfully', | |
| ...result, | |
| }; | |
| } | |
| } | |