MimeTypes.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #pragma warning disable CS1591
  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
  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. { ".wasm", "application/wasm" },
  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. { ".mpegts", "video/mp2t" },
  103. // Type audio
  104. { ".mp3", "audio/mpeg" },
  105. { ".m4a", "audio/mp4" },
  106. { ".aac", "audio/mp4" },
  107. { ".webma", "audio/webm" },
  108. { ".wav", "audio/wav" },
  109. { ".wma", "audio/x-ms-wma" },
  110. { ".ogg", "audio/ogg" },
  111. { ".oga", "audio/ogg" },
  112. { ".opus", "audio/ogg" },
  113. { ".ac3", "audio/ac3" },
  114. { ".dsf", "audio/dsf" },
  115. { ".m4b", "audio/m4b" },
  116. { ".xsp", "audio/xsp" },
  117. { ".dsp", "audio/dsp" },
  118. { ".flac", "audio/flac" },
  119. { ".ape", "audio/x-ape" },
  120. { ".wv", "audio/x-wavpack" },
  121. };
  122. private static readonly Dictionary<string, string> _extensionLookup = CreateExtensionLookup();
  123. private static Dictionary<string, string> CreateExtensionLookup()
  124. {
  125. var dict = _mimeTypeLookup
  126. .GroupBy(i => i.Value)
  127. .ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
  128. dict["image/jpg"] = ".jpg";
  129. dict["image/x-png"] = ".png";
  130. dict["audio/x-aac"] = ".aac";
  131. return dict;
  132. }
  133. public static string GetMimeType(string path) => GetMimeType(path, true);
  134. /// <summary>
  135. /// Gets the type of the MIME.
  136. /// </summary>
  137. public static string GetMimeType(string path, bool enableStreamDefault)
  138. {
  139. if (string.IsNullOrEmpty(path))
  140. {
  141. throw new ArgumentNullException(nameof(path));
  142. }
  143. var ext = Path.GetExtension(path);
  144. if (_mimeTypeLookup.TryGetValue(ext, out string result))
  145. {
  146. return result;
  147. }
  148. // Catch-all for all video types that don't require specific mime types
  149. if (_videoFileExtensions.Contains(ext))
  150. {
  151. return "video/" + ext.Substring(1);
  152. }
  153. // Type text
  154. if (string.Equals(ext, ".html", StringComparison.OrdinalIgnoreCase)
  155. || string.Equals(ext, ".htm", StringComparison.OrdinalIgnoreCase))
  156. {
  157. return "text/html; charset=UTF-8";
  158. }
  159. if (string.Equals(ext, ".log", StringComparison.OrdinalIgnoreCase)
  160. || string.Equals(ext, ".srt", StringComparison.OrdinalIgnoreCase))
  161. {
  162. return "text/plain";
  163. }
  164. // Misc
  165. if (string.Equals(ext, ".dll", StringComparison.OrdinalIgnoreCase))
  166. {
  167. return "application/octet-stream";
  168. }
  169. return enableStreamDefault ? "application/octet-stream" : null;
  170. }
  171. public static string ToExtension(string mimeType)
  172. {
  173. if (string.IsNullOrEmpty(mimeType))
  174. {
  175. throw new ArgumentNullException(nameof(mimeType));
  176. }
  177. // handle text/html; charset=UTF-8
  178. mimeType = mimeType.Split(';')[0];
  179. if (_extensionLookup.TryGetValue(mimeType, out string result))
  180. {
  181. return result;
  182. }
  183. return null;
  184. }
  185. }
  186. }