MimeTypes.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace MediaBrowser.Model.Net
  7. {
  8. /// <summary>
  9. /// Class MimeTypes
  10. /// </summary>
  11. public static class MimeTypes
  12. {
  13. /// <summary>
  14. /// Any extension in this list is considered a video file
  15. /// </summary>
  16. private static readonly HashSet<string> _videoFileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  17. {
  18. ".3gp",
  19. ".asf",
  20. ".avi",
  21. ".divx",
  22. ".dvr-ms",
  23. ".f4v",
  24. ".flv",
  25. ".img",
  26. ".iso",
  27. ".m2t",
  28. ".m2ts",
  29. ".m2v",
  30. ".m4v",
  31. ".mk3d",
  32. ".mkv",
  33. ".mov",
  34. ".mp4",
  35. ".mpg",
  36. ".mpeg",
  37. ".mts",
  38. ".ogg",
  39. ".ogm",
  40. ".ogv",
  41. ".rec",
  42. ".ts",
  43. ".rmvb",
  44. ".webm",
  45. ".wmv",
  46. ".wtv",
  47. };
  48. // http://en.wikipedia.org/wiki/Internet_media_type
  49. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  50. // http://www.iana.org/assignments/media-types/media-types.xhtml
  51. // Add more as needed
  52. private static readonly Dictionary<string, string> _mimeTypeLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
  53. {
  54. // Type application
  55. { ".7z", "application/x-7z-compressed" },
  56. { ".azw", "application/vnd.amazon.ebook" },
  57. { ".azw3", "application/vnd.amazon.ebook" },
  58. { ".cbz", "application/x-cbz" },
  59. { ".cbr", "application/epub+zip" },
  60. { ".eot", "application/vnd.ms-fontobject" },
  61. { ".epub", "application/epub+zip" },
  62. { ".js", "application/x-javascript" },
  63. { ".json", "application/json" },
  64. { ".m3u8", "application/x-mpegURL" },
  65. { ".map", "application/x-javascript" },
  66. { ".mobi", "application/x-mobipocket-ebook" },
  67. { ".pdf", "application/pdf" },
  68. { ".rar", "application/vnd.rar" },
  69. { ".srt", "application/x-subrip" },
  70. { ".ttml", "application/ttml+xml" },
  71. { ".wasm", "application/wasm" },
  72. { ".xml", "application/xml" },
  73. { ".zip", "application/zip" },
  74. // Type image
  75. { ".bmp", "image/bmp" },
  76. { ".gif", "image/gif" },
  77. { ".ico", "image/vnd.microsoft.icon" },
  78. { ".jpg", "image/jpeg" },
  79. { ".jpeg", "image/jpeg" },
  80. { ".png", "image/png" },
  81. { ".svg", "image/svg+xml" },
  82. { ".svgz", "image/svg+xml" },
  83. { ".tbn", "image/jpeg" },
  84. { ".tif", "image/tiff" },
  85. { ".tiff", "image/tiff" },
  86. { ".webp", "image/webp" },
  87. // Type font
  88. { ".ttf" , "font/ttf" },
  89. { ".woff" , "font/woff" },
  90. { ".woff2" , "font/woff2" },
  91. // Type text
  92. { ".ass", "text/x-ssa" },
  93. { ".ssa", "text/x-ssa" },
  94. { ".css", "text/css" },
  95. { ".csv", "text/csv" },
  96. { ".rtf", "text/rtf" },
  97. { ".txt", "text/plain" },
  98. { ".vtt", "text/vtt" },
  99. // Type video
  100. { ".3gp", "video/3gpp" },
  101. { ".3g2", "video/3gpp2" },
  102. { ".asf", "video/x-ms-asf" },
  103. { ".avi", "video/x-msvideo" },
  104. { ".flv", "video/x-flv" },
  105. { ".mp4", "video/mp4" },
  106. { ".m4s", "video/mp4" },
  107. { ".m4v", "video/x-m4v" },
  108. { ".mpegts", "video/mp2t" },
  109. { ".mpg", "video/mpeg" },
  110. { ".mkv", "video/x-matroska" },
  111. { ".mov", "video/quicktime" },
  112. { ".mpd", "video/vnd.mpeg.dash.mpd" },
  113. { ".ogv", "video/ogg" },
  114. { ".ts", "video/mp2t" },
  115. { ".webm", "video/webm" },
  116. { ".wmv", "video/x-ms-wmv" },
  117. // Type audio
  118. { ".aac", "audio/mp4" },
  119. { ".ac3", "audio/ac3" },
  120. { ".ape", "audio/x-ape" },
  121. { ".dsf", "audio/dsf" },
  122. { ".dsp", "audio/dsp" },
  123. { ".flac", "audio/flac" },
  124. { ".m4a", "audio/mp4" },
  125. { ".m4b", "audio/m4b" },
  126. { ".mid", "audio/midi" },
  127. { ".midi", "audio/midi" },
  128. { ".mp3", "audio/mpeg" },
  129. { ".oga", "audio/ogg" },
  130. { ".ogg", "audio/ogg" },
  131. { ".opus", "audio/ogg" },
  132. { ".vorbis", "audio/vorbis" },
  133. { ".wav", "audio/wav" },
  134. { ".webma", "audio/webm" },
  135. { ".wma", "audio/x-ms-wma" },
  136. { ".wv", "audio/x-wavpack" },
  137. { ".xsp", "audio/xsp" },
  138. };
  139. private static readonly Dictionary<string, string> _extensionLookup = CreateExtensionLookup();
  140. private static Dictionary<string, string> CreateExtensionLookup()
  141. {
  142. var dict = _mimeTypeLookup
  143. .GroupBy(i => i.Value)
  144. .ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
  145. dict["image/jpg"] = ".jpg";
  146. dict["image/x-png"] = ".png";
  147. dict["audio/x-aac"] = ".aac";
  148. return dict;
  149. }
  150. public static string GetMimeType(string path) => GetMimeType(path, true);
  151. /// <summary>
  152. /// Gets the type of the MIME.
  153. /// </summary>
  154. public static string GetMimeType(string path, bool enableStreamDefault)
  155. {
  156. if (string.IsNullOrEmpty(path))
  157. {
  158. throw new ArgumentNullException(nameof(path));
  159. }
  160. var ext = Path.GetExtension(path);
  161. if (_mimeTypeLookup.TryGetValue(ext, out string result))
  162. {
  163. return result;
  164. }
  165. // Catch-all for all video types that don't require specific mime types
  166. if (_videoFileExtensions.Contains(ext))
  167. {
  168. return "video/" + ext.Substring(1);
  169. }
  170. // Type text
  171. if (string.Equals(ext, ".html", StringComparison.OrdinalIgnoreCase)
  172. || string.Equals(ext, ".htm", StringComparison.OrdinalIgnoreCase))
  173. {
  174. return "text/html; charset=UTF-8";
  175. }
  176. if (string.Equals(ext, ".log", StringComparison.OrdinalIgnoreCase)
  177. || string.Equals(ext, ".srt", StringComparison.OrdinalIgnoreCase))
  178. {
  179. return "text/plain";
  180. }
  181. // Misc
  182. if (string.Equals(ext, ".dll", StringComparison.OrdinalIgnoreCase))
  183. {
  184. return "application/octet-stream";
  185. }
  186. return enableStreamDefault ? "application/octet-stream" : null;
  187. }
  188. public static string ToExtension(string mimeType)
  189. {
  190. if (string.IsNullOrEmpty(mimeType))
  191. {
  192. throw new ArgumentNullException(nameof(mimeType));
  193. }
  194. // handle text/html; charset=UTF-8
  195. mimeType = mimeType.Split(';')[0];
  196. if (_extensionLookup.TryGetValue(mimeType, out string result))
  197. {
  198. return result;
  199. }
  200. return null;
  201. }
  202. }
  203. }