5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 12:19:38 +08:00
wails/v2/internal/frontend/assetserver/mimecache.go
2022-04-01 08:21:57 +11:00

47 lines
695 B
Go

package assetserver
import (
"net/http"
"path/filepath"
"sync"
"github.com/wailsapp/mimetype"
)
var (
cache = map[string]string{}
mutex sync.Mutex
)
func GetMimetype(filename string, data []byte) string {
mutex.Lock()
defer mutex.Unlock()
if filepath.Ext(filename) == ".js" {
return "application/javascript"
}
if filepath.Ext(filename) == ".css" {
return "text/css"
}
result := cache[filename]
if result != "" {
return result
}
detect := mimetype.Detect(data)
if detect == nil {
result = http.DetectContentType(data)
} else {
result = detect.String()
}
if result == "" {
result = "application/octet-stream"
}
cache[filename] = result
return result
}