Commit c7e4ad25 authored by mingyard's avatar mingyard

feat:下载翻译后的图片

parent 24b04bda
......@@ -4,6 +4,7 @@ import {
Get,
Post,
Query,
Res,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
......@@ -16,6 +17,7 @@ import { FileInterceptor } from '@nestjs/platform-express';
import { TranslateImageReqDto } from './dto/req/translateImageReq.dto';
import { TranslateProgressReqDto } from './dto/req/translateProgressReq.dto';
import { ApiResponseInterceptor } from '@/common/interceptor/api.response.interceptor';
import { Response } from 'express';
@ApiTags('translate')
@UseInterceptors(ApiResponseInterceptor)
......@@ -146,16 +148,18 @@ export class TranslateController {
return await this.translateService.getProgress(dto.taskId);
}
// 获取翻译结果
@Get('result')
@ApiOperation({ summary: '获取翻译结果' })
// 下载翻译结果
@Get('downloadImage')
@ApiOperation({ summary: '下载翻译结果' })
@ApiResponse({
status: 200,
description: '成功返回翻译结果',
example: {},
})
@Auth()
async getResult(@Query() dto: TranslateProgressReqDto): Promise<any> {
return await this.translateService.downloadImage(dto.taskId);
async getResult(
@Query() dto: TranslateProgressReqDto,
@Res() res: Response,
): Promise<any> {
return await this.translateService.downloadImage(dto.taskId, res);
}
}
......@@ -11,6 +11,8 @@ import { BadRequestError } from '@/common/exception/badRequest/BadRequestError';
import * as crypto from 'crypto';
import * as FormData from 'form-data';
import { TranslateImageReqDto } from './dto/req/translateImageReq.dto';
import { Response } from 'express';
import axios, { AxiosResponse } from 'axios';
@Injectable()
export class TranslateService {
......@@ -167,7 +169,7 @@ export class TranslateService {
}
// 下载翻译图片
async downloadImage(taskId: string): Promise<any> {
async downloadImage(taskId: string, res: Response): Promise<any> {
const params = {
nonce_str: crypto.randomUUID(),
tid: taskId,
......@@ -182,20 +184,26 @@ export class TranslateService {
formData.append('tid', params.tid);
formData.append('token', token);
const result = await axiosPostRequest(
const response: AxiosResponse = await axios.post(
`${config.service.fanYiGou.endpoint}/TranslateApi/api/image/downloadImage`,
formData,
{
headers: {
...formData.getHeaders(),
},
responseType: 'stream', // 设置 responseType 为 stream
},
);
if (!result?.data) {
if (response.status === 200 && !response?.data) {
throw BadRequestError.default('下载翻译图片失败');
}
return result?.data;
// 设置响应头
res.setHeader('Content-Type', 'image/jpeg'); // 根据实际文件类型设置
res.setHeader('Content-Disposition', 'attachment; filename="image.jpg"'); // 设置下载文件名
// 将文件流返回给前端
response.data.pipe(res);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment