MimeTypes.cs 6.6 KB

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