MimeTypes.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. namespace MediaBrowser.Model.Net
  8. {
  9. /// <summary>
  10. /// Class MimeTypes
  11. /// </summary>
  12. public static class MimeTypes
  13. {
  14. /// <summary>
  15. /// Any extension in this list is considered a video file
  16. /// </summary>
  17. private static readonly HashSet<string> _videoFileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  18. {
  19. ".mkv",
  20. ".m2t",
  21. ".m2ts",
  22. ".img",
  23. ".iso",
  24. ".mk3d",
  25. ".ts",
  26. ".rmvb",
  27. ".mov",
  28. ".avi",
  29. ".mpg",
  30. ".mpeg",
  31. ".wmv",
  32. ".mp4",
  33. ".divx",
  34. ".dvr-ms",
  35. ".wtv",
  36. ".ogm",
  37. ".ogv",
  38. ".asf",
  39. ".m4v",
  40. ".flv",
  41. ".f4v",
  42. ".3gp",
  43. ".webm",
  44. ".mts",
  45. ".m2v",
  46. ".rec"
  47. };
  48. // http://en.wikipedia.org/wiki/Internet_media_type
  49. // Add more as needed
  50. private static readonly Dictionary<string, string> _mimeTypeLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
  51. {
  52. // Type application
  53. { ".cbz", "application/x-cbz" },
  54. { ".cbr", "application/epub+zip" },
  55. { ".eot", "application/vnd.ms-fontobject" },
  56. { ".epub", "application/epub+zip" },
  57. { ".js", "application/x-javascript" },
  58. { ".json", "application/json" },
  59. { ".map", "application/x-javascript" },
  60. { ".pdf", "application/pdf" },
  61. { ".ttml", "application/ttml+xml" },
  62. { ".m3u8", "application/x-mpegURL" },
  63. { ".mobi", "application/x-mobipocket-ebook" },
  64. { ".xml", "application/xml" },
  65. // Type image
  66. { ".jpg", "image/jpeg" },
  67. { ".jpeg", "image/jpeg" },
  68. { ".tbn", "image/jpeg" },
  69. { ".png", "image/png" },
  70. { ".gif", "image/gif" },
  71. { ".tiff", "image/tiff" },
  72. { ".webp", "image/webp" },
  73. { ".ico", "image/vnd.microsoft.icon" },
  74. { ".svg", "image/svg+xml" },
  75. { ".svgz", "image/svg+xml" },
  76. // Type font
  77. { ".ttf" , "font/ttf" },
  78. { ".woff" , "font/woff" },
  79. // Type text
  80. { ".ass", "text/x-ssa" },
  81. { ".ssa", "text/x-ssa" },
  82. { ".css", "text/css" },
  83. { ".csv", "text/csv" },
  84. { ".txt", "text/plain" },
  85. { ".vtt", "text/vtt" },
  86. // Type video
  87. { ".mpg", "video/mpeg" },
  88. { ".ogv", "video/ogg" },
  89. { ".mov", "video/quicktime" },
  90. { ".webm", "video/webm" },
  91. { ".mkv", "video/x-matroska" },
  92. { ".wmv", "video/x-ms-wmv" },
  93. { ".flv", "video/x-flv" },
  94. { ".avi", "video/x-msvideo" },
  95. { ".asf", "video/x-ms-asf" },
  96. { ".m4v", "video/x-m4v" },
  97. { ".m4s", "video/mp4" },
  98. { ".3gp", "video/3gpp" },
  99. { ".3g2", "video/3gpp2" },
  100. { ".mpd", "video/vnd.mpeg.dash.mpd" },
  101. { ".ts", "video/mp2t" },
  102. // Type audio
  103. { ".mp3", "audio/mpeg" },
  104. { ".m4a", "audio/mp4" },
  105. { ".aac", "audio/mp4" },
  106. { ".webma", "audio/webm" },
  107. { ".wav", "audio/wav" },
  108. { ".wma", "audio/x-ms-wma" },
  109. { ".ogg", "audio/ogg" },
  110. { ".oga", "audio/ogg" },
  111. { ".opus", "audio/ogg" },
  112. { ".ac3", "audio/ac3" },
  113. { ".dsf", "audio/dsf" },
  114. { ".m4b", "audio/m4b" },
  115. { ".xsp", "audio/xsp" },
  116. { ".dsp", "audio/dsp" },
  117. { ".flac", "audio/flac" },
  118. };
  119. private static readonly Dictionary<string, string> _extensionLookup = CreateExtensionLookup();
  120. private static Dictionary<string, string> CreateExtensionLookup()
  121. {
  122. var dict = _mimeTypeLookup
  123. .GroupBy(i => i.Value)
  124. .ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
  125. dict["image/jpg"] = ".jpg";
  126. dict["image/x-png"] = ".png";
  127. dict["audio/x-aac"] = ".aac";
  128. return dict;
  129. }
  130. public static string GetMimeType(string path) => GetMimeType(path, true);
  131. /// <summary>
  132. /// Gets the type of the MIME.
  133. /// </summary>
  134. public static string GetMimeType(string path, bool enableStreamDefault)
  135. {
  136. if (string.IsNullOrEmpty(path))
  137. {
  138. throw new ArgumentNullException(nameof(path));
  139. }
  140. var ext = Path.GetExtension(path);
  141. if (_mimeTypeLookup.TryGetValue(ext, out string result))
  142. {
  143. return result;
  144. }
  145. // Catch-all for all video types that don't require specific mime types
  146. if (_videoFileExtensions.Contains(ext))
  147. {
  148. return "video/" + ext.Substring(1);
  149. }
  150. // Type text
  151. if (string.Equals(ext, ".html", StringComparison.OrdinalIgnoreCase)
  152. || string.Equals(ext, ".htm", StringComparison.OrdinalIgnoreCase))
  153. {
  154. return "text/html; charset=UTF-8";
  155. }
  156. if (string.Equals(ext, ".log", StringComparison.OrdinalIgnoreCase)
  157. || string.Equals(ext, ".srt", StringComparison.OrdinalIgnoreCase))
  158. {
  159. return "text/plain";
  160. }
  161. // Misc
  162. if (string.Equals(ext, ".dll", StringComparison.OrdinalIgnoreCase))
  163. {
  164. return "application/octet-stream";
  165. }
  166. return enableStreamDefault ? "application/octet-stream" : null;
  167. }
  168. public static string ToExtension(string mimeType)
  169. {
  170. if (string.IsNullOrEmpty(mimeType))
  171. {
  172. throw new ArgumentNullException(nameof(mimeType));
  173. }
  174. // handle text/html; charset=UTF-8
  175. mimeType = mimeType.Split(';')[0];
  176. if (_extensionLookup.TryGetValue(mimeType, out string result))
  177. {
  178. return result;
  179. }
  180. return null;
  181. }
  182. }
  183. }