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) }