Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
translation-server
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
qinmingyuan
translation-server
Commits
c7e4ad25
Commit
c7e4ad25
authored
Jan 15, 2025
by
mingyard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:下载翻译后的图片
parent
24b04bda
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
10 deletions
+22
-10
translate.controller.ts
src/controller/translate/translate.controller.ts
+10
-6
translate.service.ts
src/controller/translate/translate.service.ts
+12
-4
No files found.
src/controller/translate/translate.controller.ts
View file @
c7e4ad25
...
@@ -4,6 +4,7 @@ import {
...
@@ -4,6 +4,7 @@ import {
Get
,
Get
,
Post
,
Post
,
Query
,
Query
,
Res
,
UploadedFile
,
UploadedFile
,
UseInterceptors
,
UseInterceptors
,
}
from
'@nestjs/common'
;
}
from
'@nestjs/common'
;
...
@@ -16,6 +17,7 @@ import { FileInterceptor } from '@nestjs/platform-express';
...
@@ -16,6 +17,7 @@ import { FileInterceptor } from '@nestjs/platform-express';
import
{
TranslateImageReqDto
}
from
'./dto/req/translateImageReq.dto'
;
import
{
TranslateImageReqDto
}
from
'./dto/req/translateImageReq.dto'
;
import
{
TranslateProgressReqDto
}
from
'./dto/req/translateProgressReq.dto'
;
import
{
TranslateProgressReqDto
}
from
'./dto/req/translateProgressReq.dto'
;
import
{
ApiResponseInterceptor
}
from
'@/common/interceptor/api.response.interceptor'
;
import
{
ApiResponseInterceptor
}
from
'@/common/interceptor/api.response.interceptor'
;
import
{
Response
}
from
'express'
;
@
ApiTags
(
'translate'
)
@
ApiTags
(
'translate'
)
@
UseInterceptors
(
ApiResponseInterceptor
)
@
UseInterceptors
(
ApiResponseInterceptor
)
...
@@ -146,16 +148,18 @@ export class TranslateController {
...
@@ -146,16 +148,18 @@ export class TranslateController {
return
await
this
.
translateService
.
getProgress
(
dto
.
taskId
);
return
await
this
.
translateService
.
getProgress
(
dto
.
taskId
);
}
}
//
获取
翻译结果
//
下载
翻译结果
@
Get
(
'
result
'
)
@
Get
(
'
downloadImage
'
)
@
ApiOperation
({
summary
:
'
获取
翻译结果'
})
@
ApiOperation
({
summary
:
'
下载
翻译结果'
})
@
ApiResponse
({
@
ApiResponse
({
status
:
200
,
status
:
200
,
description
:
'成功返回翻译结果'
,
description
:
'成功返回翻译结果'
,
example
:
{},
})
})
@
Auth
()
@
Auth
()
async
getResult
(@
Query
()
dto
:
TranslateProgressReqDto
):
Promise
<
any
>
{
async
getResult
(
return
await
this
.
translateService
.
downloadImage
(
dto
.
taskId
);
@
Query
()
dto
:
TranslateProgressReqDto
,
@
Res
()
res
:
Response
,
):
Promise
<
any
>
{
return
await
this
.
translateService
.
downloadImage
(
dto
.
taskId
,
res
);
}
}
}
}
src/controller/translate/translate.service.ts
View file @
c7e4ad25
...
@@ -11,6 +11,8 @@ import { BadRequestError } from '@/common/exception/badRequest/BadRequestError';
...
@@ -11,6 +11,8 @@ import { BadRequestError } from '@/common/exception/badRequest/BadRequestError';
import
*
as
crypto
from
'crypto'
;
import
*
as
crypto
from
'crypto'
;
import
*
as
FormData
from
'form-data'
;
import
*
as
FormData
from
'form-data'
;
import
{
TranslateImageReqDto
}
from
'./dto/req/translateImageReq.dto'
;
import
{
TranslateImageReqDto
}
from
'./dto/req/translateImageReq.dto'
;
import
{
Response
}
from
'express'
;
import
axios
,
{
AxiosResponse
}
from
'axios'
;
@
Injectable
()
@
Injectable
()
export
class
TranslateService
{
export
class
TranslateService
{
...
@@ -167,7 +169,7 @@ 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
=
{
const
params
=
{
nonce_str
:
crypto
.
randomUUID
(),
nonce_str
:
crypto
.
randomUUID
(),
tid
:
taskId
,
tid
:
taskId
,
...
@@ -182,20 +184,26 @@ export class TranslateService {
...
@@ -182,20 +184,26 @@ export class TranslateService {
formData
.
append
(
'tid'
,
params
.
tid
);
formData
.
append
(
'tid'
,
params
.
tid
);
formData
.
append
(
'token'
,
token
);
formData
.
append
(
'token'
,
token
);
const
res
ult
=
await
axiosPostReque
st
(
const
res
ponse
:
AxiosResponse
=
await
axios
.
po
st
(
`
${
config
.
service
.
fanYiGou
.
endpoint
}
/TranslateApi/api/image/downloadImage`
,
`
${
config
.
service
.
fanYiGou
.
endpoint
}
/TranslateApi/api/image/downloadImage`
,
formData
,
formData
,
{
{
headers
:
{
headers
:
{
...
formData
.
getHeaders
(),
...
formData
.
getHeaders
(),
},
},
responseType
:
'stream'
,
// 设置 responseType 为 stream
},
},
);
);
if
(
!
result
?.
data
)
{
if
(
response
.
status
===
200
&&
!
response
?.
data
)
{
throw
BadRequestError
.
default
(
'下载翻译图片失败'
);
throw
BadRequestError
.
default
(
'下载翻译图片失败'
);
}
}
return
result
?.
data
;
// 设置响应头
res
.
setHeader
(
'Content-Type'
,
'image/jpeg'
);
// 根据实际文件类型设置
res
.
setHeader
(
'Content-Disposition'
,
'attachment; filename="image.jpg"'
);
// 设置下载文件名
// 将文件流返回给前端
response
.
data
.
pipe
(
res
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment