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

111 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-01-06 08:45:23 +08:00
import {
Controller,
2025-01-06 18:30:16 +08:00
All,
Req,
Res,
Get,
2025-01-06 08:45:23 +08:00
Post,
2025-01-06 18:30:16 +08:00
Patch,
2025-01-06 08:45:23 +08:00
Param,
2025-01-06 18:30:16 +08:00
Delete,
2025-01-06 08:45:23 +08:00
} from '@nestjs/common';
2025-01-06 18:30:16 +08:00
import { Request, Response } from "express"
import { TusService } from './tus.service';
2025-01-03 09:24:46 +08:00
2025-01-06 08:45:23 +08:00
@Controller('upload')
2025-01-03 09:24:46 +08:00
export class UploadController {
2025-01-06 18:30:16 +08:00
constructor(private readonly tusService: TusService) { }
@Post()
async handlePost(@Req() req: Request, @Res() res: Response) {
return this.tusService.handleTus(req, res);
2025-01-06 08:45:23 +08:00
}
2025-01-06 18:30:16 +08:00
@Patch(':fileId') // 添加文件ID参数
async handlePatch(
@Req() req: Request,
@Res() res: Response,
@Param('fileId') fileId: string // 添加文件ID参数
) {
try {
// 添加错误处理和日志
const result = await this.tusService.handleTus(req, res);
return result;
} catch (error: any) {
console.error('Upload PATCH error:', error);
res.status(500).json({
message: 'Upload failed',
error: error.message
});
}
2025-01-06 08:45:23 +08:00
}
2025-01-06 18:30:16 +08:00
@Delete(':fileId')
async handleDelete(
@Req() req: Request,
@Res() res: Response,
@Param('fileId') fileId: string
2025-01-06 08:45:23 +08:00
) {
2025-01-06 18:30:16 +08:00
try {
const result = await this.tusService.handleTus(req, res);
return result;
} catch (error: any) {
console.error('Upload DELETE error:', error);
res.status(500).json({
message: 'Delete failed',
error: error.message
});
}
2025-01-06 08:45:23 +08:00
}
2025-01-06 18:30:16 +08:00
@Get(':fileId')
async handleGet(
@Req() req: Request,
@Res() res: Response,
@Param('fileId') fileId: string
2025-01-06 08:45:23 +08:00
) {
2025-01-06 18:30:16 +08:00
try {
const result = await this.tusService.handleTus(req, res);
return result;
} catch (error: any) {
console.error('Upload GET error:', error);
res.status(500).json({
message: 'Retrieve failed',
error: error.message
});
2025-01-03 09:24:46 +08:00
}
}
2025-01-06 18:30:16 +08:00
// @Post('chunk')
// @UseInterceptors(FileInterceptor('file'))
// async uploadChunk(
// @Body('chunk') chunkString: string, // 改为接收字符串
// @UploadedFile() file: Express.Multer.File,
// @Body('clientId') clientId: string
// ) {
// const chunk = JSON.parse(chunkString); // 解析字符串为对象
// await this.uploadService.uploadChunk(chunk, file, clientId);
// return { message: 'Chunk uploaded successfully' };
// }
// @Get('status/:identifier')
// checkUploadStatusInfo(@Param('identifier') identifier: string) {
// const status = this.uploadService.checkUploadStatusInfo(identifier);
// return status || { message: 'No upload status found' };
// }
// @Post('pause/:identifier')
// pauseUpload(
// @Param('identifier') identifier: string,
// @Body('clientId') clientId: string
// ) {
// this.uploadService.pauseUpload(identifier, clientId);
// return { message: 'Upload paused successfully' };
// }
// @Post('resume/:identifier')
// async resumeUpload(
// @Param('identifier') identifier: string,
// @Body('clientId') clientId: string
// ) {
// const resumed = this.uploadService.resumeUpload(identifier, clientId);
// if (!resumed) {
// throw new Error('Unable to resume upload');
// }
// return { message: 'Upload resumed successfully' };
// }
2025-01-03 09:24:46 +08:00
}