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 ".mkv": v = "video/x-matroska" case ".epub", ".mobi": v = "application/epub+zip" k = "Content-Disposition" v = "attachment" case ".md": v = "text/markdown" v = "text/html" case ".ico": v = "image/x-icon" case ".pdf": w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%q", path.Base(r.URL.Path))) v = "application/pdf" case ".m4b": v = "application/octet-stream" case ".rss", ".feed", ".rdf": v = "application/rss+xml" case ".htm": v = "text/html; charset=utf-8" case ".html": v = "text/html; charset=utf-8" case ".jpeg": v = "image/jpeg" case ".jpg": v = "image/jpeg" case ".wasm": v = "application/wasm" case ".aac": v = "audio/aac" case ".abw": v = "application/x-abiword" case ".arc": v = "application/x-freearc" case ".avif": v = "image/avif" case ".avi": v = "video/x-msvideo" case ".azw": v = "application/vnd.amazon.ebook" case ".bin": v = "application/octet-stream" case ".bmp": v = "image/bmp" case ".bz": v = "application/x-bzip" case ".bz2": v = "application/x-bzip2" case ".cda": v = "application/x-cdf" case ".csh": v = "application/x-csh" case ".css": v = "text/css" case ".csv": v = "text/csv" case ".doc": v = "application/msword" case ".docx": v = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" case ".eot": v = "application/vnd.ms-fontobject" case ".gz": w.Header().Set("Content-Encoding", "gzip") v = "application/gzip" if strings.HasSuffix(r.URL.Path, ".js.gz") { v = "application/javascript" } else if strings.HasSuffix(r.URL.Path, ".wasm.gz") { v = "application/wasm" } case ".gif": v = "image/gif" case ".htm,": v = ".html text/html" case ".ics": v = "text/calendar" case ".jar": v = "application/java-archive" case ".jpeg,": v = ".jpg image/jpeg" case ".js": v = "text/javascript (Specifications: HTML and RFC 9239)" case ".json": v = "application/json" case ".jsonld": v = "application/ld+json" case ".mid,": v = ".midi audio/midi, audio/x-midi" case ".mjs": v = "text/javascript" case ".mp3": v = "audio/mpeg" case ".mp4": v = "video/mp4" case ".mpeg": v = "video/mpeg" case ".mpkg": v = "application/vnd.apple.installer+xml" case ".odp": v = "application/vnd.oasis.opendocument.presentation" case ".ods": v = "application/vnd.oasis.opendocument.spreadsheet" case ".odt": v = "application/vnd.oasis.opendocument.text" case ".oga": v = "audio/ogg" case ".ogv": v = "video/ogg" case ".ogx": v = "application/ogg" case ".opus": v = "audio/opus" case ".otf": v = "font/otf" case ".png": v = "image/png" case ".php": v = "application/x-httpd-php" case ".ppt": v = "application/vnd.ms-powerpoint" case ".pptx": v = "application/vnd.openxmlformats-officedocument.presentationml.presentation" case ".rar": v = "application/vnd.rar" case ".rtf": v = "application/rtf" case ".sh": v = "application/x-sh" case ".svg": v = "image/svg+xml" case ".tar": v = "application/x-tar" case ".tif,": v = ".tiff image/tiff" case ".ts": v = "video/mp2t" case ".ttf": v = "font/ttf" case ".txt": v = "text/plain" case ".vsd": v = "application/vnd.visio" case ".wav": v = "audio/wav" case ".weba": v = "audio/webm" case ".webm": v = "video/webm" case ".webp": v = "image/webp" case ".woff": v = "font/woff" case ".woff2": v = "font/woff2" case ".xhtml": v = "application/xhtml+xml" case ".xls": v = "application/vnd.ms-excel" case ".xlsx": v = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" case ".xml": v = "application/xml" case ".xul": v = "application/vnd.mozilla.xul+xml" case ".zip": v = "application/zip" case ".3gp": v = "video/3gpp; audio/3gpp if it doesn't contain video" case ".3g2": v = "video/3gpp2; audio/3gpp2 if it doesn't contain video" case ".7z": v = "application/x-7z-compressed" default: return } w.Header().Set(k, v) }