import { Body, Controller, Get, HttpException, HttpStatus, Param, Post, UploadedFile, UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { ApiBearerAuth, ApiConsumes, ApiOperation, ApiTags } from '@nestjs/swagger'; import { CurrentUser, JwtPayload } from '../common/decorators/current-user.decorator'; import { WordCloudService } from './wordcloud.service'; @ApiTags('词云') @ApiBearerAuth() @Controller('wordcloud') export class WordCloudController { constructor(private readonly wordCloudService: WordCloudService) {} /** 创建词云任务:multipart(image) + form 字段 names/params */ @Post('generate') @ApiOperation({ summary: '创建词云任务(底图 + 名单文本)' }) @ApiConsumes('multipart/form-data') @UseInterceptors(FileInterceptor('image', { limits: { fileSize: 10 * 1024 * 1024 } })) async generate( @CurrentUser() user: JwtPayload, @UploadedFile() image: Express.Multer.File | undefined, @Body('names') names: string, @Body('params') params?: string, ) { if (!image) { throw new HttpException('缺少底图', HttpStatus.BAD_REQUEST); } const jobId = await this.wordCloudService.createGenerateJob( user.sub, { buffer: image.buffer, originalname: image.originalname }, names ?? '', params, ); return { jobId }; } /** 轮询词云任务(仅本人) */ @Get('jobs/:id') @ApiOperation({ summary: '轮询词云任务状态(仅本人)' }) async job(@CurrentUser() user: JwtPayload, @Param('id') id: string) { return this.wordCloudService.getUserJob(user.sub, id); } /** 取词云任务结果(仅本人;产物已转存 COS) */ @Get('jobs/:id/result') @ApiOperation({ summary: '词云任务结果(仅本人)' }) async result(@CurrentUser() user: JwtPayload, @Param('id') id: string) { return this.wordCloudService.getUserJobResult(user.sub, id); } }