| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- package mimetype
- import (
- "mime"
- "path/filepath"
- "strings"
- )
- var extraMIMETypes = map[string]string{
- // --- Text ---
- ".txt": "text/plain; charset=utf-8",
- ".log": "text/plain; charset=utf-8",
- ".md": "text/markdown; charset=utf-8",
- ".csv": "text/csv; charset=utf-8",
- ".tsv": "text/tab-separated-values",
- ".css": "text/css",
- ".htm": "text/html",
- ".html": "text/html",
- ".shtml": "text/html",
- ".xml": "application/xml",
- ".yaml": "application/x-yaml",
- ".yml": "application/x-yaml",
- // --- JavaScript / JSON ---
- ".js": "application/javascript",
- ".mjs": "application/javascript",
- ".json": "application/json",
- ".jsonld": "application/ld+json",
- // --- Images ---
- ".jpg": "image/jpeg",
- ".jpeg": "image/jpeg",
- ".png": "image/png",
- ".gif": "image/gif",
- ".svg": "image/svg+xml",
- ".webp": "image/webp",
- ".bmp": "image/bmp",
- ".ico": "image/x-icon",
- ".tif": "image/tiff",
- ".tiff": "image/tiff",
- ".avif": "image/avif",
- ".heif": "image/heif",
- ".heic": "image/heic",
- // --- Audio ---
- ".mp3": "audio/mpeg",
- ".wav": "audio/wav",
- ".aac": "audio/aac",
- ".flac": "audio/flac",
- ".ogg": "audio/ogg",
- ".oga": "audio/ogg",
- ".m4a": "audio/mp4",
- ".mid": "audio/midi",
- ".midi": "audio/midi",
- // --- Video ---
- ".mp4": "video/mp4",
- ".mov": "video/quicktime",
- ".avi": "video/x-msvideo",
- ".wmv": "video/x-ms-wmv",
- ".flv": "video/x-flv",
- ".mkv": "video/x-matroska",
- ".webm": "video/webm",
- ".mpeg": "video/mpeg",
- ".mpg": "video/mpeg",
- // --- Archives ---
- ".zip": "application/zip",
- ".rar": "application/vnd.rar",
- ".tar": "application/x-tar",
- ".gz": "application/gzip",
- ".tgz": "application/gzip",
- ".bz": "application/x-bzip",
- ".bz2": "application/x-bzip2",
- ".7z": "application/x-7z-compressed",
- // --- Documents ---
- ".pdf": "application/pdf",
- ".doc": "application/msword",
- ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
- ".xls": "application/vnd.ms-excel",
- ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
- ".ppt": "application/vnd.ms-powerpoint",
- ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
- // --- Code / Config / Dev ---
- ".go": "text/plain",
- ".py": "text/x-python",
- ".java": "text/x-java-source",
- ".c": "text/x-c",
- ".cpp": "text/x-c++",
- ".h": "text/x-c",
- ".hpp": "text/x-c++",
- ".rs": "text/plain",
- ".php": "text/x-php",
- ".sql": "application/sql",
- ".env": "text/plain",
- ".ini": "text/plain",
- // --- Fonts ---
- ".woff": "font/woff",
- ".woff2": "font/woff2",
- ".ttf": "font/ttf",
- ".otf": "font/otf",
- ".eot": "application/vnd.ms-fontobject",
- // --- Vector / CAD ---
- ".psd": "image/vnd.adobe.photoshop",
- ".ai": "application/postscript",
- ".eps": "application/postscript",
- // --- Binary / Program ---
- ".exe": "application/vnd.microsoft.portable-executable",
- ".bin": "application/octet-stream",
- ".dll": "application/x-msdownload",
- ".apk": "application/vnd.android.package-archive",
- ".dmg": "application/x-apple-diskimage",
- ".iso": "application/x-iso9660-image",
- // --- Misc ---
- ".rtf": "application/rtf",
- ".wasm": "application/wasm",
- ".swf": "application/x-shockwave-flash",
- ".map": "application/json",
- }
- // GetContentType 根据文件名/扩展名获取 Content-Type
- func GetContentType(filename string) string {
- ext := strings.ToLower(filepath.Ext(filename))
- if ext == "" {
- return "application/octet-stream"
- }
- // 1. 标准库优先
- if ct := mime.TypeByExtension(ext); ct != "" {
- return ct
- }
- // 2. 补充表
- if ct, ok := extraMIMETypes[ext]; ok {
- return ct
- }
- // 3. 默认值
- return "application/octet-stream"
- }
|