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

111 lines
3.5 KiB
TypeScript

import {
Controller,
All,
Req,
Res,
Get,
Post,
Patch,
Param,
Delete,
} from '@nestjs/common';
import { Request, Response } from "express"
import { TusService } from './tus.service';
@Controller('upload')
export class UploadController {
constructor(private readonly tusService: TusService) { }
@Post()
async handlePost(@Req() req: Request, @Res() res: Response) {
return this.tusService.handleTus(req, res);
}
@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
});
}
}
@Delete(':fileId')
async handleDelete(
@Req() req: Request,
@Res() res: Response,
@Param('fileId') fileId: string
) {
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
});
}
}
@Get(':fileId')
async handleGet(
@Req() req: Request,
@Res() res: Response,
@Param('fileId') fileId: string
) {
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
});
}
}
// @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' };
// }
}