MimeTypes.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // 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. };
  120. private static readonly Dictionary<string, string> _extensionLookup = CreateExtensionLookup();
  121. private static Dictionary<string, string> CreateExtensionLookup()
  122. {
  123. var dict = _mimeTypeLookup
  124. .GroupBy(i => i.Value)
  125. .ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
  126. dict["image/jpg"] = ".jpg";
  127. dict["image/x-png"] = ".png";
  128. dict["audio/x-aac"] = ".aac";
  129. return dict;
  130. }
  131. public static string GetMimeType(string path) => GetMimeType(path, true);
  132. /// <summary>
  133. /// Gets the type of the MIME.
  134. /// </summary>
  135. public static string GetMimeType(string path, bool enableStreamDefault)
  136. {
  137. if (string.IsNullOrEmpty(path))
  138. {
  139. throw new ArgumentNullException(nameof(path));
  140. }
  141. var ext = Path.GetExtension(path);
  142. if (_mimeTypeLookup.TryGetValue(ext, out string result))
  143. {
  144. return result;
  145. }
  146. // Catch-all for all video types that don't require specific mime types
  147. if (_videoFileExtensions.Contains(ext))
  148. {
  149. return "video/" + ext.Substring(1);
  150. }
  151. // Type text
  152. if (string.Equals(ext, ".html", StringComparison.OrdinalIgnoreCase)
  153. || string.Equals(ext, ".htm", StringComparison.OrdinalIgnoreCase))
  154. {
  155. return "text/html; charset=UTF-8";
  156. }
  157. if (string.Equals(ext, ".log", StringComparison.OrdinalIgnoreCase)
  158. || string.Equals(ext, ".srt", StringComparison.OrdinalIgnoreCase))
  159. {
  160. return "text/plain";
  161. }
  162. // Misc
  163. if (string.Equals(ext, ".dll", StringComparison.OrdinalIgnoreCase))
  164. {
  165. return "application/octet-stream";
  166. }
  167. return enableStreamDefault ? "application/octet-stream" : null;
  168. }
  169. public static string ToExtension(string mimeType)
  170. {
  171. if (string.IsNullOrEmpty(mimeType))
  172. {
  173. throw new ArgumentNullException(nameof(mimeType));
  174. }
  175. // handle text/html; charset=UTF-8
  176. mimeType = mimeType.Split(';')[0];
  177. if (_extensionLookup.TryGetValue(mimeType, out string result))
  178. {
  179. return result;
  180. }
  181. return null;
  182. }
  183. }
  184. }