Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
1382ec4f
authored
Aug 12, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(adaptor): optimize multipart form handling and resource management
parent
9717af7d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
23 deletions
+28
-23
relay/channel/openai/adaptor.go
+28
-23
No files found.
relay/channel/openai/adaptor.go
View file @
1382ec4f
...
@@ -359,40 +359,42 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
...
@@ -359,40 +359,42 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
writer
:=
multipart
.
NewWriter
(
&
requestBody
)
writer
:=
multipart
.
NewWriter
(
&
requestBody
)
writer
.
WriteField
(
"model"
,
request
.
Model
)
writer
.
WriteField
(
"model"
,
request
.
Model
)
// 获取所有表单字段
// 使用已解析的 multipart 表单,避免重复解析
formData
:=
c
.
Request
.
PostForm
mf
:=
c
.
Request
.
MultipartForm
// 遍历表单字段并打印输出
if
mf
==
nil
{
for
key
,
values
:=
range
formData
{
if
_
,
err
:=
c
.
MultipartForm
();
err
!=
nil
{
if
key
==
"model"
{
return
nil
,
errors
.
New
(
"failed to parse multipart form"
)
continue
}
for
_
,
value
:=
range
values
{
writer
.
WriteField
(
key
,
value
)
}
}
mf
=
c
.
Request
.
MultipartForm
}
}
// Parse the multipart form to handle both single image and multiple images
// 写入所有非文件字段
if
err
:=
c
.
Request
.
ParseMultipartForm
(
32
<<
20
);
err
!=
nil
{
// 32MB max memory
if
mf
!=
nil
{
return
nil
,
errors
.
New
(
"failed to parse multipart form"
)
for
key
,
values
:=
range
mf
.
Value
{
if
key
==
"model"
{
continue
}
for
_
,
value
:=
range
values
{
writer
.
WriteField
(
key
,
value
)
}
}
}
}
if
c
.
Request
.
MultipartForm
!=
nil
&&
c
.
Request
.
MultipartForm
.
File
!=
nil
{
if
mf
!=
nil
&&
mf
.
File
!=
nil
{
// Check if "image" field exists in any form, including array notation
// Check if "image" field exists in any form, including array notation
var
imageFiles
[]
*
multipart
.
FileHeader
var
imageFiles
[]
*
multipart
.
FileHeader
var
exists
bool
var
exists
bool
// First check for standard "image" field
// First check for standard "image" field
if
imageFiles
,
exists
=
c
.
Request
.
MultipartForm
.
File
[
"image"
];
!
exists
||
len
(
imageFiles
)
==
0
{
if
imageFiles
,
exists
=
mf
.
File
[
"image"
];
!
exists
||
len
(
imageFiles
)
==
0
{
// If not found, check for "image[]" field
// If not found, check for "image[]" field
if
imageFiles
,
exists
=
c
.
Request
.
MultipartForm
.
File
[
"image[]"
];
!
exists
||
len
(
imageFiles
)
==
0
{
if
imageFiles
,
exists
=
mf
.
File
[
"image[]"
];
!
exists
||
len
(
imageFiles
)
==
0
{
// If still not found, iterate through all fields to find any that start with "image["
// If still not found, iterate through all fields to find any that start with "image["
foundArrayImages
:=
false
foundArrayImages
:=
false
for
fieldName
,
files
:=
range
c
.
Request
.
MultipartForm
.
File
{
for
fieldName
,
files
:=
range
mf
.
File
{
if
strings
.
HasPrefix
(
fieldName
,
"image["
)
&&
len
(
files
)
>
0
{
if
strings
.
HasPrefix
(
fieldName
,
"image["
)
&&
len
(
files
)
>
0
{
foundArrayImages
=
true
foundArrayImages
=
true
for
_
,
file
:=
range
files
{
imageFiles
=
append
(
imageFiles
,
files
...
)
imageFiles
=
append
(
imageFiles
,
file
)
}
}
}
}
}
...
@@ -409,7 +411,6 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
...
@@ -409,7 +411,6 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to open image file %d: %w"
,
i
,
err
)
return
nil
,
fmt
.
Errorf
(
"failed to open image file %d: %w"
,
i
,
err
)
}
}
defer
file
.
Close
()
// If multiple images, use image[] as the field name
// If multiple images, use image[] as the field name
fieldName
:=
"image"
fieldName
:=
"image"
...
@@ -433,15 +434,18 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
...
@@ -433,15 +434,18 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
if
_
,
err
:=
io
.
Copy
(
part
,
file
);
err
!=
nil
{
if
_
,
err
:=
io
.
Copy
(
part
,
file
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"copy file failed for image %d: %w"
,
i
,
err
)
return
nil
,
fmt
.
Errorf
(
"copy file failed for image %d: %w"
,
i
,
err
)
}
}
// 复制完立即关闭,避免在循环内使用 defer 占用资源
_
=
file
.
Close
()
}
}
// Handle mask file if present
// Handle mask file if present
if
maskFiles
,
exists
:=
c
.
Request
.
MultipartForm
.
File
[
"mask"
];
exists
&&
len
(
maskFiles
)
>
0
{
if
maskFiles
,
exists
:=
mf
.
File
[
"mask"
];
exists
&&
len
(
maskFiles
)
>
0
{
maskFile
,
err
:=
maskFiles
[
0
]
.
Open
()
maskFile
,
err
:=
maskFiles
[
0
]
.
Open
()
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"failed to open mask file"
)
return
nil
,
errors
.
New
(
"failed to open mask file"
)
}
}
defer
maskFile
.
Close
()
// 复制完立即关闭,避免在循环内使用 defer 占用资源
// Determine MIME type for mask file
// Determine MIME type for mask file
mimeType
:=
detectImageMimeType
(
maskFiles
[
0
]
.
Filename
)
mimeType
:=
detectImageMimeType
(
maskFiles
[
0
]
.
Filename
)
...
@@ -459,6 +463,7 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
...
@@ -459,6 +463,7 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
if
_
,
err
:=
io
.
Copy
(
maskPart
,
maskFile
);
err
!=
nil
{
if
_
,
err
:=
io
.
Copy
(
maskPart
,
maskFile
);
err
!=
nil
{
return
nil
,
errors
.
New
(
"copy mask file failed"
)
return
nil
,
errors
.
New
(
"copy mask file failed"
)
}
}
_
=
maskFile
.
Close
()
}
}
}
else
{
}
else
{
return
nil
,
errors
.
New
(
"no multipart form data found"
)
return
nil
,
errors
.
New
(
"no multipart form data found"
)
...
@@ -467,7 +472,7 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
...
@@ -467,7 +472,7 @@ func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInf
// 关闭 multipart 编写器以设置分界线
// 关闭 multipart 编写器以设置分界线
writer
.
Close
()
writer
.
Close
()
c
.
Request
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
c
.
Request
.
Header
.
Set
(
"Content-Type"
,
writer
.
FormDataContentType
())
return
bytes
.
NewReader
(
requestBody
.
Bytes
())
,
nil
return
&
requestBody
,
nil
default
:
default
:
return
request
,
nil
return
request
,
nil
...
...
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