MimeTypes.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. public static class MimeTypes
  14. {
  15. /// <summary>
  16. /// Any extension in this list is considered a video file.
  17. /// </summary>
  18. private static readonly HashSet<string> _videoFileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  19. {
  20. ".3gp",
  21. ".asf",
  22. ".avi",
  23. ".divx",
  24. ".dvr-ms",
  25. ".f4v",
  26. ".flv",
  27. ".img",
  28. ".iso",
  29. ".m2t",
  30. ".m2ts",
  31. ".m2v",
  32. ".m4v",
  33. ".mk3d",
  34. ".mkv",
  35. ".mov",
  36. ".mp4",
  37. ".mpg",
  38. ".mpeg",
  39. ".mts",
  40. ".ogg",
  41. ".ogm",
  42. ".ogv",
  43. ".rec",
  44. ".ts",
  45. ".rmvb",
  46. ".webm",
  47. ".wmv",
  48. ".wtv",
  49. };
  50. // http://en.wikipedia.org/wiki/Internet_media_type
  51. // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
  52. // http://www.iana.org/assignments/media-types/media-types.xhtml
  53. // Add more as needed
  54. private static readonly Dictionary<string, string> _mimeTypeLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
  55. {
  56. // Type application
  57. { ".7z", "application/x-7z-compressed" },
  58. { ".azw", "application/vnd.amazon.ebook" },
  59. { ".azw3", "application/vnd.amazon.ebook" },
  60. { ".cbz", "application/x-cbz" },
  61. { ".cbr", "application/epub+zip" },
  62. { ".eot", "application/vnd.ms-fontobject" },
  63. { ".epub", "application/epub+zip" },
  64. { ".js", "application/x-javascript" },
  65. { ".json", "application/json" },
  66. { ".m3u8", "application/x-mpegURL" },
  67. { ".map", "application/x-javascript" },
  68. { ".mobi", "application/x-mobipocket-ebook" },
  69. { ".opf", "application/oebps-package+xml" },
  70. { ".pdf", "application/pdf" },
  71. { ".rar", "application/vnd.rar" },
  72. { ".srt", "application/x-subrip" },
  73. { ".ttml", "application/ttml+xml" },
  74. { ".wasm", "application/wasm" },
  75. { ".xml", "application/xml" },
  76. { ".zip", "application/zip" },
  77. // Type image
  78. { ".bmp", "image/bmp" },
  79. { ".gif", "image/gif" },
  80. { ".ico", "image/vnd.microsoft.icon" },
  81. { ".jpg", "image/jpeg" },
  82. { ".jpeg", "image/jpeg" },
  83. { ".png", "image/png" },
  84. { ".svg", "image/svg+xml" },
  85. { ".svgz", "image/svg+xml" },
  86. { ".tbn", "image/jpeg" },
  87. { ".tif", "image/tiff" },
  88. { ".tiff", "image/tiff" },
  89. { ".webp", "image/webp" },
  90. // Type font
  91. { ".ttf", "font/ttf" },
  92. { ".woff", "font/woff" },
  93. { ".woff2", "font/woff2" },
  94. // Type text
  95. { ".ass", "text/x-ssa" },
  96. { ".ssa", "text/x-ssa" },
  97. { ".css", "text/css" },
  98. { ".csv", "text/csv" },
  99. { ".edl", "text/plain" },
  100. { ".rtf", "text/rtf" },
  101. { ".txt", "text/plain" },
  102. { ".vtt", "text/vtt" },
  103. // Type video
  104. { ".3gp", "video/3gpp" },
  105. { ".3g2", "video/3gpp2" },
  106. { ".asf", "video/x-ms-asf" },
  107. { ".avi", "video/x-msvideo" },
  108. { ".flv", "video/x-flv" },
  109. { ".mp4", "video/mp4" },
  110. { ".m4s", "video/mp4" },
  111. { ".m4v", "video/x-m4v" },
  112. { ".mpegts", "video/mp2t" },
  113. { ".mpg", "video/mpeg" },
  114. { ".mkv", "video/x-matroska" },
  115. { ".mov", "video/quicktime" },
  116. { ".mpd", "video/vnd.mpeg.dash.mpd" },
  117. { ".ogv", "video/ogg" },
  118. { ".ts", "video/mp2t" },
  119. { ".webm", "video/webm" },
  120. { ".wmv", "video/x-ms-wmv" },
  121. // Type audio
  122. { ".aac", "audio/aac" },
  123. { ".ac3", "audio/ac3" },
  124. { ".ape", "audio/x-ape" },
  125. { ".dsf", "audio/dsf" },
  126. { ".dsp", "audio/dsp" },
  127. { ".flac", "audio/flac" },
  128. { ".m4a", "audio/mp4" },
  129. { ".m4b", "audio/m4b" },
  130. { ".mid", "audio/midi" },
  131. { ".midi", "audio/midi" },
  132. { ".mp3", "audio/mpeg" },
  133. { ".oga", "audio/ogg" },
  134. { ".ogg", "audio/ogg" },
  135. { ".opus", "audio/ogg" },
  136. { ".vorbis", "audio/vorbis" },
  137. { ".wav", "audio/wav" },
  138. { ".webma", "audio/webm" },
  139. { ".wma", "audio/x-ms-wma" },
  140. { ".wv", "audio/x-wavpack" },
  141. { ".xsp", "audio/xsp" },
  142. };
  143. private static readonly Dictionary<string, string> _extensionLookup = CreateExtensionLookup();
  144. private static Dictionary<string, string> CreateExtensionLookup()
  145. {
  146. var dict = _mimeTypeLookup
  147. .GroupBy(i => i.Value)
  148. .ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
  149. dict["image/jpg"] = ".jpg";
  150. dict["image/x-png"] = ".png";
  151. dict["audio/x-aac"] = ".aac";
  152. return dict;
  153. }
  154. public static string GetMimeType(string path) => GetMimeType(path, "application/octet-stream");
  155. /// <summary>
  156. /// Gets the type of the MIME.
  157. /// </summary>
  158. /// <param name="filename">The filename to find the MIME type of.</param>
  159. /// <param name="defaultValue">The default value to return if no fitting MIME type is found.</param>
  160. /// <returns>The correct MIME type for the given filename, or <paramref name="defaultValue"/> if it wasn't found.</returns>
  161. [return: NotNullIfNotNullAttribute("defaultValue")]
  162. public static string? GetMimeType(string filename, string? defaultValue = null)
  163. {
  164. if (filename.Length == 0)
  165. {
  166. throw new ArgumentException("String can't be empty.", nameof(filename));
  167. }
  168. var ext = Path.GetExtension(filename);
  169. if (_mimeTypeLookup.TryGetValue(ext, out string? result))
  170. {
  171. return result;
  172. }
  173. // Catch-all for all video types that don't require specific mime types
  174. if (_videoFileExtensions.Contains(ext))
  175. {
  176. return "video/" + ext.Substring(1);
  177. }
  178. // Type text
  179. if (string.Equals(ext, ".html", StringComparison.OrdinalIgnoreCase)
  180. || string.Equals(ext, ".htm", StringComparison.OrdinalIgnoreCase))
  181. {
  182. return "text/html; charset=UTF-8";
  183. }
  184. if (string.Equals(ext, ".log", StringComparison.OrdinalIgnoreCase)
  185. || string.Equals(ext, ".srt", StringComparison.OrdinalIgnoreCase))
  186. {
  187. return "text/plain";
  188. }
  189. // Misc
  190. if (string.Equals(ext, ".dll", StringComparison.OrdinalIgnoreCase))
  191. {
  192. return "application/octet-stream";
  193. }
  194. return defaultValue;
  195. }
  196. public static string? ToExtension(string mimeType)
  197. {
  198. if (mimeType.Length == 0)
  199. {
  200. throw new ArgumentException("String can't be empty.", nameof(mimeType));
  201. }
  202. // handle text/html; charset=UTF-8
  203. mimeType = mimeType.AsSpan().LeftPart(';').ToString();
  204. if (_extensionLookup.TryGetValue(mimeType, out string? result))
  205. {
  206. return result;
  207. }
  208. return null;
  209. }
  210. }
  211. }