2
0

MimeTypes.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. ".webm",
  58. ".wmv",
  59. ".wtv",
  60. }.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
  61. /// <summary>
  62. /// Used for extensions not in <see cref="Model.MimeTypes"/> or to override them.
  63. /// </summary>
  64. private static readonly FrozenDictionary<string, string> _mimeTypeLookup = new KeyValuePair<string, string>[]
  65. {
  66. // Type application
  67. new(".azw3", "application/vnd.amazon.ebook"),
  68. new(".cb7", "application/x-cb7"),
  69. new(".cba", "application/x-cba"),
  70. new(".cbr", "application/vnd.comicbook-rar"),
  71. new(".cbt", "application/x-cbt"),
  72. new(".cbz", "application/vnd.comicbook+zip"),
  73. // Type image
  74. new(".tbn", "image/jpeg"),
  75. // Type text
  76. new(".ass", "text/x-ssa"),
  77. new(".ssa", "text/x-ssa"),
  78. new(".edl", "text/plain"),
  79. new(".html", "text/html; charset=UTF-8"),
  80. new(".htm", "text/html; charset=UTF-8"),
  81. // Type video
  82. new(".mpegts", "video/mp2t"),
  83. // Type audio
  84. new(".aac", "audio/aac"),
  85. new(".ac3", "audio/ac3"),
  86. new(".ape", "audio/x-ape"),
  87. new(".dsf", "audio/dsf"),
  88. new(".dsp", "audio/dsp"),
  89. new(".flac", "audio/flac"),
  90. new(".m4b", "audio/mp4"),
  91. new(".mp3", "audio/mpeg"),
  92. new(".vorbis", "audio/vorbis"),
  93. new(".webma", "audio/webm"),
  94. new(".wv", "audio/x-wavpack"),
  95. new(".xsp", "audio/xsp"),
  96. }.ToFrozenDictionary(pair => pair.Key, pair => pair.Value, StringComparer.OrdinalIgnoreCase);
  97. private static readonly FrozenDictionary<string, string> _extensionLookup = new KeyValuePair<string, string>[]
  98. {
  99. // Type application
  100. new("application/vnd.comicbook-rar", ".cbr"),
  101. new("application/vnd.comicbook+zip", ".cbz"),
  102. new("application/x-cb7", ".cb7"),
  103. new("application/x-cba", ".cba"),
  104. new("application/x-cbr", ".cbr"),
  105. new("application/x-cbt", ".cbt"),
  106. new("application/x-cbz", ".cbz"),
  107. new("application/x-javascript", ".js"),
  108. new("application/xml", ".xml"),
  109. new("application/x-mpegURL", ".m3u8"),
  110. // Type audio
  111. new("audio/aac", ".aac"),
  112. new("audio/ac3", ".ac3"),
  113. new("audio/dsf", ".dsf"),
  114. new("audio/dsp", ".dsp"),
  115. new("audio/flac", ".flac"),
  116. new("audio/m4b", ".m4b"),
  117. new("audio/vorbis", ".vorbis"),
  118. new("audio/x-ape", ".ape"),
  119. new("audio/xsp", ".xsp"),
  120. new("audio/x-aac", ".aac"),
  121. new("audio/x-wavpack", ".wv"),
  122. // Type image
  123. new("image/jpeg", ".jpg"),
  124. new("image/tiff", ".tiff"),
  125. new("image/x-png", ".png"),
  126. new("image/x-icon", ".ico"),
  127. // Type text
  128. new("text/plain", ".txt"),
  129. new("text/rtf", ".rtf"),
  130. new("text/x-ssa", ".ssa"),
  131. // Type video
  132. new("video/vnd.mpeg.dash.mpd", ".mpd"),
  133. new("video/x-matroska", ".mkv"),
  134. }.ToFrozenDictionary(pair => pair.Key, pair => pair.Value, StringComparer.OrdinalIgnoreCase);
  135. public static string GetMimeType(string path) => GetMimeType(path, MediaTypeNames.Application.Octet);
  136. /// <summary>
  137. /// Gets the type of the MIME.
  138. /// </summary>
  139. /// <param name="filename">The filename to find the MIME type of.</param>
  140. /// <param name="defaultValue">The default value to return if no fitting MIME type is found.</param>
  141. /// <returns>The correct MIME type for the given filename, or <paramref name="defaultValue"/> if it wasn't found.</returns>
  142. [return: NotNullIfNotNull("defaultValue")]
  143. public static string? GetMimeType(string filename, string? defaultValue = null)
  144. {
  145. ArgumentException.ThrowIfNullOrEmpty(filename);
  146. var ext = Path.GetExtension(filename);
  147. if (_mimeTypeLookup.TryGetValue(ext, out string? result))
  148. {
  149. return result;
  150. }
  151. if (Model.MimeTypes.TryGetMimeType(filename, out var mimeType))
  152. {
  153. return mimeType;
  154. }
  155. // Catch-all for all video types that don't require specific mime types
  156. if (_videoFileExtensions.Contains(ext))
  157. {
  158. return string.Concat("video/", ext.AsSpan(1));
  159. }
  160. return defaultValue;
  161. }
  162. public static string? ToExtension(string mimeType)
  163. {
  164. ArgumentException.ThrowIfNullOrEmpty(mimeType);
  165. // handle text/html; charset=UTF-8
  166. mimeType = mimeType.AsSpan().LeftPart(';').ToString();
  167. if (_extensionLookup.TryGetValue(mimeType, out string? result))
  168. {
  169. return result;
  170. }
  171. var extension = Model.MimeTypes.GetMimeTypeExtensions(mimeType).FirstOrDefault();
  172. return string.IsNullOrEmpty(extension) ? null : "." + extension;
  173. }
  174. public static bool IsImage(ReadOnlySpan<char> mimeType)
  175. => mimeType.StartsWith("image/", StringComparison.OrdinalIgnoreCase);
  176. }
  177. }