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

54 lines
1.7 KiB
TypeScript
Raw Normal View History

2025-01-06 08:45:23 +08:00
import {
Controller,
Post,
UseInterceptors,
UploadedFile,
Body,
Param,
Get,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { UploadService } from './upload.service';
import { ChunkDto } from '@nice/common';
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 {
constructor(private readonly uploadService: UploadService) { }
2025-01-06 08:45:23 +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');
2025-01-03 09:24:46 +08:00
}
2025-01-06 08:45:23 +08:00
return { message: 'Upload resumed successfully' };
2025-01-03 09:24:46 +08:00
}
}