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
d499b641
authored
Mar 03, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Improve image download and validation in GetImageFromUrl
parent
4208ecb9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
11 deletions
+44
-11
service/image.go
+44
-11
No files found.
service/image.go
View file @
d499b641
...
@@ -7,7 +7,9 @@ import (
...
@@ -7,7 +7,9 @@ import (
"fmt"
"fmt"
"image"
"image"
"io"
"io"
"net/http"
"one-api/common"
"one-api/common"
"one-api/constant"
"strings"
"strings"
"golang.org/x/image/webp"
"golang.org/x/image/webp"
...
@@ -23,7 +25,7 @@ func DecodeBase64ImageData(base64String string) (image.Config, string, string, e
...
@@ -23,7 +25,7 @@ func DecodeBase64ImageData(base64String string) (image.Config, string, string, e
decodedData
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
base64String
)
decodedData
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
base64String
)
if
err
!=
nil
{
if
err
!=
nil
{
fmt
.
Println
(
"Error: Failed to decode base64 string"
)
fmt
.
Println
(
"Error: Failed to decode base64 string"
)
return
image
.
Config
{},
""
,
""
,
err
return
image
.
Config
{},
""
,
""
,
fmt
.
Errorf
(
"failed to decode base64 string: %s"
,
err
.
Error
())
}
}
// 创建一个bytes.Buffer用于存储解码后的数据
// 创建一个bytes.Buffer用于存储解码后的数据
...
@@ -61,20 +63,51 @@ func DecodeBase64FileData(base64String string) (string, string, error) {
...
@@ -61,20 +63,51 @@ func DecodeBase64FileData(base64String string) (string, string, error) {
func
GetImageFromUrl
(
url
string
)
(
mimeType
string
,
data
string
,
err
error
)
{
func
GetImageFromUrl
(
url
string
)
(
mimeType
string
,
data
string
,
err
error
)
{
resp
,
err
:=
DoDownloadRequest
(
url
)
resp
,
err
:=
DoDownloadRequest
(
url
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
""
,
err
return
""
,
""
,
fmt
.
Errorf
(
"failed to download image: %w"
,
err
)
}
if
!
strings
.
HasPrefix
(
resp
.
Header
.
Get
(
"Content-Type"
),
"image/"
)
{
return
""
,
""
,
fmt
.
Errorf
(
"invalid content type: %s, required image/*"
,
resp
.
Header
.
Get
(
"Content-Type"
))
}
}
defer
resp
.
Body
.
Close
()
defer
resp
.
Body
.
Close
()
buffer
:=
bytes
.
NewBuffer
(
nil
)
_
,
err
=
buffer
.
ReadFrom
(
resp
.
Body
)
// Check HTTP status code
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
""
,
""
,
fmt
.
Errorf
(
"failed to download image: HTTP %d"
,
resp
.
StatusCode
)
}
contentType
:=
resp
.
Header
.
Get
(
"Content-Type"
)
if
contentType
!=
"application/octet-stream"
&&
!
strings
.
HasPrefix
(
contentType
,
"image/"
)
{
return
""
,
""
,
fmt
.
Errorf
(
"invalid content type: %s, required image/*"
,
contentType
)
}
maxImageSize
:=
int64
(
constant
.
MaxFileDownloadMB
*
1024
*
1024
)
// Check Content-Length if available
if
resp
.
ContentLength
>
maxImageSize
{
return
""
,
""
,
fmt
.
Errorf
(
"image size %d exceeds maximum allowed size of %d bytes"
,
resp
.
ContentLength
,
maxImageSize
)
}
// Use LimitReader to prevent reading oversized images
limitReader
:=
io
.
LimitReader
(
resp
.
Body
,
maxImageSize
)
buffer
:=
&
bytes
.
Buffer
{}
written
,
err
:=
io
.
Copy
(
buffer
,
limitReader
)
if
err
!=
nil
{
if
err
!=
nil
{
return
return
""
,
""
,
fmt
.
Errorf
(
"failed to read image data: %w"
,
err
)
}
if
written
>=
maxImageSize
{
return
""
,
""
,
fmt
.
Errorf
(
"image size exceeds maximum allowed size of %d bytes"
,
maxImageSize
)
}
}
mimeType
=
resp
.
Header
.
Get
(
"Content-Type"
)
data
=
base64
.
StdEncoding
.
EncodeToString
(
buffer
.
Bytes
())
data
=
base64
.
StdEncoding
.
EncodeToString
(
buffer
.
Bytes
())
return
mimeType
=
contentType
// Handle application/octet-stream type
if
mimeType
==
"application/octet-stream"
{
_
,
format
,
_
,
err
:=
DecodeBase64ImageData
(
data
)
if
err
!=
nil
{
return
""
,
""
,
err
}
mimeType
=
"image/"
+
format
}
return
mimeType
,
data
,
nil
}
}
func
DecodeUrlImageData
(
imageUrl
string
)
(
image
.
Config
,
string
,
error
)
{
func
DecodeUrlImageData
(
imageUrl
string
)
(
image
.
Config
,
string
,
error
)
{
...
@@ -92,7 +125,7 @@ func DecodeUrlImageData(imageUrl string) (image.Config, string, error) {
...
@@ -92,7 +125,7 @@ func DecodeUrlImageData(imageUrl string) (image.Config, string, error) {
mimeType
:=
response
.
Header
.
Get
(
"Content-Type"
)
mimeType
:=
response
.
Header
.
Get
(
"Content-Type"
)
if
!
strings
.
HasPrefix
(
mimeType
,
"image/"
)
{
if
mimeType
!=
"application/octet-stream"
&&
!
strings
.
HasPrefix
(
mimeType
,
"image/"
)
{
return
image
.
Config
{},
""
,
fmt
.
Errorf
(
"invalid content type: %s, required image/*"
,
mimeType
)
return
image
.
Config
{},
""
,
fmt
.
Errorf
(
"invalid content type: %s, required image/*"
,
mimeType
)
}
}
...
...
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