MimeTypes.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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
  15. /// </summary>
  16. private static readonly HashSet<string> _videoFileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  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. // http://en.wikipedia.org/wiki/Internet_media_type
  48. // Add more as needed
  49. private static readonly Dictionary<string, string> _mimeTypeLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
  50. {
  51. // Type application
  52. { ".cbz", "application/x-cbz" },
  53. { ".cbr", "application/epub+zip" },
  54. { ".eot", "application/vnd.ms-fontobject" },
  55. { ".epub", "application/epub+zip" },
  56. { ".js", "application/x-javascript" },
  57. { ".json", "application/json" },
  58. { ".map", "application/x-javascript" },
  59. { ".pdf", "application/pdf" },
  60. { ".ttml", "application/ttml+xml" },
  61. { ".m3u8", "application/x-mpegURL" },
  62. { ".mobi", "application/x-mobipocket-ebook" },
  63. { ".xml", "application/xml" },
  64. // Type image
  65. { ".jpg", "image/jpeg" },
  66. { ".jpeg", "image/jpeg" },
  67. { ".tbn", "image/jpeg" },
  68. { ".png", "image/png" },
  69. { ".gif", "image/gif" },
  70. { ".tiff", "image/tiff" },
  71. { ".webp", "image/webp" },
  72. { ".ico", "image/vnd.microsoft.icon" },
  73. { ".svg", "image/svg+xml" },
  74. { ".svgz", "image/svg+xml" },
  75. // Type font
  76. { ".ttf" , "font/ttf" },
  77. { ".woff" , "font/woff" },
  78. // Type text
  79. { ".ass", "text/x-ssa" },
  80. { ".ssa", "text/x-ssa" },
  81. { ".css", "text/css" },
  82. { ".csv", "text/csv" },
  83. { ".txt", "text/plain" },
  84. { ".vtt", "text/vtt" },
  85. // Type video
  86. { ".mpg", "video/mpeg" },
  87. { ".ogv", "video/ogg" },
  88. { ".mov", "video/quicktime" },
  89. { ".webm", "video/webm" },
  90. { ".mkv", "video/x-matroska" },
  91. { ".wmv", "video/x-ms-wmv" },
  92. { ".flv", "video/x-flv" },
  93. { ".avi", "video/x-msvideo" },
  94. { ".asf", "video/x-ms-asf" },
  95. { ".m4v", "video/x-m4v" },
  96. { ".m4s", "video/mp4" },
  97. { ".3gp", "video/3gpp" },
  98. { ".3g2", "video/3gpp2" },
  99. { ".mpd", "video/vnd.mpeg.dash.mpd" },
  100. { ".ts", "video/mp2t" },
  101. // Type audio
  102. { ".mp3", "audio/mpeg" },
  103. { ".m4a", "audio/mp4" },
  104. { ".aac", "audio/mp4" },
  105. { ".webma", "audio/webm" },
  106. { ".wav", "audio/wav" },
  107. { ".wma", "audio/x-ms-wma" },
  108. { ".ogg", "audio/ogg" },
  109. { ".oga", "audio/ogg" },
  110. { ".opus", "audio/ogg" },
  111. { ".ac3", "audio/ac3" },
  112. { ".dsf", "audio/dsf" },
  113. { ".m4b", "audio/m4b" },
  114. { ".xsp", "audio/xsp" },
  115. { ".dsp", "audio/dsp" },
  116. { ".flac", "audio/flac" },
  117. };
  118. private static readonly Dictionary<string, string> _extensionLookup = CreateExtensionLookup();
  119. private static Dictionary<string, string> CreateExtensionLookup()
  120. {
  121. var dict = _mimeTypeLookup
  122. .GroupBy(i => i.Value)
  123. .ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
  124. dict["image/jpg"] = ".jpg";
  125. dict["image/x-png"] = ".png";
  126. dict["audio/x-aac"] = ".aac";
  127. return dict;
  128. }
  129. public static string GetMimeType(string path) => GetMimeType(path, true);
  130. /// <summary>
  131. /// Gets the type of the MIME.
  132. /// </summary>
  133. public static string GetMimeType(string path, bool enableStreamDefault)
  134. {
  135. if (string.IsNullOrEmpty(path))
  136. {
  137. throw new ArgumentNullException(nameof(path));
  138. }
  139. var ext = Path.GetExtension(path);
  140. if (_mimeTypeLookup.TryGetValue(ext, out string result))
  141. {
  142. return result;
  143. }
  144. // Catch-all for all video types that don't require specific mime types
  145. if (_videoFileExtensions.Contains(ext))
  146. {
  147. return "video/" + ext.Substring(1);
  148. }
  149. // Type text
  150. if (StringHelper.EqualsIgnoreCase(ext, ".html")
  151. || StringHelper.EqualsIgnoreCase(ext, ".htm"))
  152. {
  153. return "text/html; charset=UTF-8";
  154. }
  155. if (StringHelper.EqualsIgnoreCase(ext, ".log")
  156. || StringHelper.EqualsIgnoreCase(ext, ".srt"))
  157. {
  158. return "text/plain";
  159. }
  160. // Misc
  161. if (StringHelper.EqualsIgnoreCase(ext, ".dll"))
  162. {
  163. return "application/octet-stream";
  164. }
  165. return enableStreamDefault ? "application/octet-stream" : null;
  166. }
  167. public static string ToExtension(string mimeType)
  168. {
  169. if (string.IsNullOrEmpty(mimeType))
  170. {
  171. throw new ArgumentNullException(nameof(mimeType));
  172. }
  173. // handle text/html; charset=UTF-8
  174. mimeType = mimeType.Split(';')[0];
  175. if (_extensionLookup.TryGetValue(mimeType, out string result))
  176. {
  177. return result;
  178. }
  179. return null;
  180. }
  181. }
  182. }