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
680ff8e8
authored
Jun 17, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(file_decoder): enhance MIME type detection based on URL and Content-Disposition header
parent
129d1b08
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
90 additions
and
1 deletions
+90
-1
service/file_decoder.go
+90
-1
No files found.
service/file_decoder.go
View file @
680ff8e8
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"io"
"io"
"one-api/constant"
"one-api/constant"
"one-api/dto"
"one-api/dto"
"strings"
)
)
func
GetFileBase64FromUrl
(
url
string
)
(
*
dto
.
LocalFileData
,
error
)
{
func
GetFileBase64FromUrl
(
url
string
)
(
*
dto
.
LocalFileData
,
error
)
{
...
@@ -30,9 +31,97 @@ func GetFileBase64FromUrl(url string) (*dto.LocalFileData, error) {
...
@@ -30,9 +31,97 @@ func GetFileBase64FromUrl(url string) (*dto.LocalFileData, error) {
// Convert to base64
// Convert to base64
base64Data
:=
base64
.
StdEncoding
.
EncodeToString
(
fileBytes
)
base64Data
:=
base64
.
StdEncoding
.
EncodeToString
(
fileBytes
)
mimeType
:=
resp
.
Header
.
Get
(
"Content-Type"
)
if
mimeType
==
"application/octet-stream"
{
// try to guess the MIME type from the url last segment
urlParts
:=
strings
.
Split
(
url
,
"/"
)
if
len
(
urlParts
)
>
0
{
lastSegment
:=
urlParts
[
len
(
urlParts
)
-
1
]
if
strings
.
Contains
(
lastSegment
,
"."
)
{
// Extract the file extension
filename
:=
strings
.
Split
(
lastSegment
,
"."
)
if
len
(
filename
)
>
1
{
ext
:=
strings
.
ToLower
(
filename
[
len
(
filename
)
-
1
])
// Guess MIME type based on file extension
mimeType
=
GetMimeTypeByExtension
(
ext
)
}
}
}
else
{
// try to guess the MIME type from the file extension
fileName
:=
resp
.
Header
.
Get
(
"Content-Disposition"
)
if
fileName
!=
""
{
// Extract the filename from the Content-Disposition header
parts
:=
strings
.
Split
(
fileName
,
";"
)
for
_
,
part
:=
range
parts
{
if
strings
.
HasPrefix
(
strings
.
TrimSpace
(
part
),
"filename="
)
{
fileName
=
strings
.
TrimSpace
(
strings
.
TrimPrefix
(
part
,
"filename="
))
// Remove quotes if present
if
len
(
fileName
)
>
2
&&
fileName
[
0
]
==
'"'
&&
fileName
[
len
(
fileName
)
-
1
]
==
'"'
{
fileName
=
fileName
[
1
:
len
(
fileName
)
-
1
]
}
// Guess MIME type based on file extension
if
ext
:=
strings
.
ToLower
(
strings
.
TrimPrefix
(
fileName
,
"."
));
ext
!=
""
{
mimeType
=
GetMimeTypeByExtension
(
ext
)
}
break
}
}
}
}
}
return
&
dto
.
LocalFileData
{
return
&
dto
.
LocalFileData
{
Base64Data
:
base64Data
,
Base64Data
:
base64Data
,
MimeType
:
resp
.
Header
.
Get
(
"Content-Type"
)
,
MimeType
:
mimeType
,
Size
:
int64
(
len
(
fileBytes
)),
Size
:
int64
(
len
(
fileBytes
)),
},
nil
},
nil
}
}
func
GetMimeTypeByExtension
(
ext
string
)
string
{
// Convert to lowercase for case-insensitive comparison
ext
=
strings
.
ToLower
(
ext
)
switch
ext
{
// Text files
case
"txt"
:
return
"text/plain"
// Image files
case
"jpg"
,
"jpeg"
:
return
"image/jpeg"
case
"png"
:
return
"image/png"
case
"gif"
:
return
"image/gif"
// Audio files
case
"mp3"
:
return
"audio/mp3"
case
"wav"
:
return
"audio/wav"
case
"mpeg"
:
return
"audio/mpeg"
// Video files
case
"mp4"
:
return
"video/mp4"
case
"wmv"
:
return
"video/wmv"
case
"flv"
:
return
"video/flv"
case
"mov"
:
return
"video/mov"
case
"mpg"
:
return
"video/mpg"
case
"avi"
:
return
"video/avi"
case
"mpegps"
:
return
"video/mpegps"
// Document files
case
"pdf"
:
return
"application/pdf"
default
:
return
"application/octet-stream"
// Default for unknown types
}
}
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