MimeTypes.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Jellyfin.Extensions;
  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. ".3gp",
  20. ".asf",
  21. ".avi",
  22. ".divx",
  23. ".dvr-ms",
  24. ".f4v",
  25. ".flv",
  26. ".img",
  27. ".iso",
  28. ".m2t",
  29. ".m2ts",
  30. ".m2v",
  31. ".m4v",
  32. ".mk3d",
  33. ".mkv",
  34. ".mov",
  35. ".mp4",
  36. ".mpg",
  37. ".mpeg",
  38. ".mts",
  39. ".ogg",
  40. ".ogm",
  41. ".ogv",
  42. ".rec",
  43. ".ts",
  44. ".rmvb",
  45. ".webm",
  46. ".wmv",
  47. ".wtv",
  48. };
  49. // http://en.wikipedia.org/wiki/Internet_media_type
  50. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  51. // http://www.iana.org/assignments/media-types/media-types.xhtml
  52. // Add more as needed
  53. private static readonly Dictionary<string, string> _mimeTypeLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
  54. {
  55. // Type application
  56. { ".7z", "application/x-7z-compressed" },
  57. { ".azw", "application/vnd.amazon.ebook" },
  58. { ".azw3", "application/vnd.amazon.ebook" },
  59. { ".cbz", "application/x-cbz" },
  60. { ".cbr", "application/epub+zip" },
  61. { ".eot", "application/vnd.ms-fontobject" },
  62. { ".epub", "application/epub+zip" },
  63. { ".js", "application/x-javascript" },
  64. { ".json", "application/json" },
  65. { ".m3u8", "application/x-mpegURL" },
  66. { ".map", "application/x-javascript" },
  67. { ".mobi", "application/x-mobipocket-ebook" },
  68. { ".opf", "application/oebps-package+xml" },
  69. { ".pdf", "application/pdf" },
  70. { ".rar", "application/vnd.rar" },
  71. { ".srt", "application/x-subrip" },
  72. { ".ttml", "application/ttml+xml" },
  73. { ".wasm", "application/wasm" },
  74. { ".xml", "application/xml" },
  75. { ".zip", "application/zip" },
  76. // Type image
  77. { ".bmp", "image/bmp" },
  78. { ".gif", "image/gif" },
  79. { ".ico", "image/vnd.microsoft.icon" },
  80. { ".jpg", "image/jpeg" },
  81. { ".jpeg", "image/jpeg" },
  82. { ".png", "image/png" },
  83. { ".svg", "image/svg+xml" },
  84. { ".svgz", "image/svg+xml" },
  85. { ".tbn", "image/jpeg" },
  86. { ".tif", "image/tiff" },
  87. { ".tiff", "image/tiff" },
  88. { ".webp", "image/webp" },
  89. // Type font
  90. { ".ttf", "font/ttf" },
  91. { ".woff", "font/woff" },
  92. { ".woff2", "font/woff2" },
  93. // Type text
  94. { ".ass", "text/x-ssa" },
  95. { ".ssa", "text/x-ssa" },
  96. { ".css", "text/css" },
  97. { ".csv", "text/csv" },
  98. { ".edl", "text/plain" },
  99. { ".rtf", "text/rtf" },
  100. { ".txt", "text/plain" },
  101. { ".vtt", "text/vtt" },
  102. // Type video
  103. { ".3gp", "video/3gpp" },
  104. { ".3g2", "video/3gpp2" },
  105. { ".asf", "video/x-ms-asf" },
  106. { ".avi", "video/x-msvideo" },
  107. { ".flv", "video/x-flv" },
  108. { ".mp4", "video/mp4" },
  109. { ".m4s", "video/mp4" },
  110. { ".m4v", "video/x-m4v" },
  111. { ".mpegts", "video/mp2t" },
  112. { ".mpg", "video/mpeg" },
  113. { ".mkv", "video/x-matroska" },
  114. { ".mov", "video/quicktime" },
  115. { ".mpd", "video/vnd.mpeg.dash.mpd" },
  116. { ".ogv", "video/ogg" },
  117. { ".ts", "video/mp2t" },
  118. { ".webm", "video/webm" },
  119. { ".wmv", "video/x-ms-wmv" },
  120. // Type audio
  121. { ".aac", "audio/aac" },
  122. { ".ac3", "audio/ac3" },
  123. { ".ape", "audio/x-ape" },
  124. { ".dsf", "audio/dsf" },
  125. { ".dsp", "audio/dsp" },
  126. { ".flac", "audio/flac" },
  127. { ".m4a", "audio/mp4" },
  128. { ".m4b", "audio/m4b" },
  129. { ".mid", "audio/midi" },
  130. { ".midi", "audio/midi" },
  131. { ".mp3", "audio/mpeg" },
  132. { ".oga", "audio/ogg" },
  133. { ".ogg", "audio/ogg" },
  134. { ".opus", "audio/ogg" },
  135. { ".vorbis", "audio/vorbis" },
  136. { ".wav", "audio/wav" },
  137. { ".webma", "audio/webm" },
  138. { ".wma", "audio/x-ms-wma" },
  139. { ".wv", "audio/x-wavpack" },
  140. { ".xsp", "audio/xsp" },
  141. };
  142. private static readonly Dictionary<string, string> _extensionLookup = CreateExtensionLookup();
  143. private static Dictionary<string, string> CreateExtensionLookup()
  144. {
  145. var dict = _mimeTypeLookup
  146. .GroupBy(i => i.Value)
  147. .ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
  148. dict["image/jpg"] = ".jpg";
  149. dict["image/x-png"] = ".png";
  150. dict["audio/x-aac"] = ".aac";
  151. return dict;
  152. }
  153. public static string? GetMimeType(string path) => GetMimeType(path, true);
  154. /// <summary>
  155. /// Gets the type of the MIME.
  156. /// </summary>
  157. /// <param name="filename">The filename to find the MIME type of.</param>
  158. /// <param name="enableStreamDefault">Whether of not to return a default value if no fitting MIME type is found.</param>
  159. /// <returns>The worrect MIME type for the given filename, or `null` if it wasn't found and <paramref name="enableStreamDefault"/> is false.</returns>
  160. public static string? GetMimeType(string filename, bool enableStreamDefault)
  161. {
  162. if (filename.Length == 0)
  163. {
  164. throw new ArgumentException("String can't be empty.", nameof(filename));
  165. }
  166. var ext = Path.GetExtension(filename);
  167. if (_mimeTypeLookup.TryGetValue(ext, out string? result))
  168. {
  169. return result;
  170. }
  171. // Catch-all for all video types that don't require specific mime types
  172. if (_videoFileExtensions.Contains(ext))
  173. {
  174. return "video/" + ext.Substring(1);
  175. }
  176. // Type text
  177. if (string.Equals(ext, ".html", StringComparison.OrdinalIgnoreCase)
  178. || string.Equals(ext, ".htm", StringComparison.OrdinalIgnoreCase))
  179. {
  180. return "text/html; charset=UTF-8";
  181. }
  182. if (string.Equals(ext, ".log", StringComparison.OrdinalIgnoreCase)
  183. || string.Equals(ext, ".srt", StringComparison.OrdinalIgnoreCase))
  184. {
  185. return "text/plain";
  186. }
  187. // Misc
  188. if (string.Equals(ext, ".dll", StringComparison.OrdinalIgnoreCase))
  189. {
  190. return "application/octet-stream";
  191. }
  192. return enableStreamDefault ? "application/octet-stream" : null;
  193. }
  194. public static string? ToExtension(string mimeType)
  195. {
  196. if (mimeType.Length == 0)
  197. {
  198. throw new ArgumentException("String can't be empty.", nameof(mimeType));
  199. }
  200. // handle text/html; charset=UTF-8
  201. mimeType = mimeType.AsSpan().LeftPart(';').ToString();
  202. if (_extensionLookup.TryGetValue(mimeType, out string? result))
  203. {
  204. return result;
  205. }
  206. return null;
  207. }
  208. }
  209. }