2
0

MimeTypes.cs 5.7 KB

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