You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Model/Net/MimeTypes.cs

199 lines
6.5 KiB

#pragma warning disable CS1591
using System;
5 years ago
using System.Collections.Generic;
3 years ago
using System.Diagnostics.CodeAnalysis;
5 years ago
using System.IO;
using System.Linq;
using Jellyfin.Extensions;
5 years ago
namespace MediaBrowser.Model.Net
{
/// <summary>
/// Class MimeTypes.
5 years ago
/// </summary>
///
/// <remarks>
/// For more information on MIME types:
/// <list type="bullet">
/// <item>http://en.wikipedia.org/wiki/Internet_media_type</item>
/// <item>https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types</item>
/// <item>http://www.iana.org/assignments/media-types/media-types.xhtml</item>
/// </list>
/// </remarks>
5 years ago
public static class MimeTypes
{
/// <summary>
/// Any extension in this list is considered a video file.
5 years ago
/// </summary>
private static readonly HashSet<string> _videoFileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
5 years ago
{
".3gp",
".asf",
".avi",
".divx",
".dvr-ms",
".f4v",
".flv",
".img",
".iso",
".m2t",
".m2ts",
".m2v",
".m4v",
".mk3d",
".mkv",
".mov",
".mp4",
".mpg",
".mpeg",
".mts",
".ogg",
".ogm",
".ogv",
".rec",
".ts",
".rmvb",
".webm",
".wmv",
".wtv",
};
5 years ago
/// <summary>
/// Used for extensions not in <see cref="Model.MimeTypes"/> or to override them.
/// </summary>
private static readonly Dictionary<string, string> _mimeTypeLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
5 years ago
{
// Type application
4 years ago
{ ".azw3", "application/vnd.amazon.ebook" },
Add missing MIME types for comicbook formats (#11010) * Correct MIME types for comicbook file extensions cb7, cba, cbr, cbt and cbz all refer to different types of digital comicbooks. The last letter of the extension indicates the compression algorithm that was used: 7zip, arc, rar, tar or zip. All these filetypes used to have the `application/x-cbr` MIME type assigned to them. However, that has since been deprecated and was replaced with - `application/vnd.comicbook-rar` for rar compressed files and - `application/vnd.comicbook+zip` for rar compressed files. Only these two are officially listed by IANA https://www.iana.org/assignments/media-types/application/vnd.comicbook+zip . cbr and cbz are by far the most common file extensions for comicbooks. There's no official MIME type for cb7, cba or cbt files. However, with rar being a proprietary compression algorithm, FOSS applications will often refuse to handle files that identify themselves as `application/x-cbr`, so I decided to assign extension specific MIME types to them. I've seen these being used by other applications, specifically comic book readers. I've read through the docs on iana.org, but haven't figured out why they chose `-rar`, but `+zip`. * Add conversions from MIME type to file extensions for comicbook formats cb7, cba, cbr, cbt and cbz all refer to different types of digital comicbooks. The last letter of the extension indicates the compression algorithm that was used: 7zip, arc, rar, tar or zip. All these filetypes used to have the `application/x-cbr` MIME type assigned to them. However, that has since been deprecated and was replaced with - `application/vnd.comicbook-rar` for rar compressed files and - `application/vnd.comicbook+zip` for rar compressed files. Only these two are officially listed by IANA https://www.iana.org/assignments/media-types/application/vnd.comicbook+zip . cbr and cbz are by far the most common file extensions for comicbooks. There's no official MIME type for cb7, cba or cbt files. However, with rar being a proprietary compression algorithm, FOSS applications will often refuse to handle files that identify themselves as `application/x-cbr`, so I decided to assign extension specific MIME types to them. I've seen these being used by other applications, specifically comic book readers. * Update CONTRIBUTORS.md
3 months ago
{ ".cb7", "application/x-cb7" },
{ ".cba", "application/x-cba" },
{ ".cbr", "application/vnd.comicbook-rar" },
{ ".cbt", "application/x-cbt" },
{ ".cbz", "application/vnd.comicbook+zip" },
// Type image
{ ".tbn", "image/jpeg" },
5 years ago
// Type text
{ ".ass", "text/x-ssa" },
{ ".ssa", "text/x-ssa" },
{ ".edl", "text/plain" },
{ ".html", "text/html; charset=UTF-8" },
{ ".htm", "text/html; charset=UTF-8" },
5 years ago
// Type video
{ ".mpegts", "video/mp2t" },
5 years ago
// Type audio
{ ".aac", "audio/aac" },
{ ".ac3", "audio/ac3" },
{ ".ape", "audio/x-ape" },
{ ".dsf", "audio/dsf" },
{ ".dsp", "audio/dsp" },
{ ".flac", "audio/flac" },
{ ".m4b", "audio/mp4" },
{ ".mp3", "audio/mpeg" },
{ ".vorbis", "audio/vorbis" },
{ ".webma", "audio/webm" },
{ ".wv", "audio/x-wavpack" },
{ ".xsp", "audio/xsp" },
};
5 years ago
private static readonly Dictionary<string, string> _extensionLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
5 years ago
{
// Type application
Add missing MIME types for comicbook formats (#11010) * Correct MIME types for comicbook file extensions cb7, cba, cbr, cbt and cbz all refer to different types of digital comicbooks. The last letter of the extension indicates the compression algorithm that was used: 7zip, arc, rar, tar or zip. All these filetypes used to have the `application/x-cbr` MIME type assigned to them. However, that has since been deprecated and was replaced with - `application/vnd.comicbook-rar` for rar compressed files and - `application/vnd.comicbook+zip` for rar compressed files. Only these two are officially listed by IANA https://www.iana.org/assignments/media-types/application/vnd.comicbook+zip . cbr and cbz are by far the most common file extensions for comicbooks. There's no official MIME type for cb7, cba or cbt files. However, with rar being a proprietary compression algorithm, FOSS applications will often refuse to handle files that identify themselves as `application/x-cbr`, so I decided to assign extension specific MIME types to them. I've seen these being used by other applications, specifically comic book readers. I've read through the docs on iana.org, but haven't figured out why they chose `-rar`, but `+zip`. * Add conversions from MIME type to file extensions for comicbook formats cb7, cba, cbr, cbt and cbz all refer to different types of digital comicbooks. The last letter of the extension indicates the compression algorithm that was used: 7zip, arc, rar, tar or zip. All these filetypes used to have the `application/x-cbr` MIME type assigned to them. However, that has since been deprecated and was replaced with - `application/vnd.comicbook-rar` for rar compressed files and - `application/vnd.comicbook+zip` for rar compressed files. Only these two are officially listed by IANA https://www.iana.org/assignments/media-types/application/vnd.comicbook+zip . cbr and cbz are by far the most common file extensions for comicbooks. There's no official MIME type for cb7, cba or cbt files. However, with rar being a proprietary compression algorithm, FOSS applications will often refuse to handle files that identify themselves as `application/x-cbr`, so I decided to assign extension specific MIME types to them. I've seen these being used by other applications, specifically comic book readers. * Update CONTRIBUTORS.md
3 months ago
{ "application/vnd.comicbook-rar", ".cbr" },
{ "application/vnd.comicbook+zip", ".cbz" },
{ "application/x-cb7", ".cb7" },
{ "application/x-cba", ".cba" },
{ "application/x-cbr", ".cbr" },
{ "application/x-cbt", ".cbt" },
{ "application/x-cbz", ".cbz" },
{ "application/x-javascript", ".js" },
{ "application/xml", ".xml" },
{ "application/x-mpegURL", ".m3u8" },
// Type audio
{ "audio/aac", ".aac" },
{ "audio/ac3", ".ac3" },
{ "audio/dsf", ".dsf" },
{ "audio/dsp", ".dsp" },
{ "audio/flac", ".flac" },
{ "audio/m4b", ".m4b" },
{ "audio/vorbis", ".vorbis" },
{ "audio/x-ape", ".ape" },
{ "audio/xsp", ".xsp" },
{ "audio/x-wavpack", ".wv" },
5 years ago
// Type image
{ "image/jpeg", ".jpg" },
{ "image/tiff", ".tiff" },
{ "image/x-png", ".png" },
{ "image/x-icon", ".ico" },
5 years ago
// Type text
{ "text/plain", ".txt" },
{ "text/rtf", ".rtf" },
{ "text/x-ssa", ".ssa" },
// Type video
{ "video/vnd.mpeg.dash.mpd", ".mpd" },
{ "video/x-matroska", ".mkv" },
};
5 years ago
3 years ago
public static string GetMimeType(string path) => GetMimeType(path, "application/octet-stream");
5 years ago
/// <summary>
/// Gets the type of the MIME.
/// </summary>
/// <param name="filename">The filename to find the MIME type of.</param>
3 years ago
/// <param name="defaultValue">The default value to return if no fitting MIME type is found.</param>
3 years ago
/// <returns>The correct MIME type for the given filename, or <paramref name="defaultValue"/> if it wasn't found.</returns>
[return: NotNullIfNotNull("defaultValue")]
3 years ago
public static string? GetMimeType(string filename, string? defaultValue = null)
5 years ago
{
ArgumentException.ThrowIfNullOrEmpty(filename);
5 years ago
var ext = Path.GetExtension(filename);
5 years ago
if (_mimeTypeLookup.TryGetValue(ext, out string? result))
5 years ago
{
return result;
}
if (Model.MimeTypes.TryGetMimeType(filename, out var mimeType))
5 years ago
{
return mimeType;
5 years ago
}
// Catch-all for all video types that don't require specific mime types
if (_videoFileExtensions.Contains(ext))
5 years ago
{
return string.Concat("video/", ext.AsSpan(1));
5 years ago
}
3 years ago
return defaultValue;
5 years ago
}
public static string? ToExtension(string mimeType)
5 years ago
{
ArgumentException.ThrowIfNullOrEmpty(mimeType);
5 years ago
// handle text/html; charset=UTF-8
mimeType = mimeType.AsSpan().LeftPart(';').ToString();
5 years ago
if (_extensionLookup.TryGetValue(mimeType, out string? result))
5 years ago
{
return result;
}
var extension = Model.MimeTypes.GetMimeTypeExtensions(mimeType).FirstOrDefault();
return string.IsNullOrEmpty(extension) ? null : "." + extension;
5 years ago
}
public static bool IsImage(ReadOnlySpan<char> mimeType)
=> mimeType.StartsWith("image/", StringComparison.OrdinalIgnoreCase);
5 years ago
}
}