MimeTypes.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Frozen;
  4. using System.Collections.Generic;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.IO;
  7. using System.Linq;
  8. using Jellyfin.Extensions;
  9. namespace MediaBrowser.Model.Net
  10. {
  11. /// <summary>
  12. /// Class MimeTypes.
  13. /// </summary>
  14. ///
  15. /// <remarks>
  16. /// For more information on MIME types:
  17. /// <list type="bullet">
  18. /// <item>http://en.wikipedia.org/wiki/Internet_media_type</item>
  19. /// <item>https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types</item>
  20. /// <item>http://www.iana.org/assignments/media-types/media-types.xhtml</item>
  21. /// </list>
  22. /// </remarks>
  23. public static class MimeTypes
  24. {
  25. /// <summary>
  26. /// Any extension in this list is considered a video file.
  27. /// </summary>
  28. private static readonly FrozenSet<string> _videoFileExtensions = new[]
  29. {
  30. ".3gp",
  31. ".asf",
  32. ".avi",
  33. ".divx",
  34. ".dvr-ms",
  35. ".f4v",
  36. ".flv",
  37. ".img",
  38. ".iso",
  39. ".m2t",
  40. ".m2ts",
  41. ".m2v",
  42. ".m4v",
  43. ".mk3d",
  44. ".mkv",
  45. ".mov",
  46. ".mp4",
  47. ".mpg",
  48. ".mpeg",
  49. ".mts",
  50. ".ogg",
  51. ".ogm",
  52. ".ogv",
  53. ".rec",
  54. ".ts",
  55. ".rmvb",
  56. ".webm",
  57. ".wmv",
  58. ".wtv",
  59. }.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
  60. /// <summary>
  61. /// Used for extensions not in <see cref="Model.MimeTypes"/> or to override them.
  62. /// </summary>
  63. private static readonly FrozenDictionary<string, string> _mimeTypeLookup = new KeyValuePair<string, string>[]
  64. {
  65. // Type application
  66. new(".azw3", "application/vnd.amazon.ebook"),
  67. new(".cb7", "application/x-cb7"),
  68. new(".cba", "application/x-cba"),
  69. new(".cbr", "application/vnd.comicbook-rar"),
  70. new(".cbt", "application/x-cbt"),
  71. new(".cbz", "application/vnd.comicbook+zip"),
  72. // Type image
  73. new(".tbn", "image/jpeg"),
  74. // Type text
  75. new(".ass", "text/x-ssa"),
  76. new(".ssa", "text/x-ssa"),
  77. new(".edl", "text/plain"),
  78. new(".html", "text/html; charset=UTF-8"),
  79. new(".htm", "text/html; charset=UTF-8"),
  80. // Type video
  81. new(".mpegts", "video/mp2t"),
  82. // Type audio
  83. new(".aac", "audio/aac"),
  84. new(".ac3", "audio/ac3"),
  85. new(".ape", "audio/x-ape"),
  86. new(".dsf", "audio/dsf"),
  87. new(".dsp", "audio/dsp"),
  88. new(".flac", "audio/flac"),
  89. new(".m4b", "audio/mp4"),
  90. new(".mp3", "audio/mpeg"),
  91. new(".vorbis", "audio/vorbis"),
  92. new(".webma", "audio/webm"),
  93. new(".wv", "audio/x-wavpack"),
  94. new(".xsp", "audio/xsp"),
  95. }.ToFrozenDictionary(pair => pair.Key, pair => pair.Value, StringComparer.OrdinalIgnoreCase);
  96. private static readonly FrozenDictionary<string, string> _extensionLookup = new KeyValuePair<string, string>[]
  97. {
  98. // Type application
  99. new("application/vnd.comicbook-rar", ".cbr"),
  100. new("application/vnd.comicbook+zip", ".cbz"),
  101. new("application/x-cb7", ".cb7"),
  102. new("application/x-cba", ".cba"),
  103. new("application/x-cbr", ".cbr"),
  104. new("application/x-cbt", ".cbt"),
  105. new("application/x-cbz", ".cbz"),
  106. new("application/x-javascript", ".js"),
  107. new("application/xml", ".xml"),
  108. new("application/x-mpegURL", ".m3u8"),
  109. // Type audio
  110. new("audio/aac", ".aac"),
  111. new("audio/ac3", ".ac3"),
  112. new("audio/dsf", ".dsf"),
  113. new("audio/dsp", ".dsp"),
  114. new("audio/flac", ".flac"),
  115. new("audio/m4b", ".m4b"),
  116. new("audio/vorbis", ".vorbis"),
  117. new("audio/x-ape", ".ape"),
  118. new("audio/xsp", ".xsp"),
  119. new("audio/x-aac", ".aac"),
  120. new("audio/x-wavpack", ".wv"),
  121. // Type image
  122. new("image/jpeg", ".jpg"),
  123. new("image/tiff", ".tiff"),
  124. new("image/x-png", ".png"),
  125. new("image/x-icon", ".ico"),
  126. // Type text
  127. new("text/plain", ".txt"),
  128. new("text/rtf", ".rtf"),
  129. new("text/x-ssa", ".ssa"),
  130. // Type video
  131. new("video/vnd.mpeg.dash.mpd", ".mpd"),
  132. new("video/x-matroska", ".mkv"),
  133. }.ToFrozenDictionary(pair => pair.Key, pair => pair.Value, StringComparer.OrdinalIgnoreCase);
  134. public static string GetMimeType(string path) => GetMimeType(path, "application/octet-stream");
  135. /// <summary>
  136. /// Gets the type of the MIME.
  137. /// </summary>
  138. /// <param name="filename">The filename to find the MIME type of.</param>
  139. /// <param name="defaultValue">The default value to return if no fitting MIME type is found.</param>
  140. /// <returns>The correct MIME type for the given filename, or <paramref name="defaultValue"/> if it wasn't found.</returns>
  141. [return: NotNullIfNotNull("defaultValue")]
  142. public static string? GetMimeType(string filename, string? defaultValue = null)
  143. {
  144. ArgumentException.ThrowIfNullOrEmpty(filename);
  145. var ext = Path.GetExtension(filename);
  146. if (_mimeTypeLookup.TryGetValue(ext, out string? result))
  147. {
  148. return result;
  149. }
  150. if (Model.MimeTypes.TryGetMimeType(filename, out var mimeType))
  151. {
  152. return mimeType;
  153. }
  154. // Catch-all for all video types that don't require specific mime types
  155. if (_videoFileExtensions.Contains(ext))
  156. {
  157. return string.Concat("video/", ext.AsSpan(1));
  158. }
  159. return defaultValue;
  160. }
  161. public static string? ToExtension(string mimeType)
  162. {
  163. ArgumentException.ThrowIfNullOrEmpty(mimeType);
  164. // handle text/html; charset=UTF-8
  165. mimeType = mimeType.AsSpan().LeftPart(';').ToString();
  166. if (_extensionLookup.TryGetValue(mimeType, out string? result))
  167. {
  168. return result;
  169. }
  170. var extension = Model.MimeTypes.GetMimeTypeExtensions(mimeType).FirstOrDefault();
  171. return string.IsNullOrEmpty(extension) ? null : "." + extension;
  172. }
  173. public static bool IsImage(ReadOnlySpan<char> mimeType)
  174. => mimeType.StartsWith("image/", StringComparison.OrdinalIgnoreCase);
  175. }
  176. }