feat: add content type and path helpers

This commit is contained in:
2026-07-03 02:54:56 +02:00
parent 58abaaceb0
commit 01776f4eef

View File

@@ -1,7 +1,9 @@
package server
import (
"mime"
"net/http"
"path/filepath"
"strings"
)
@@ -16,3 +18,41 @@ func addVary(header http.Header, value string) {
}
header.Add("Vary", value)
}
//nolint:unused
func isCompressibleContentType(value string) bool {
mediaType, _, err := mime.ParseMediaType(value)
if err != nil {
mediaType = strings.TrimSpace(strings.Split(value, ";")[0])
}
mediaType = strings.ToLower(mediaType)
if strings.HasPrefix(mediaType, "text/") || strings.HasSuffix(mediaType, "+json") || strings.HasSuffix(mediaType, "+xml") {
return true
}
switch mediaType {
case "application/javascript", "application/json", "application/manifest+json", "application/x-javascript", "application/xhtml+xml", "application/xml", "image/svg+xml":
return true
default:
return false
}
}
//nolint:unused
func isCompressedPath(path string) bool {
switch strings.ToLower(filepath.Ext(path)) {
case ".7z", ".avif", ".br", ".gif", ".gz", ".ico", ".jpeg", ".jpg", ".m4a", ".m4v", ".mov", ".mp3", ".mp4", ".oga", ".ogg", ".ogv", ".pdf", ".png", ".rar", ".webm", ".webp", ".woff", ".woff2", ".zip":
return true
default:
return false
}
}
//nolint:unused
func isImagePath(path string) bool {
switch strings.ToLower(filepath.Ext(path)) {
case ".avif", ".gif", ".ico", ".jpeg", ".jpg", ".png", ".svg", ".webp":
return true
default:
return false
}
}