MimeTypes.cs 5.8 KB

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