add SetContentTypeIfMedia

master
Bel LaPointe 2023-04-10 11:12:40 -06:00
parent 6b42100cf2
commit 32da87640b
1 changed files with 71 additions and 0 deletions

71
contenttype.go Executable file
View File

@ -0,0 +1,71 @@
package gziphttp
import (
"fmt"
"net/http"
"path"
"strings"
)
func SetContentTypeIfMedia(w http.ResponseWriter, r *http.Request) {
ext := strings.ToLower(path.Ext(r.URL.Path))
if i := strings.LastIndex(ext, "."); i != -1 {
ext = ext[i:]
}
k := "Content-Type"
v := ""
switch ext {
case ".mp4":
v = "video/mp4"
case ".mkv":
v = "video/x-matroska"
case ".mp3":
v = "audio/mpeg3"
case ".epub", ".mobi":
k = "Content-Disposition"
v = "attachment"
case ".jpg", ".jpeg":
v = "image/jpeg"
case ".md":
v = "text/markdown"
v = "text/html"
case ".gif":
v = "image/gif"
case ".png":
v = "image/png"
case ".ico":
v = "image/x-icon"
case ".svg":
v = "image/svg+xml"
case ".css":
v = "text/css"
case ".js":
v = "text/javascript"
case ".json":
v = "application/json"
case ".html", ".htm":
v = "text/html"
case ".pdf":
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%q", path.Base(r.URL.Path)))
v = "application/pdf"
case ".webm":
v = "video/webm"
case ".weba":
v = "audio/webm"
case ".webp":
v = "image/webp"
case ".zip":
v = "application/zip"
case ".7z":
v = "application/x-7z-compressed"
case ".tar":
v = "application/x-tar"
case ".m4b":
v = "application/octet-stream"
case ".xml", ".rss", ".feed", ".rdf":
v = "application/rss+xml"
default:
return
}
w.Header().Set(k, v)
}