MimeTypes.cs 9.8 KB

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