Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[assetserver] Add more builtin mimetypes by extension #2391

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions v2/pkg/assetserver/mimecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

// 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"
mimeMutex.Lock()
defer mimeMutex.Unlock()

result := mimeTypesByExt[filepath.Ext(filename)]
if result != "" {
return result
}

result := cache[filename]
result = mimeCache[filename]
if result != "" {
return result
}
Expand All @@ -42,6 +62,6 @@ func GetMimetype(filename string, data []byte) string {
result = "application/octet-stream"
}

cache[filename] = result
mimeCache[filename] = result
return result
}
7 changes: 4 additions & 3 deletions v2/pkg/assetserver/mimecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
1 change: 1 addition & 0 deletions website/src/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down