Skip to content

Commit

Permalink
[assetserver] Add more builtin mimetypes by extension
Browse files Browse the repository at this point in the history
  • Loading branch information
stffabi committed Feb 21, 2023
1 parent bd184ca commit dfc5f4b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
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

0 comments on commit dfc5f4b

Please sign in to comment.