MimeTypes.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. { ".opf", "application/oebps-package+xml" },
  68. { ".pdf", "application/pdf" },
  69. { ".rar", "application/vnd.rar" },
  70. { ".srt", "application/x-subrip" },
  71. { ".ttml", "application/ttml+xml" },
  72. { ".wasm", "application/wasm" },
  73. { ".xml", "application/xml" },
  74. { ".zip", "application/zip" },
  75. // Type image
  76. { ".bmp", "image/bmp" },
  77. { ".gif", "image/gif" },
  78. { ".ico", "image/vnd.microsoft.icon" },
  79. { ".jpg", "image/jpeg" },
  80. { ".jpeg", "image/jpeg" },
  81. { ".png", "image/png" },
  82. { ".svg", "image/svg+xml" },
  83. { ".svgz", "image/svg+xml" },
  84. { ".tbn", "image/jpeg" },
  85. { ".tif", "image/tiff" },
  86. { ".tiff", "image/tiff" },
  87. { ".webp", "image/webp" },
  88. // Type font
  89. { ".ttf", "font/ttf" },
  90. { ".woff", "font/woff" },
  91. { ".woff2", "font/woff2" },
  92. // Type text
  93. { ".ass", "text/x-ssa" },
  94. { ".ssa", "text/x-ssa" },
  95. { ".css", "text/css" },
  96. { ".csv", "text/csv" },
  97. { ".edl", "text/plain" },
  98. { ".rtf", "text/rtf" },
  99. { ".txt", "text/plain" },
  100. { ".vtt", "text/vtt" },
  101. // Type video
  102. { ".3gp", "video/3gpp" },
  103. { ".3g2", "video/3gpp2" },
  104. { ".asf", "video/x-ms-asf" },
  105. { ".avi", "video/x-msvideo" },
  106. { ".flv", "video/x-flv" },
  107. { ".mp4", "video/mp4" },
  108. { ".m4s", "video/mp4" },
  109. { ".m4v", "video/x-m4v" },
  110. { ".mpegts", "video/mp2t" },
  111. { ".mpg", "video/mpeg" },
  112. { ".mkv", "video/x-matroska" },
  113. { ".mov", "video/quicktime" },
  114. { ".mpd", "video/vnd.mpeg.dash.mpd" },
  115. { ".ogv", "video/ogg" },
  116. { ".ts", "video/mp2t" },
  117. { ".webm", "video/webm" },
  118. { ".wmv", "video/x-ms-wmv" },
  119. // Type audio
  120. { ".aac", "audio/aac" },
  121. { ".ac3", "audio/ac3" },
  122. { ".ape", "audio/x-ape" },
  123. { ".dsf", "audio/dsf" },
  124. { ".dsp", "audio/dsp" },
  125. { ".flac", "audio/flac" },
  126. { ".m4a", "audio/mp4" },
  127. { ".m4b", "audio/m4b" },
  128. { ".mid", "audio/midi" },
  129. { ".midi", "audio/midi" },
  130. { ".mp3", "audio/mpeg" },
  131. { ".oga", "audio/ogg" },
  132. { ".ogg", "audio/ogg" },
  133. { ".opus", "audio/ogg" },
  134. { ".vorbis", "audio/vorbis" },
  135. { ".wav", "audio/wav" },
  136. { ".webma", "audio/webm" },
  137. { ".wma", "audio/x-ms-wma" },
  138. { ".wv", "audio/x-wavpack" },
  139. { ".xsp", "audio/xsp" },
  140. };
  141. private static readonly Dictionary<string, string> _extensionLookup = CreateExtensionLookup();
  142. private static Dictionary<string, string> CreateExtensionLookup()
  143. {
  144. var dict = _mimeTypeLookup
  145. .GroupBy(i => i.Value)
  146. .ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
  147. dict["image/jpg"] = ".jpg";
  148. dict["image/x-png"] = ".png";
  149. dict["audio/x-aac"] = ".aac";
  150. return dict;
  151. }
  152. public static string? GetMimeType(string path) => GetMimeType(path, true);
  153. /// <summary>
  154. /// Gets the type of the MIME.
  155. /// </summary>
  156. /// <param name="filename">The filename to find the MIME type of.</param>
  157. /// <param name="enableStreamDefault">Whether of not to return a default value if no fitting MIME type is found.</param>
  158. /// <returns>The worrect MIME type for the given filename, or `null` if it wasn't found and <paramref name="enableStreamDefault"/> is false.</returns>
  159. public static string? GetMimeType(string filename, bool enableStreamDefault)
  160. {
  161. if (filename.Length == 0)
  162. {
  163. throw new ArgumentException("String can't be empty.", nameof(filename));
  164. }
  165. var ext = Path.GetExtension(filename);
  166. if (_mimeTypeLookup.TryGetValue(ext, out string? result))
  167. {
  168. return result;
  169. }
  170. // Catch-all for all video types that don't require specific mime types
  171. if (_videoFileExtensions.Contains(ext))
  172. {
  173. return "video/" + ext.Substring(1);
  174. }
  175. // Type text
  176. if (string.Equals(ext, ".html", StringComparison.OrdinalIgnoreCase)
  177. || string.Equals(ext, ".htm", StringComparison.OrdinalIgnoreCase))
  178. {
  179. return "text/html; charset=UTF-8";
  180. }
  181. if (string.Equals(ext, ".log", StringComparison.OrdinalIgnoreCase)
  182. || string.Equals(ext, ".srt", StringComparison.OrdinalIgnoreCase))
  183. {
  184. return "text/plain";
  185. }
  186. // Misc
  187. if (string.Equals(ext, ".dll", StringComparison.OrdinalIgnoreCase))
  188. {
  189. return "application/octet-stream";
  190. }
  191. return enableStreamDefault ? "application/octet-stream" : null;
  192. }
  193. public static string? ToExtension(string mimeType)
  194. {
  195. if (mimeType.Length == 0)
  196. {
  197. throw new ArgumentException("String can't be empty.", nameof(mimeType));
  198. }
  199. // handle text/html; charset=UTF-8
  200. mimeType = mimeType.Split(';')[0];
  201. if (_extensionLookup.TryGetValue(mimeType, out string? result))
  202. {
  203. return result;
  204. }
  205. return null;
  206. }
  207. }
  208. }