mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 18:10:48 +08:00
[assetserver] Add more builtin mimetypes by extension (#2391)
This commit is contained in:
parent
bd184cab85
commit
cccd708b2b
@ -9,24 +9,44 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
cache = map[string]string{}
|
||||
mutex sync.Mutex
|
||||
mimeCache = map[string]string{}
|
||||
mimeMutex sync.Mutex
|
||||
|
||||
// The list of builtin mime-types by extension as defined by
|
||||
// the golang standard lib package "mime"
|
||||
// The standard lib also takes into account mime type definitions from
|
||||
// etc files like '/etc/apache2/mime.types' but we want to have the
|
||||
// same behavivour on all platforms and not depend on some external file.
|
||||
mimeTypesByExt = map[string]string{
|
||||
".avif": "image/avif",
|
||||
".css": "text/css; charset=utf-8",
|
||||
".gif": "image/gif",
|
||||
".htm": "text/html; charset=utf-8",
|
||||
".html": "text/html; charset=utf-8",
|
||||
".jpeg": "image/jpeg",
|
||||
".jpg": "image/jpeg",
|
||||
".js": "text/javascript; charset=utf-8",
|
||||
".json": "application/json",
|
||||
".mjs": "text/javascript; charset=utf-8",
|
||||
".pdf": "application/pdf",
|
||||
".png": "image/png",
|
||||
".svg": "image/svg+xml",
|
||||
".wasm": "application/wasm",
|
||||
".webp": "image/webp",
|
||||
".xml": "text/xml; charset=utf-8",
|
||||
}
|
||||
)
|
||||
|
||||
func GetMimetype(filename string, data []byte) string {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
mimeMutex.Lock()
|
||||
defer mimeMutex.Unlock()
|
||||
|
||||
// short-circuit .js, .css to ensure the
|
||||
// browser evaluates them in the right context
|
||||
switch filepath.Ext(filename) {
|
||||
case ".js":
|
||||
return "application/javascript"
|
||||
case ".css":
|
||||
return "text/css; charset=utf-8"
|
||||
result := mimeTypesByExt[filepath.Ext(filename)]
|
||||
if result != "" {
|
||||
return result
|
||||
}
|
||||
|
||||
result := cache[filename]
|
||||
result = mimeCache[filename]
|
||||
if result != "" {
|
||||
return result
|
||||
}
|
||||
@ -42,6 +62,6 @@ func GetMimetype(filename string, data []byte) string {
|
||||
result = "application/octet-stream"
|
||||
}
|
||||
|
||||
cache[filename] = result
|
||||
mimeCache[filename] = result
|
||||
return result
|
||||
}
|
||||
|
@ -25,10 +25,11 @@ func TestGetMimetype(t *testing.T) {
|
||||
want string
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{"nil data", args{"nil.svg", nil}, "text/plain"},
|
||||
{"empty data", args{"empty.html", emptyMsg}, "text/plain"},
|
||||
{"nil data", args{"nil.svg", nil}, "image/svg+xml"},
|
||||
{"empty data", args{"empty.html", emptyMsg}, "text/html; charset=utf-8"},
|
||||
{"css", args{"test.css", css}, "text/css; charset=utf-8"},
|
||||
{"js", args{"test.js", []byte("let foo = 'bar'; console.log(foo);")}, "application/javascript"},
|
||||
{"js", args{"test.js", []byte("let foo = 'bar'; console.log(foo);")}, "text/javascript; charset=utf-8"},
|
||||
{"mjs", args{"test.mjs", []byte("let foo = 'bar'; console.log(foo);")}, "text/javascript; charset=utf-8"},
|
||||
{"html-utf8", args{"test_utf8.html", html}, "text/html; charset=utf-8"},
|
||||
{"html-bom-utf8", args{"test_bom_utf8.html", bomHtml}, "text/html; charset=utf-8"},
|
||||
{"svg", args{"test.svg", svg}, "image/svg+xml"},
|
||||
|
@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Added Webview GPU acceleration options for [Windows](/docs/reference/options#webviewgpuisdisabled) and [Linux](/docs/reference/options#webviewgpupolicy). Added by @Lyimmi in [PR](https://github.com/wailsapp/wails/pull/2266)
|
||||
- Added `EnableFraudulentWebsiteDetection` option to opt-in to scan services for fraudulent content, such as malware or phishing attempts. Older releases had the scan services per default activated. Added by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2269)
|
||||
- Allow an [AssetServer Middleware](/docs/reference/options#middleware) to specify the `Content-Type` of a file served by the [Assets](/docs/reference/options#assets-1) `fs.FS`. Added by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2286)
|
||||
- The AssetServer now detects more mimetypes by extension, e.g. `.mjs`. Added by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2391)
|
||||
|
||||
### Changed
|
||||
- Improved fullscreen mode for frameless window on Windows. Changed by @stffabi in [PR](https://github.com/wailsapp/wails/pull/2279), [PR](https://github.com/wailsapp/wails/pull/2288) and [PR](https://github.com/wailsapp/wails/pull/2299)
|
||||
|
Loading…
Reference in New Issue
Block a user