2020-02-04 00:49:27 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:02:23 +00:00
|
|
|
using System;
|
2018-12-27 23:27:57 +00:00
|
|
|
using System.Collections.Generic;
|
2021-02-14 14:11:46 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2018-12-27 23:27:57 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2021-09-19 18:53:31 +00:00
|
|
|
using Jellyfin.Extensions;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Model.Net
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-04-05 16:10:56 +00:00
|
|
|
/// Class MimeTypes.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
2021-12-04 13:50:48 +00:00
|
|
|
///
|
2021-12-04 15:06:51 +00:00
|
|
|
/// <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>
|
2018-12-27 23:27:57 +00:00
|
|
|
public static class MimeTypes
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-04-05 16:10:56 +00:00
|
|
|
/// Any extension in this list is considered a video file.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
2019-02-13 15:37:18 +00:00
|
|
|
private static readonly HashSet<string> _videoFileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-05-03 22:23:25 +00:00
|
|
|
".3gp",
|
|
|
|
".asf",
|
|
|
|
".avi",
|
|
|
|
".divx",
|
|
|
|
".dvr-ms",
|
|
|
|
".f4v",
|
|
|
|
".flv",
|
2019-02-13 15:37:18 +00:00
|
|
|
".img",
|
|
|
|
".iso",
|
2020-05-03 22:23:25 +00:00
|
|
|
".m2t",
|
|
|
|
".m2ts",
|
|
|
|
".m2v",
|
|
|
|
".m4v",
|
2019-02-13 15:37:18 +00:00
|
|
|
".mk3d",
|
2020-05-03 22:23:25 +00:00
|
|
|
".mkv",
|
2019-02-13 15:37:18 +00:00
|
|
|
".mov",
|
2020-05-03 22:23:25 +00:00
|
|
|
".mp4",
|
2019-02-13 15:37:18 +00:00
|
|
|
".mpg",
|
|
|
|
".mpeg",
|
2020-05-03 22:23:25 +00:00
|
|
|
".mts",
|
|
|
|
".ogg",
|
2019-02-13 15:37:18 +00:00
|
|
|
".ogm",
|
|
|
|
".ogv",
|
2020-05-05 15:17:03 +00:00
|
|
|
".rec",
|
2020-05-03 22:23:25 +00:00
|
|
|
".ts",
|
|
|
|
".rmvb",
|
2019-02-13 15:37:18 +00:00
|
|
|
".webm",
|
2020-05-03 22:23:25 +00:00
|
|
|
".wmv",
|
|
|
|
".wtv",
|
2019-02-13 15:37:18 +00:00
|
|
|
};
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2021-12-04 13:50:48 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Used for extensions not in <see cref="Model.MimeTypes"/> or to override them.
|
|
|
|
/// </summary>
|
2019-02-13 15:37:18 +00:00
|
|
|
private static readonly Dictionary<string, string> _mimeTypeLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2019-02-13 15:37:18 +00:00
|
|
|
// Type application
|
2020-05-05 11:25:32 +00:00
|
|
|
{ ".azw3", "application/vnd.amazon.ebook" },
|
2019-02-13 15:37:18 +00:00
|
|
|
|
|
|
|
// Type image
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".tbn", "image/jpeg" },
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2019-02-13 15:37:18 +00:00
|
|
|
// Type text
|
|
|
|
{ ".ass", "text/x-ssa" },
|
|
|
|
{ ".ssa", "text/x-ssa" },
|
2020-05-20 20:46:33 +00:00
|
|
|
{ ".edl", "text/plain" },
|
2021-12-04 13:50:48 +00:00
|
|
|
{ ".html", "text/html; charset=UTF-8" },
|
|
|
|
{ ".htm", "text/html; charset=UTF-8" },
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2019-02-13 15:37:18 +00:00
|
|
|
// Type video
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".mpegts", "video/mp2t" },
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2019-02-13 15:37:18 +00:00
|
|
|
// Type audio
|
2020-09-04 21:41:31 +00:00
|
|
|
{ ".aac", "audio/aac" },
|
2019-02-13 15:37:18 +00:00
|
|
|
{ ".ac3", "audio/ac3" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".ape", "audio/x-ape" },
|
2019-02-13 15:37:18 +00:00
|
|
|
{ ".dsf", "audio/dsf" },
|
|
|
|
{ ".dsp", "audio/dsp" },
|
2019-09-01 05:15:34 +00:00
|
|
|
{ ".flac", "audio/flac" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".m4b", "audio/m4b" },
|
|
|
|
{ ".mp3", "audio/mpeg" },
|
|
|
|
{ ".vorbis", "audio/vorbis" },
|
|
|
|
{ ".webma", "audio/webm" },
|
2020-04-18 05:25:54 +00:00
|
|
|
{ ".wv", "audio/x-wavpack" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".xsp", "audio/xsp" },
|
2019-02-13 15:37:18 +00:00
|
|
|
};
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2021-12-04 13:50:48 +00:00
|
|
|
private static readonly Dictionary<string, string> _extensionLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2021-12-04 13:50:48 +00:00
|
|
|
// Type application
|
|
|
|
{ "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" },
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2021-12-04 13:50:48 +00:00
|
|
|
// Type image
|
2021-12-12 01:07:35 +00:00
|
|
|
{ "image/jpeg", ".jpg" },
|
2021-12-04 13:50:48 +00:00
|
|
|
{ "image/x-png", ".png" },
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2021-12-04 13:50:48 +00:00
|
|
|
// Type text
|
|
|
|
{ "text/plain", ".txt" },
|
|
|
|
{ "text/rtf", ".rtf" },
|
|
|
|
{ "text/x-ssa", ".ssa" },
|
2019-02-13 15:37:18 +00:00
|
|
|
|
2021-12-04 13:50:48 +00:00
|
|
|
// Type video
|
|
|
|
{ "video/vnd.mpeg.dash.mpd", ".mpd" },
|
|
|
|
{ "video/x-matroska", ".mkv" },
|
|
|
|
};
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2021-02-14 14:11:46 +00:00
|
|
|
public static string GetMimeType(string path) => GetMimeType(path, "application/octet-stream");
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the type of the MIME.
|
|
|
|
/// </summary>
|
2021-02-20 22:13:04 +00:00
|
|
|
/// <param name="filename">The filename to find the MIME type of.</param>
|
2021-09-24 18:15:10 +00:00
|
|
|
/// <param name="defaultValue">The default value to return if no fitting MIME type is found.</param>
|
2021-02-14 14:11:46 +00:00
|
|
|
/// <returns>The correct MIME type for the given filename, or <paramref name="defaultValue"/> if it wasn't found.</returns>
|
2021-12-12 01:07:35 +00:00
|
|
|
[return: NotNullIfNotNull("defaultValue")]
|
2021-02-14 14:11:46 +00:00
|
|
|
public static string? GetMimeType(string filename, string? defaultValue = null)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2022-10-13 17:08:00 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(filename);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2021-02-20 22:13:04 +00:00
|
|
|
var ext = Path.GetExtension(filename);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2020-11-13 16:36:17 +00:00
|
|
|
if (_mimeTypeLookup.TryGetValue(ext, out string? result))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-12-04 13:50:48 +00:00
|
|
|
if (Model.MimeTypes.TryGetMimeType(filename, out var mimeType))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2021-12-04 13:50:48 +00:00
|
|
|
return mimeType;
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-04 13:50:48 +00:00
|
|
|
// Catch-all for all video types that don't require specific mime types
|
|
|
|
if (_videoFileExtensions.Contains(ext))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2021-12-04 13:50:48 +00:00
|
|
|
return string.Concat("video/", ext.AsSpan(1));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 14:11:46 +00:00
|
|
|
return defaultValue;
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2021-02-20 22:13:04 +00:00
|
|
|
public static string? ToExtension(string mimeType)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2022-10-13 17:08:00 +00:00
|
|
|
ArgumentException.ThrowIfNullOrEmpty(mimeType);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
// handle text/html; charset=UTF-8
|
2021-09-19 18:53:31 +00:00
|
|
|
mimeType = mimeType.AsSpan().LeftPart(';').ToString();
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2020-11-13 16:04:31 +00:00
|
|
|
if (_extensionLookup.TryGetValue(mimeType, out string? result))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
2019-02-13 15:37:18 +00:00
|
|
|
|
2021-12-04 15:13:18 +00:00
|
|
|
var extension = Model.MimeTypes.GetMimeTypeExtensions(mimeType).FirstOrDefault();
|
|
|
|
return string.IsNullOrEmpty(extension) ? null : "." + extension;
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|