origin/apps/server/src/upload/upload.controller.ts

102 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-01-06 08:45:23 +08:00
import {
2025-01-27 22:43:31 +08:00
Controller,
All,
Req,
Res,
Get,
Post,
Patch,
Param,
Delete,
Head,
Options,
2025-03-21 10:55:44 +08:00
NotFoundException,
HttpException,
HttpStatus,
2025-01-06 08:45:23 +08:00
} from '@nestjs/common';
2025-01-27 22:43:31 +08:00
import { Request, Response } from 'express';
2025-01-06 18:30:16 +08:00
import { TusService } from './tus.service';
2025-03-21 10:55:44 +08:00
import { ShareCodeService } from './share-code.service';
2025-01-06 08:45:23 +08:00
@Controller('upload')
2025-01-03 09:24:46 +08:00
export class UploadController {
2025-03-21 10:55:44 +08:00
constructor(
private readonly tusService: TusService,
private readonly shareCodeService: ShareCodeService,
) {}
2025-01-27 22:43:31 +08:00
// @Post()
// async handlePost(@Req() req: Request, @Res() res: Response) {
// return this.tusService.handleTus(req, res);
// }
2025-01-08 00:52:11 +08:00
2025-01-27 22:43:31 +08:00
@Options()
async handleOptions(@Req() req: Request, @Res() res: Response) {
return this.tusService.handleTus(req, res);
}
2025-01-08 00:52:11 +08:00
2025-01-27 22:43:31 +08:00
@Head()
async handleHead(@Req() req: Request, @Res() res: Response) {
return this.tusService.handleTus(req, res);
}
2025-01-08 00:52:11 +08:00
2025-01-27 22:43:31 +08:00
@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);
}
2025-01-08 00:52:11 +08:00
2025-01-27 22:43:31 +08:00
@Patch('/*')
async handlePatch(@Req() req: Request, @Res() res: Response) {
return this.tusService.handleTus(req, res);
}
2025-01-06 08:45:23 +08:00
2025-01-27 22:43:31 +08:00
// Keeping the catch-all method as a fallback
@All()
async handleUpload(@Req() req: Request, @Res() res: Response) {
return this.tusService.handleTus(req, res);
}
2025-03-21 10:55:44 +08:00
@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);
}
2025-01-27 22:43:31 +08:00
}