2
0

MimeTypes.cs 10 KB

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