fix: add correct content-type to their respective files

This commit is contained in:
2025-02-21 19:06:28 +01:00
parent d17932adb6
commit 4879729893

22
main.go
View File

@@ -34,21 +34,23 @@ func SafeFileServer(root http.FileSystem) http.Handler {
w.Header().Set("X-XSS-Protection", "1; mode=block") w.Header().Set("X-XSS-Protection", "1; mode=block")
log.Println("assets handler", r.URL.Path) log.Println("assets handler", r.URL.Path)
log.Println(w.Header())
// Only allow specific file extensions // Only allow specific file extensions
if allowed := func(path string) bool { if allowed := func(path string) bool {
allowedExts := map[string]bool{ allowedExts := map[string]string{
".js": true, ".js": "text/javascript;charset=utf-8",
".css": true, ".css": "text/css;charset=utf-8",
".png": true, ".png": "image/png",
".webp": true, ".webp": "image/webp",
".jpg": true, ".jpg": "image/jpeg",
".jpeg": true, ".jpeg": "image/jpeg",
".svg": true, ".svg": "image/svg+xml",
".ico": true, ".ico": "image/x-icon",
} }
for ext := range allowedExts { for ext, contentType := range allowedExts {
if strings.HasSuffix(path, ext) { if strings.HasSuffix(path, ext) {
w.Header().Set("Content-Type", contentType)
return true return true
} }
} }