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
6deb6f25
authored
Jan 25, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 优化vision计费逻辑
parent
3d69bb7e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
10 deletions
+22
-10
common/image.go
+22
-10
No files found.
common/image.go
View file @
6deb6f25
...
...
@@ -68,17 +68,29 @@ func DecodeUrlImageData(imageUrl string) (image.Config, string, error) {
SysLog
(
fmt
.
Sprintf
(
"fail to get image from url: %s"
,
err
.
Error
()))
return
image
.
Config
{},
""
,
err
}
defer
response
.
Body
.
Close
()
// 限制读取的字节数,防止下载整个图片
limitReader
:=
io
.
LimitReader
(
response
.
Body
,
1024
*
20
)
//data, err := io.ReadAll(limitReader)
//if err != nil {
// log.Fatal(err)
//}
//log.Printf("%x", data)
config
,
format
,
err
:=
getImageConfig
(
limitReader
)
response
.
Body
.
Close
()
return
config
,
format
,
err
var
readData
[]
byte
for
_
,
limit
:=
range
[]
int64
{
1024
*
8
,
1024
*
24
,
1024
*
64
}
{
SysLog
(
fmt
.
Sprintf
(
"try to decode image config with limit: %d"
,
limit
))
// 从response.Body读取更多的数据直到达到当前的限制
additionalData
:=
make
([]
byte
,
limit
-
int64
(
len
(
readData
)))
n
,
_
:=
io
.
ReadFull
(
response
.
Body
,
additionalData
)
readData
=
append
(
readData
,
additionalData
[
:
n
]
...
)
// 使用io.MultiReader组合已经读取的数据和response.Body
limitReader
:=
io
.
MultiReader
(
bytes
.
NewReader
(
readData
),
response
.
Body
)
var
config
image
.
Config
var
format
string
config
,
format
,
err
=
getImageConfig
(
limitReader
)
if
err
==
nil
{
return
config
,
format
,
nil
}
}
return
image
.
Config
{},
""
,
err
// 返回最后一个错误
}
func
getImageConfig
(
reader
io
.
Reader
)
(
image
.
Config
,
string
,
error
)
{
...
...
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