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;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace MediaBrowser.Model.Net
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-04-05 16:10:56 +00:00
|
|
|
/// Class MimeTypes.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
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
|
|
|
|
|
|
|
// http://en.wikipedia.org/wiki/Internet_media_type
|
2020-05-03 22:23:25 +00: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 23:27:57 +00:00
|
|
|
// Add more as needed
|
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-03 22:23:25 +00:00
|
|
|
{ ".7z", "application/x-7z-compressed" },
|
|
|
|
{ ".azw", "application/vnd.amazon.ebook" },
|
2020-05-05 11:25:32 +00:00
|
|
|
{ ".azw3", "application/vnd.amazon.ebook" },
|
2019-02-13 15:37:18 +00: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 22:23:25 +00:00
|
|
|
{ ".m3u8", "application/x-mpegURL" },
|
2019-02-13 15:37:18 +00:00
|
|
|
{ ".map", "application/x-javascript" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".mobi", "application/x-mobipocket-ebook" },
|
2020-05-15 13:38:26 +00:00
|
|
|
{ ".opf", "application/oebps-package+xml" },
|
2019-02-13 15:37:18 +00:00
|
|
|
{ ".pdf", "application/pdf" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".rar", "application/vnd.rar" },
|
|
|
|
{ ".srt", "application/x-subrip" },
|
2019-02-13 15:37:18 +00:00
|
|
|
{ ".ttml", "application/ttml+xml" },
|
2020-03-17 03:49:32 +00:00
|
|
|
{ ".wasm", "application/wasm" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".xml", "application/xml" },
|
|
|
|
{ ".zip", "application/zip" },
|
2019-02-13 15:37:18 +00:00
|
|
|
|
|
|
|
// Type image
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".bmp", "image/bmp" },
|
|
|
|
{ ".gif", "image/gif" },
|
|
|
|
{ ".ico", "image/vnd.microsoft.icon" },
|
2019-02-13 15:37:18 +00:00
|
|
|
{ ".jpg", "image/jpeg" },
|
|
|
|
{ ".jpeg", "image/jpeg" },
|
|
|
|
{ ".png", "image/png" },
|
|
|
|
{ ".svg", "image/svg+xml" },
|
|
|
|
{ ".svgz", "image/svg+xml" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".tbn", "image/jpeg" },
|
|
|
|
{ ".tif", "image/tiff" },
|
|
|
|
{ ".tiff", "image/tiff" },
|
|
|
|
{ ".webp", "image/webp" },
|
2019-02-13 15:37:18 +00:00
|
|
|
|
|
|
|
// Type font
|
|
|
|
{ ".ttf" , "font/ttf" },
|
|
|
|
{ ".woff" , "font/woff" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".woff2" , "font/woff2" },
|
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" },
|
|
|
|
{ ".css", "text/css" },
|
|
|
|
{ ".csv", "text/csv" },
|
2020-05-20 20:46:33 +00:00
|
|
|
{ ".edl", "text/plain" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".rtf", "text/rtf" },
|
2019-02-13 15:37:18 +00:00
|
|
|
{ ".txt", "text/plain" },
|
|
|
|
{ ".vtt", "text/vtt" },
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2019-02-13 15:37:18 +00:00
|
|
|
// Type video
|
|
|
|
{ ".3gp", "video/3gpp" },
|
|
|
|
{ ".3g2", "video/3gpp2" },
|
2020-05-03 22:23:25 +00: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 15:37:18 +00:00
|
|
|
{ ".mpd", "video/vnd.mpeg.dash.mpd" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".ogv", "video/ogg" },
|
2019-02-13 15:37:18 +00:00
|
|
|
{ ".ts", "video/mp2t" },
|
2020-05-03 22:23:25 +00:00
|
|
|
{ ".webm", "video/webm" },
|
|
|
|
{ ".wmv", "video/x-ms-wmv" },
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2019-02-13 15:37:18 +00:00
|
|
|
// Type audio
|
|
|
|
{ ".aac", "audio/mp4" },
|
|
|
|
{ ".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
|
|
|
{ ".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-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
|
|
|
|
2019-02-13 15:37:18 +00:00
|
|
|
private static readonly Dictionary<string, string> _extensionLookup = CreateExtensionLookup();
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
private static Dictionary<string, string> CreateExtensionLookup()
|
|
|
|
{
|
2019-02-13 15:37:18 +00:00
|
|
|
var dict = _mimeTypeLookup
|
2018-12-27 23:27:57 +00: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 15:37:18 +00:00
|
|
|
dict["audio/x-aac"] = ".aac";
|
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
2020-04-05 16:10:56 +00:00
|
|
|
public static string? GetMimeType(string path) => GetMimeType(path, true);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the type of the MIME.
|
|
|
|
/// </summary>
|
2020-04-05 16:10:56 +00:00
|
|
|
public static string? GetMimeType(string path, bool enableStreamDefault)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-04-05 16:10:56 +00:00
|
|
|
if (path.Length == 0)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-04-05 16:10:56 +00:00
|
|
|
throw new ArgumentException("String can't be empty.", nameof(path));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 15:37:18 +00:00
|
|
|
var ext = Path.GetExtension(path);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2019-02-13 15:37:18 +00:00
|
|
|
if (_mimeTypeLookup.TryGetValue(ext, out string result))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Catch-all for all video types that don't require specific mime types
|
2019-02-13 15:37:18 +00:00
|
|
|
if (_videoFileExtensions.Contains(ext))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2019-02-13 15:37:18 +00:00
|
|
|
return "video/" + ext.Substring(1);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type text
|
2020-01-09 16:07:13 +00:00
|
|
|
if (string.Equals(ext, ".html", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| string.Equals(ext, ".htm", StringComparison.OrdinalIgnoreCase))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
return "text/html; charset=UTF-8";
|
|
|
|
}
|
|
|
|
|
2020-01-09 16:07:13 +00:00
|
|
|
if (string.Equals(ext, ".log", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| string.Equals(ext, ".srt", StringComparison.OrdinalIgnoreCase))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2019-02-13 15:37:18 +00:00
|
|
|
return "text/plain";
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Misc
|
2020-01-09 16:07:13 +00:00
|
|
|
if (string.Equals(ext, ".dll", StringComparison.OrdinalIgnoreCase))
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
return "application/octet-stream";
|
|
|
|
}
|
|
|
|
|
2019-02-13 15:37:18 +00:00
|
|
|
return enableStreamDefault ? "application/octet-stream" : null;
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:10:56 +00:00
|
|
|
public static string? ToExtension(string mimeType)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-04-05 16:10:56 +00:00
|
|
|
if (mimeType.Length == 0)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-04-05 16:10:56 +00:00
|
|
|
throw new ArgumentException("String can't be empty.", nameof(mimeType));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle text/html; charset=UTF-8
|
|
|
|
mimeType = mimeType.Split(';')[0];
|
|
|
|
|
2019-02-13 15:37:18 +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
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|