fix: use defaultMIME fallback in DetectContentType

This commit is contained in:
xfy 2026-06-03 17:44:16 +08:00
parent a3b4507be0
commit 13547ec63e
2 changed files with 9 additions and 3 deletions

View File

@ -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
}

View File

@ -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 {