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