MimeTypes.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using MediaBrowser.Model.Extensions;
  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 - 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. Dictionary<string, string> 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. Dictionary<string, string> 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. return dict;
  82. }
  83. private static readonly Dictionary<string, string> MimeTypeLookup = GetMimeTypeLookup();
  84. private static readonly Dictionary<string, string> ExtensionLookup = CreateExtensionLookup();
  85. private static Dictionary<string, string> CreateExtensionLookup()
  86. {
  87. var dict = MimeTypeLookup
  88. .GroupBy(i => i.Value)
  89. .ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
  90. dict["image/jpg"] = ".jpg";
  91. dict["image/x-png"] = ".png";
  92. return dict;
  93. }
  94. public static string GetMimeType(string path)
  95. {
  96. return GetMimeType(path, true);
  97. }
  98. /// <summary>
  99. /// Gets the type of the MIME.
  100. /// </summary>
  101. public static string GetMimeType(string path, bool enableStreamDefault)
  102. {
  103. if (string.IsNullOrEmpty(path))
  104. {
  105. throw new ArgumentNullException("path");
  106. }
  107. var ext = Path.GetExtension(path) ?? string.Empty;
  108. string result;
  109. if (MimeTypeLookup.TryGetValue(ext, out result))
  110. {
  111. return result;
  112. }
  113. // Type video
  114. if (StringHelper.EqualsIgnoreCase(ext, ".3gp"))
  115. {
  116. return "video/3gpp";
  117. }
  118. if (StringHelper.EqualsIgnoreCase(ext, ".3g2"))
  119. {
  120. return "video/3gpp2";
  121. }
  122. if (StringHelper.EqualsIgnoreCase(ext, ".ts"))
  123. {
  124. return "video/mp2t";
  125. }
  126. if (StringHelper.EqualsIgnoreCase(ext, ".mpd"))
  127. {
  128. return "video/vnd.mpeg.dash.mpd";
  129. }
  130. // Catch-all for all video types that don't require specific mime types
  131. if (VideoFileExtensionsDictionary.ContainsKey(ext))
  132. {
  133. return "video/" + ext.TrimStart('.').ToLower();
  134. }
  135. // Type text
  136. if (StringHelper.EqualsIgnoreCase(ext, ".css"))
  137. {
  138. return "text/css";
  139. }
  140. if (StringHelper.EqualsIgnoreCase(ext, ".csv"))
  141. {
  142. return "text/csv";
  143. }
  144. if (StringHelper.EqualsIgnoreCase(ext, ".html"))
  145. {
  146. return "text/html; charset=UTF-8";
  147. }
  148. if (StringHelper.EqualsIgnoreCase(ext, ".htm"))
  149. {
  150. return "text/html; charset=UTF-8";
  151. }
  152. if (StringHelper.EqualsIgnoreCase(ext, ".txt"))
  153. {
  154. return "text/plain";
  155. }
  156. if (StringHelper.EqualsIgnoreCase(ext, ".xml"))
  157. {
  158. return "application/xml";
  159. }
  160. // Type document
  161. if (StringHelper.EqualsIgnoreCase(ext, ".pdf"))
  162. {
  163. return "application/pdf";
  164. }
  165. if (StringHelper.EqualsIgnoreCase(ext, ".mobi"))
  166. {
  167. return "application/x-mobipocket-ebook";
  168. }
  169. if (StringHelper.EqualsIgnoreCase(ext, ".epub"))
  170. {
  171. return "application/epub+zip";
  172. }
  173. if (StringHelper.EqualsIgnoreCase(ext, ".cbz"))
  174. {
  175. return "application/epub+zip";
  176. }
  177. if (StringHelper.EqualsIgnoreCase(ext, ".cbr"))
  178. {
  179. return "application/epub+zip";
  180. }
  181. // Type audio
  182. if (StringHelper.EqualsIgnoreCase(ext, ".mp3"))
  183. {
  184. return "audio/mpeg";
  185. }
  186. if (StringHelper.EqualsIgnoreCase(ext, ".m4a"))
  187. {
  188. return "audio/mp4";
  189. }
  190. if (StringHelper.EqualsIgnoreCase(ext, ".aac"))
  191. {
  192. return "audio/mp4";
  193. }
  194. if (StringHelper.EqualsIgnoreCase(ext, ".webma"))
  195. {
  196. return "audio/webm";
  197. }
  198. if (StringHelper.EqualsIgnoreCase(ext, ".wav"))
  199. {
  200. return "audio/wav";
  201. }
  202. if (StringHelper.EqualsIgnoreCase(ext, ".wma"))
  203. {
  204. return "audio/x-ms-wma";
  205. }
  206. if (StringHelper.EqualsIgnoreCase(ext, ".flac"))
  207. {
  208. return "audio/flac";
  209. }
  210. if (StringHelper.EqualsIgnoreCase(ext, ".aac"))
  211. {
  212. return "audio/x-aac";
  213. }
  214. if (StringHelper.EqualsIgnoreCase(ext, ".ogg"))
  215. {
  216. return "audio/ogg";
  217. }
  218. if (StringHelper.EqualsIgnoreCase(ext, ".oga"))
  219. {
  220. return "audio/ogg";
  221. }
  222. if (StringHelper.EqualsIgnoreCase(ext, ".opus"))
  223. {
  224. return "audio/ogg";
  225. }
  226. if (StringHelper.EqualsIgnoreCase(ext, ".ac3"))
  227. {
  228. return "audio/ac3";
  229. }
  230. if (StringHelper.EqualsIgnoreCase(ext, ".dsf"))
  231. {
  232. return "audio/dsf";
  233. }
  234. if (StringHelper.EqualsIgnoreCase(ext, ".m4b"))
  235. {
  236. return "audio/m4b";
  237. }
  238. if (StringHelper.EqualsIgnoreCase(ext, ".xsp"))
  239. {
  240. return "audio/xsp";
  241. }
  242. if (StringHelper.EqualsIgnoreCase(ext, ".dsp"))
  243. {
  244. return "audio/dsp";
  245. }
  246. // Playlists
  247. if (StringHelper.EqualsIgnoreCase(ext, ".m3u8"))
  248. {
  249. return "application/x-mpegURL";
  250. }
  251. // Misc
  252. if (StringHelper.EqualsIgnoreCase(ext, ".dll"))
  253. {
  254. return "application/octet-stream";
  255. }
  256. // Web
  257. if (StringHelper.EqualsIgnoreCase(ext, ".js"))
  258. {
  259. return "application/x-javascript";
  260. }
  261. if (StringHelper.EqualsIgnoreCase(ext, ".json"))
  262. {
  263. return "application/json";
  264. }
  265. if (StringHelper.EqualsIgnoreCase(ext, ".map"))
  266. {
  267. return "application/x-javascript";
  268. }
  269. if (StringHelper.EqualsIgnoreCase(ext, ".woff"))
  270. {
  271. return "font/woff";
  272. }
  273. if (StringHelper.EqualsIgnoreCase(ext, ".ttf"))
  274. {
  275. return "font/ttf";
  276. }
  277. if (StringHelper.EqualsIgnoreCase(ext, ".eot"))
  278. {
  279. return "application/vnd.ms-fontobject";
  280. }
  281. if (StringHelper.EqualsIgnoreCase(ext, ".svg"))
  282. {
  283. return "image/svg+xml";
  284. }
  285. if (StringHelper.EqualsIgnoreCase(ext, ".svgz"))
  286. {
  287. return "image/svg+xml";
  288. }
  289. if (StringHelper.EqualsIgnoreCase(ext, ".srt"))
  290. {
  291. return "text/plain";
  292. }
  293. if (StringHelper.EqualsIgnoreCase(ext, ".vtt"))
  294. {
  295. return "text/vtt";
  296. }
  297. if (StringHelper.EqualsIgnoreCase(ext, ".ttml"))
  298. {
  299. return "application/ttml+xml";
  300. }
  301. if (enableStreamDefault)
  302. {
  303. return "application/octet-stream";
  304. }
  305. return null;
  306. }
  307. public static string ToExtension(string mimeType)
  308. {
  309. if (string.IsNullOrEmpty(mimeType))
  310. {
  311. throw new ArgumentNullException("mimeType");
  312. }
  313. // handle text/html; charset=UTF-8
  314. mimeType = mimeType.Split(';')[0];
  315. string result;
  316. if (ExtensionLookup.TryGetValue(mimeType, out result))
  317. {
  318. return result;
  319. }
  320. return null;
  321. }
  322. }
  323. }