MimeTypes.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Model.Extensions;
  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 - can be added to at runtime for extensibility
  15. /// </summary>
  16. private static readonly string[] VideoFileExtensions = new string[]
  17. {
  18. ".mkv",
  19. ".m2t",
  20. ".m2ts",
  21. ".img",
  22. ".iso",
  23. ".mk3d",
  24. ".ts",
  25. ".rmvb",
  26. ".mov",
  27. ".avi",
  28. ".mpg",
  29. ".mpeg",
  30. ".wmv",
  31. ".mp4",
  32. ".divx",
  33. ".dvr-ms",
  34. ".wtv",
  35. ".ogm",
  36. ".ogv",
  37. ".asf",
  38. ".m4v",
  39. ".flv",
  40. ".f4v",
  41. ".3gp",
  42. ".webm",
  43. ".mts",
  44. ".m2v",
  45. ".rec"
  46. };
  47. private static Dictionary<string, string> GetVideoFileExtensionsDictionary()
  48. {
  49. var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  50. foreach (string ext in VideoFileExtensions)
  51. {
  52. dict[ext] = ext;
  53. }
  54. return dict;
  55. }
  56. private static readonly Dictionary<string, string> VideoFileExtensionsDictionary = GetVideoFileExtensionsDictionary();
  57. // http://en.wikipedia.org/wiki/Internet_media_type
  58. // Add more as needed
  59. private static Dictionary<string, string> GetMimeTypeLookup()
  60. {
  61. var dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  62. dict.Add(".jpg", "image/jpeg");
  63. dict.Add(".jpeg", "image/jpeg");
  64. dict.Add(".tbn", "image/jpeg");
  65. dict.Add(".png", "image/png");
  66. dict.Add(".gif", "image/gif");
  67. dict.Add(".tiff", "image/tiff");
  68. dict.Add(".webp", "image/webp");
  69. dict.Add(".ico", "image/vnd.microsoft.icon");
  70. dict.Add(".mpg", "video/mpeg");
  71. dict.Add(".mpeg", "video/mpeg");
  72. dict.Add(".ogv", "video/ogg");
  73. dict.Add(".mov", "video/quicktime");
  74. dict.Add(".webm", "video/webm");
  75. dict.Add(".mkv", "video/x-matroska");
  76. dict.Add(".wmv", "video/x-ms-wmv");
  77. dict.Add(".flv", "video/x-flv");
  78. dict.Add(".avi", "video/x-msvideo");
  79. dict.Add(".asf", "video/x-ms-asf");
  80. dict.Add(".m4v", "video/x-m4v");
  81. dict.Add(".m4s", "video/mp4");
  82. dict.Add(".cbz", "application/x-cbz");
  83. dict.Add(".cbr", "application/epub+zip");
  84. dict.Add(".epub", "application/epub+zip");
  85. dict.Add(".pdf", "application/pdf");
  86. dict.Add(".mobi", "application/x-mobipocket-ebook");
  87. dict.Add(".ass", "text/x-ssa");
  88. dict.Add(".ssa", "text/x-ssa");
  89. return dict;
  90. }
  91. private static readonly Dictionary<string, string> MimeTypeLookup = GetMimeTypeLookup();
  92. private static readonly Dictionary<string, string> ExtensionLookup = CreateExtensionLookup();
  93. private static Dictionary<string, string> CreateExtensionLookup()
  94. {
  95. var dict = MimeTypeLookup
  96. .GroupBy(i => i.Value)
  97. .ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
  98. dict["image/jpg"] = ".jpg";
  99. dict["image/x-png"] = ".png";
  100. return dict;
  101. }
  102. public static string GetMimeType(string path)
  103. {
  104. return GetMimeType(path, true);
  105. }
  106. /// <summary>
  107. /// Gets the type of the MIME.
  108. /// </summary>
  109. public static string GetMimeType(string path, bool enableStreamDefault)
  110. {
  111. if (string.IsNullOrEmpty(path))
  112. {
  113. throw new ArgumentNullException(nameof(path));
  114. }
  115. var ext = Path.GetExtension(path) ?? string.Empty;
  116. if (MimeTypeLookup.TryGetValue(ext, out string result))
  117. {
  118. return result;
  119. }
  120. // Type video
  121. if (StringHelper.EqualsIgnoreCase(ext, ".3gp"))
  122. {
  123. return "video/3gpp";
  124. }
  125. if (StringHelper.EqualsIgnoreCase(ext, ".3g2"))
  126. {
  127. return "video/3gpp2";
  128. }
  129. if (StringHelper.EqualsIgnoreCase(ext, ".ts"))
  130. {
  131. return "video/mp2t";
  132. }
  133. if (StringHelper.EqualsIgnoreCase(ext, ".mpd"))
  134. {
  135. return "video/vnd.mpeg.dash.mpd";
  136. }
  137. // Catch-all for all video types that don't require specific mime types
  138. if (VideoFileExtensionsDictionary.ContainsKey(ext))
  139. {
  140. return "video/" + ext.TrimStart('.').ToLowerInvariant();
  141. }
  142. // Type text
  143. if (StringHelper.EqualsIgnoreCase(ext, ".css"))
  144. {
  145. return "text/css";
  146. }
  147. if (StringHelper.EqualsIgnoreCase(ext, ".csv"))
  148. {
  149. return "text/csv";
  150. }
  151. if (StringHelper.EqualsIgnoreCase(ext, ".html"))
  152. {
  153. return "text/html; charset=UTF-8";
  154. }
  155. if (StringHelper.EqualsIgnoreCase(ext, ".htm"))
  156. {
  157. return "text/html; charset=UTF-8";
  158. }
  159. if (StringHelper.EqualsIgnoreCase(ext, ".txt"))
  160. {
  161. return "text/plain";
  162. }
  163. if (StringHelper.EqualsIgnoreCase(ext, ".log"))
  164. {
  165. return "text/plain";
  166. }
  167. if (StringHelper.EqualsIgnoreCase(ext, ".xml"))
  168. {
  169. return "application/xml";
  170. }
  171. // Type audio
  172. if (StringHelper.EqualsIgnoreCase(ext, ".mp3"))
  173. {
  174. return "audio/mpeg";
  175. }
  176. if (StringHelper.EqualsIgnoreCase(ext, ".m4a"))
  177. {
  178. return "audio/mp4";
  179. }
  180. if (StringHelper.EqualsIgnoreCase(ext, ".aac"))
  181. {
  182. return "audio/mp4";
  183. }
  184. if (StringHelper.EqualsIgnoreCase(ext, ".webma"))
  185. {
  186. return "audio/webm";
  187. }
  188. if (StringHelper.EqualsIgnoreCase(ext, ".wav"))
  189. {
  190. return "audio/wav";
  191. }
  192. if (StringHelper.EqualsIgnoreCase(ext, ".wma"))
  193. {
  194. return "audio/x-ms-wma";
  195. }
  196. if (StringHelper.EqualsIgnoreCase(ext, ".flac"))
  197. {
  198. return "audio/flac";
  199. }
  200. if (StringHelper.EqualsIgnoreCase(ext, ".aac"))
  201. {
  202. return "audio/x-aac";
  203. }
  204. if (StringHelper.EqualsIgnoreCase(ext, ".ogg"))
  205. {
  206. return "audio/ogg";
  207. }
  208. if (StringHelper.EqualsIgnoreCase(ext, ".oga"))
  209. {
  210. return "audio/ogg";
  211. }
  212. if (StringHelper.EqualsIgnoreCase(ext, ".opus"))
  213. {
  214. return "audio/ogg";
  215. }
  216. if (StringHelper.EqualsIgnoreCase(ext, ".ac3"))
  217. {
  218. return "audio/ac3";
  219. }
  220. if (StringHelper.EqualsIgnoreCase(ext, ".dsf"))
  221. {
  222. return "audio/dsf";
  223. }
  224. if (StringHelper.EqualsIgnoreCase(ext, ".m4b"))
  225. {
  226. return "audio/m4b";
  227. }
  228. if (StringHelper.EqualsIgnoreCase(ext, ".xsp"))
  229. {
  230. return "audio/xsp";
  231. }
  232. if (StringHelper.EqualsIgnoreCase(ext, ".dsp"))
  233. {
  234. return "audio/dsp";
  235. }
  236. // Playlists
  237. if (StringHelper.EqualsIgnoreCase(ext, ".m3u8"))
  238. {
  239. return "application/x-mpegURL";
  240. }
  241. // Misc
  242. if (StringHelper.EqualsIgnoreCase(ext, ".dll"))
  243. {
  244. return "application/octet-stream";
  245. }
  246. // Web
  247. if (StringHelper.EqualsIgnoreCase(ext, ".js"))
  248. {
  249. return "application/x-javascript";
  250. }
  251. if (StringHelper.EqualsIgnoreCase(ext, ".json"))
  252. {
  253. return "application/json";
  254. }
  255. if (StringHelper.EqualsIgnoreCase(ext, ".map"))
  256. {
  257. return "application/x-javascript";
  258. }
  259. if (StringHelper.EqualsIgnoreCase(ext, ".woff"))
  260. {
  261. return "font/woff";
  262. }
  263. if (StringHelper.EqualsIgnoreCase(ext, ".ttf"))
  264. {
  265. return "font/ttf";
  266. }
  267. if (StringHelper.EqualsIgnoreCase(ext, ".eot"))
  268. {
  269. return "application/vnd.ms-fontobject";
  270. }
  271. if (StringHelper.EqualsIgnoreCase(ext, ".svg"))
  272. {
  273. return "image/svg+xml";
  274. }
  275. if (StringHelper.EqualsIgnoreCase(ext, ".svgz"))
  276. {
  277. return "image/svg+xml";
  278. }
  279. if (StringHelper.EqualsIgnoreCase(ext, ".srt"))
  280. {
  281. return "text/plain";
  282. }
  283. if (StringHelper.EqualsIgnoreCase(ext, ".vtt"))
  284. {
  285. return "text/vtt";
  286. }
  287. if (StringHelper.EqualsIgnoreCase(ext, ".ttml"))
  288. {
  289. return "application/ttml+xml";
  290. }
  291. if (enableStreamDefault)
  292. {
  293. return "application/octet-stream";
  294. }
  295. return null;
  296. }
  297. public static string ToExtension(string mimeType)
  298. {
  299. if (string.IsNullOrEmpty(mimeType))
  300. {
  301. throw new ArgumentNullException(nameof(mimeType));
  302. }
  303. // handle text/html; charset=UTF-8
  304. mimeType = mimeType.Split(';')[0];
  305. if (ExtensionLookup.TryGetValue(mimeType, out string result))
  306. {
  307. return result;
  308. }
  309. return null;
  310. }
  311. }
  312. }