MimeTypes.cs 6.8 KB

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