fix: use defaultMIME fallback in DetectContentType
This commit is contained in:
parent
a3b4507be0
commit
13547ec63e
@ -102,7 +102,7 @@ func SetDefaultType(defaultType string) {
|
||||
// - filePath: 文件路径
|
||||
//
|
||||
// 返回值:
|
||||
// - string: MIME 类型,未知类型返回空字符串
|
||||
// - string: MIME 类型,未知类型返回 defaultMIME(默认为 application/octet-stream)
|
||||
func DetectContentType(filePath string) string {
|
||||
ext := strings.ToLower(filepath.Ext(filePath))
|
||||
|
||||
@ -151,5 +151,11 @@ func DetectContentType(filePath string) string {
|
||||
entry.element = mimeLRU.PushFront(entry)
|
||||
mimeCache[ext] = entry
|
||||
|
||||
if mimeType == "" {
|
||||
defaultMutex.RLock()
|
||||
mimeType = defaultMIME
|
||||
defaultMutex.RUnlock()
|
||||
}
|
||||
|
||||
return mimeType
|
||||
}
|
||||
|
||||
@ -31,8 +31,8 @@ func TestDetectContentType(t *testing.T) {
|
||||
{"test.WEBMANIFEST", "application/manifest+json"},
|
||||
{"test.JPG", "image/jpeg"}, // Go 已知,也处理大小写
|
||||
|
||||
// 未知类型
|
||||
{"test.unknown", ""},
|
||||
// 未知类型 - 回退到 defaultMIME
|
||||
{"test.unknown", "application/octet-stream"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user