import { Controller, All, Req, Res, Get, Post, Patch, Param, Delete, Head, Options, NotFoundException, HttpException, HttpStatus, } from '@nestjs/common'; import { Request, Response } from 'express'; import { TusService } from './tus.service'; import { ShareCodeService } from './share-code.service'; @Controller('upload') export class UploadController { constructor( private readonly tusService: TusService, private readonly shareCodeService: ShareCodeService, ) {} // @Post() // async handlePost(@Req() req: Request, @Res() res: Response) { // return this.tusService.handleTus(req, res); // } @Options() async handleOptions(@Req() req: Request, @Res() res: Response) { return this.tusService.handleTus(req, res); } @Head() async handleHead(@Req() req: Request, @Res() res: Response) { return this.tusService.handleTus(req, res); } @Post() async handlePost(@Req() req: Request, @Res() res: Response) { return this.tusService.handleTus(req, res); } @Get('/*') async handleGet(@Req() req: Request, @Res() res: Response) { return this.tusService.handleTus(req, res); } @Patch('/*') async handlePatch(@Req() req: Request, @Res() res: Response) { return this.tusService.handleTus(req, res); } // Keeping the catch-all method as a fallback @All() async handleUpload(@Req() req: Request, @Res() res: Response) { return this.tusService.handleTus(req, res); } @Post('share/:fileId(*)') async generateShareCode(@Param('fileId') fileId: string) { try { console.log('收到生成分享码请求,fileId:', fileId); const result = await this.shareCodeService.generateShareCode(fileId); console.log('生成分享码结果:', result); return result; } catch (error) { console.error('生成分享码错误:', error); throw new HttpException( { message: (error as Error).message || '生成分享码失败', error: 'SHARE_CODE_GENERATION_FAILED' }, HttpStatus.INTERNAL_SERVER_ERROR ); } } @Get('share/:code') async validateShareCode(@Param('code') code: string) { const fileId = await this.shareCodeService.validateAndUseCode(code); if (!fileId) { throw new NotFoundException('分享码无效或已过期'); } return { fileId }; } @Get('share/info/:code') async getShareCodeInfo(@Param('code') code: string) { const info = await this.shareCodeService.getShareCodeInfo(code); if (!info) { throw new NotFoundException('分享码不存在'); } return info; } @Get('share/history/:fileId') async getFileShareHistory(@Param('fileId') fileId: string) { return this.shareCodeService.getFileShareHistory(fileId); } }