mimetype.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package mimetype
  2. import (
  3. "mime"
  4. "path/filepath"
  5. "strings"
  6. )
  7. var extraMIMETypes = map[string]string{
  8. // --- Text ---
  9. ".txt": "text/plain; charset=utf-8",
  10. ".log": "text/plain; charset=utf-8",
  11. ".md": "text/markdown; charset=utf-8",
  12. ".csv": "text/csv; charset=utf-8",
  13. ".tsv": "text/tab-separated-values",
  14. ".css": "text/css",
  15. ".htm": "text/html",
  16. ".html": "text/html",
  17. ".shtml": "text/html",
  18. ".xml": "application/xml",
  19. ".yaml": "application/x-yaml",
  20. ".yml": "application/x-yaml",
  21. // --- JavaScript / JSON ---
  22. ".js": "application/javascript",
  23. ".mjs": "application/javascript",
  24. ".json": "application/json",
  25. ".jsonld": "application/ld+json",
  26. // --- Images ---
  27. ".jpg": "image/jpeg",
  28. ".jpeg": "image/jpeg",
  29. ".png": "image/png",
  30. ".gif": "image/gif",
  31. ".svg": "image/svg+xml",
  32. ".webp": "image/webp",
  33. ".bmp": "image/bmp",
  34. ".ico": "image/x-icon",
  35. ".tif": "image/tiff",
  36. ".tiff": "image/tiff",
  37. ".avif": "image/avif",
  38. ".heif": "image/heif",
  39. ".heic": "image/heic",
  40. // --- Audio ---
  41. ".mp3": "audio/mpeg",
  42. ".wav": "audio/wav",
  43. ".aac": "audio/aac",
  44. ".flac": "audio/flac",
  45. ".ogg": "audio/ogg",
  46. ".oga": "audio/ogg",
  47. ".m4a": "audio/mp4",
  48. ".mid": "audio/midi",
  49. ".midi": "audio/midi",
  50. // --- Video ---
  51. ".mp4": "video/mp4",
  52. ".mov": "video/quicktime",
  53. ".avi": "video/x-msvideo",
  54. ".wmv": "video/x-ms-wmv",
  55. ".flv": "video/x-flv",
  56. ".mkv": "video/x-matroska",
  57. ".webm": "video/webm",
  58. ".mpeg": "video/mpeg",
  59. ".mpg": "video/mpeg",
  60. // --- Archives ---
  61. ".zip": "application/zip",
  62. ".rar": "application/vnd.rar",
  63. ".tar": "application/x-tar",
  64. ".gz": "application/gzip",
  65. ".tgz": "application/gzip",
  66. ".bz": "application/x-bzip",
  67. ".bz2": "application/x-bzip2",
  68. ".7z": "application/x-7z-compressed",
  69. // --- Documents ---
  70. ".pdf": "application/pdf",
  71. ".doc": "application/msword",
  72. ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  73. ".xls": "application/vnd.ms-excel",
  74. ".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  75. ".ppt": "application/vnd.ms-powerpoint",
  76. ".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  77. // --- Code / Config / Dev ---
  78. ".go": "text/plain",
  79. ".py": "text/x-python",
  80. ".java": "text/x-java-source",
  81. ".c": "text/x-c",
  82. ".cpp": "text/x-c++",
  83. ".h": "text/x-c",
  84. ".hpp": "text/x-c++",
  85. ".rs": "text/plain",
  86. ".php": "text/x-php",
  87. ".sql": "application/sql",
  88. ".env": "text/plain",
  89. ".ini": "text/plain",
  90. // --- Fonts ---
  91. ".woff": "font/woff",
  92. ".woff2": "font/woff2",
  93. ".ttf": "font/ttf",
  94. ".otf": "font/otf",
  95. ".eot": "application/vnd.ms-fontobject",
  96. // --- Vector / CAD ---
  97. ".psd": "image/vnd.adobe.photoshop",
  98. ".ai": "application/postscript",
  99. ".eps": "application/postscript",
  100. // --- Binary / Program ---
  101. ".exe": "application/vnd.microsoft.portable-executable",
  102. ".bin": "application/octet-stream",
  103. ".dll": "application/x-msdownload",
  104. ".apk": "application/vnd.android.package-archive",
  105. ".dmg": "application/x-apple-diskimage",
  106. ".iso": "application/x-iso9660-image",
  107. // --- Misc ---
  108. ".rtf": "application/rtf",
  109. ".wasm": "application/wasm",
  110. ".swf": "application/x-shockwave-flash",
  111. ".map": "application/json",
  112. }
  113. // GetContentType 根据文件名/扩展名获取 Content-Type
  114. func GetContentType(filename string) string {
  115. ext := strings.ToLower(filepath.Ext(filename))
  116. if ext == "" {
  117. return "application/octet-stream"
  118. }
  119. // 1. 标准库优先
  120. if ct := mime.TypeByExtension(ext); ct != "" {
  121. return ct
  122. }
  123. // 2. 补充表
  124. if ct, ok := extraMIMETypes[ext]; ok {
  125. return ct
  126. }
  127. // 3. 默认值
  128. return "application/octet-stream"
  129. }