jellyfin/MediaBrowser.Model/Net/MimeTypes.cs

232 lines
7.4 KiB
C#
Raw Normal View History

2020-02-03 17:49:27 -07:00
#pragma warning disable CS1591
using System;
2018-12-27 16:27:57 -07:00
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.Model.Net
{
/// <summary>
/// Class MimeTypes.
2018-12-27 16:27:57 -07:00
/// </summary>
public static class MimeTypes
{
/// <summary>
/// Any extension in this list is considered a video file.
2018-12-27 16:27:57 -07:00
/// </summary>
2019-02-13 08:37:18 -07:00
private static readonly HashSet<string> _videoFileExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
2018-12-27 16:27:57 -07:00
{
2020-05-03 15:23:25 -07:00
".3gp",
".asf",
".avi",
".divx",
".dvr-ms",
".f4v",
".flv",
2019-02-13 08:37:18 -07:00
".img",
".iso",
2020-05-03 15:23:25 -07:00
".m2t",
".m2ts",
".m2v",
".m4v",
2019-02-13 08:37:18 -07:00
".mk3d",
2020-05-03 15:23:25 -07:00
".mkv",
2019-02-13 08:37:18 -07:00
".mov",
2020-05-03 15:23:25 -07:00
".mp4",
2019-02-13 08:37:18 -07:00
".mpg",
".mpeg",
2020-05-03 15:23:25 -07:00
".mts",
".ogg",
2019-02-13 08:37:18 -07:00
".ogm",
".ogv",
2020-05-05 08:17:03 -07:00
".rec",
2020-05-03 15:23:25 -07:00
".ts",
".rmvb",
2019-02-13 08:37:18 -07:00
".webm",
2020-05-03 15:23:25 -07:00
".wmv",
".wtv",
2019-02-13 08:37:18 -07:00
};
2018-12-27 16:27:57 -07:00
// http://en.wikipedia.org/wiki/Internet_media_type
2020-05-03 15:23:25 -07:00
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
// http://www.iana.org/assignments/media-types/media-types.xhtml
2018-12-27 16:27:57 -07:00
// Add more as needed
2019-02-13 08:37:18 -07:00
private static readonly Dictionary<string, string> _mimeTypeLookup = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
2018-12-27 16:27:57 -07:00
{
2019-02-13 08:37:18 -07:00
// Type application
2020-05-03 15:23:25 -07:00
{ ".7z", "application/x-7z-compressed" },
{ ".azw", "application/vnd.amazon.ebook" },
2020-05-05 04:25:32 -07:00
{ ".azw3", "application/vnd.amazon.ebook" },
2019-02-13 08:37:18 -07:00
{ ".cbz", "application/x-cbz" },
{ ".cbr", "application/epub+zip" },
{ ".eot", "application/vnd.ms-fontobject" },
{ ".epub", "application/epub+zip" },
{ ".js", "application/x-javascript" },
{ ".json", "application/json" },
2020-05-03 15:23:25 -07:00
{ ".m3u8", "application/x-mpegURL" },
2019-02-13 08:37:18 -07:00
{ ".map", "application/x-javascript" },
2020-05-03 15:23:25 -07:00
{ ".mobi", "application/x-mobipocket-ebook" },
2020-05-15 06:38:26 -07:00
{ ".opf", "application/oebps-package+xml" },
2019-02-13 08:37:18 -07:00
{ ".pdf", "application/pdf" },
2020-05-03 15:23:25 -07:00
{ ".rar", "application/vnd.rar" },
{ ".srt", "application/x-subrip" },
2019-02-13 08:37:18 -07:00
{ ".ttml", "application/ttml+xml" },
2020-03-16 20:49:32 -07:00
{ ".wasm", "application/wasm" },
2020-05-03 15:23:25 -07:00
{ ".xml", "application/xml" },
{ ".zip", "application/zip" },
2019-02-13 08:37:18 -07:00
// Type image
2020-05-03 15:23:25 -07:00
{ ".bmp", "image/bmp" },
{ ".gif", "image/gif" },
{ ".ico", "image/vnd.microsoft.icon" },
2019-02-13 08:37:18 -07:00
{ ".jpg", "image/jpeg" },
{ ".jpeg", "image/jpeg" },
{ ".png", "image/png" },
{ ".svg", "image/svg+xml" },
{ ".svgz", "image/svg+xml" },
2020-05-03 15:23:25 -07:00
{ ".tbn", "image/jpeg" },
{ ".tif", "image/tiff" },
{ ".tiff", "image/tiff" },
{ ".webp", "image/webp" },
2019-02-13 08:37:18 -07:00
// Type font
{ ".ttf" , "font/ttf" },
{ ".woff" , "font/woff" },
2020-05-03 15:23:25 -07:00
{ ".woff2" , "font/woff2" },
2018-12-27 16:27:57 -07:00
2019-02-13 08:37:18 -07:00
// Type text
{ ".ass", "text/x-ssa" },
{ ".ssa", "text/x-ssa" },
{ ".css", "text/css" },
{ ".csv", "text/csv" },
2020-05-20 13:46:33 -07:00
{ ".edl", "text/plain" },
2020-05-03 15:23:25 -07:00
{ ".rtf", "text/rtf" },
2019-02-13 08:37:18 -07:00
{ ".txt", "text/plain" },
{ ".vtt", "text/vtt" },
2018-12-27 16:27:57 -07:00
2019-02-13 08:37:18 -07:00
// Type video
{ ".3gp", "video/3gpp" },
{ ".3g2", "video/3gpp2" },
2020-05-03 15:23:25 -07:00
{ ".asf", "video/x-ms-asf" },
{ ".avi", "video/x-msvideo" },
{ ".flv", "video/x-flv" },
{ ".mp4", "video/mp4" },
{ ".m4s", "video/mp4" },
{ ".m4v", "video/x-m4v" },
{ ".mpegts", "video/mp2t" },
{ ".mpg", "video/mpeg" },
{ ".mkv", "video/x-matroska" },
{ ".mov", "video/quicktime" },
2019-02-13 08:37:18 -07:00
{ ".mpd", "video/vnd.mpeg.dash.mpd" },
2020-05-03 15:23:25 -07:00
{ ".ogv", "video/ogg" },
2019-02-13 08:37:18 -07:00
{ ".ts", "video/mp2t" },
2020-05-03 15:23:25 -07:00
{ ".webm", "video/webm" },
{ ".wmv", "video/x-ms-wmv" },
2018-12-27 16:27:57 -07:00
2019-02-13 08:37:18 -07:00
// Type audio
2020-09-04 14:41:31 -07:00
{ ".aac", "audio/aac" },
2019-02-13 08:37:18 -07:00
{ ".ac3", "audio/ac3" },
2020-05-03 15:23:25 -07:00
{ ".ape", "audio/x-ape" },
2019-02-13 08:37:18 -07:00
{ ".dsf", "audio/dsf" },
{ ".dsp", "audio/dsp" },
2019-08-31 22:15:34 -07:00
{ ".flac", "audio/flac" },
2020-05-03 15:23:25 -07:00
{ ".m4a", "audio/mp4" },
{ ".m4b", "audio/m4b" },
{ ".mid", "audio/midi" },
{ ".midi", "audio/midi" },
{ ".mp3", "audio/mpeg" },
{ ".oga", "audio/ogg" },
{ ".ogg", "audio/ogg" },
{ ".opus", "audio/ogg" },
{ ".vorbis", "audio/vorbis" },
{ ".wav", "audio/wav" },
{ ".webma", "audio/webm" },
{ ".wma", "audio/x-ms-wma" },
2020-04-17 22:25:54 -07:00
{ ".wv", "audio/x-wavpack" },
2020-05-03 15:23:25 -07:00
{ ".xsp", "audio/xsp" },
2019-02-13 08:37:18 -07:00
};
2018-12-27 16:27:57 -07:00
2019-02-13 08:37:18 -07:00
private static readonly Dictionary<string, string> _extensionLookup = CreateExtensionLookup();
2018-12-27 16:27:57 -07:00
private static Dictionary<string, string> CreateExtensionLookup()
{
2019-02-13 08:37:18 -07:00
var dict = _mimeTypeLookup
2018-12-27 16:27:57 -07:00
.GroupBy(i => i.Value)
.ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
dict["image/jpg"] = ".jpg";
dict["image/x-png"] = ".png";
2019-02-13 08:37:18 -07:00
dict["audio/x-aac"] = ".aac";
2018-12-27 16:27:57 -07:00
return dict;
}
public static string? GetMimeType(string path) => GetMimeType(path, true);
2018-12-27 16:27:57 -07:00
/// <summary>
/// Gets the type of the MIME.
/// </summary>
public static string? GetMimeType(string path, bool enableStreamDefault)
2018-12-27 16:27:57 -07:00
{
if (path.Length == 0)
2018-12-27 16:27:57 -07:00
{
throw new ArgumentException("String can't be empty.", nameof(path));
2018-12-27 16:27:57 -07:00
}
2019-02-13 08:37:18 -07:00
var ext = Path.GetExtension(path);
2018-12-27 16:27:57 -07:00
2019-02-13 08:37:18 -07:00
if (_mimeTypeLookup.TryGetValue(ext, out string result))
2018-12-27 16:27:57 -07:00
{
return result;
}
// Catch-all for all video types that don't require specific mime types
2019-02-13 08:37:18 -07:00
if (_videoFileExtensions.Contains(ext))
2018-12-27 16:27:57 -07:00
{
2019-02-13 08:37:18 -07:00
return "video/" + ext.Substring(1);
2018-12-27 16:27:57 -07:00
}
// Type text
2020-01-09 09:07:13 -07:00
if (string.Equals(ext, ".html", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".htm", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return "text/html; charset=UTF-8";
}
2020-01-09 09:07:13 -07:00
if (string.Equals(ext, ".log", StringComparison.OrdinalIgnoreCase)
|| string.Equals(ext, ".srt", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
2019-02-13 08:37:18 -07:00
return "text/plain";
2018-12-27 16:27:57 -07:00
}
// Misc
2020-01-09 09:07:13 -07:00
if (string.Equals(ext, ".dll", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return "application/octet-stream";
}
2019-02-13 08:37:18 -07:00
return enableStreamDefault ? "application/octet-stream" : null;
2018-12-27 16:27:57 -07:00
}
public static string? ToExtension(string mimeType)
2018-12-27 16:27:57 -07:00
{
if (mimeType.Length == 0)
2018-12-27 16:27:57 -07:00
{
throw new ArgumentException("String can't be empty.", nameof(mimeType));
2018-12-27 16:27:57 -07:00
}
// handle text/html; charset=UTF-8
mimeType = mimeType.Split(';')[0];
2019-02-13 08:37:18 -07:00
if (_extensionLookup.TryGetValue(mimeType, out string result))
2018-12-27 16:27:57 -07:00
{
return result;
}
2019-02-13 08:37:18 -07:00
2018-12-27 16:27:57 -07:00
return null;
}
}
}