MimeTypes.cs 10 KB

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