jellyfin/MediaBrowser.Model/Net/MimeTypes.cs

360 lines
11 KiB
C#
Raw Normal View History

2015-05-31 18:22:51 +00:00
using MediaBrowser.Model.Extensions;
using System;
2014-09-10 00:28:59 +00:00
using System.Collections.Generic;
2013-02-21 01:33:05 +00:00
using System.IO;
2014-09-10 00:28:59 +00:00
using System.Linq;
2013-02-21 01:33:05 +00:00
2014-12-26 17:45:06 +00:00
namespace MediaBrowser.Model.Net
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// Class MimeTypes
/// </summary>
public static class MimeTypes
{
2014-09-10 00:28:59 +00:00
/// <summary>
/// Any extension in this list is considered a video file - can be added to at runtime for extensibility
/// </summary>
2017-08-19 19:43:35 +00:00
private static readonly string[] VideoFileExtensions = new string[]
2014-09-10 00:28:59 +00:00
{
".mkv",
".m2t",
".m2ts",
".img",
".iso",
".mk3d",
".ts",
".rmvb",
".mov",
".avi",
".mpg",
".mpeg",
".wmv",
".mp4",
".divx",
".dvr-ms",
".wtv",
".ogm",
".ogv",
".asf",
".m4v",
".flv",
".f4v",
".3gp",
".webm",
".mts",
".m2v",
".rec"
};
2015-05-31 18:22:51 +00:00
private static Dictionary<string, string> GetVideoFileExtensionsDictionary()
{
Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (string ext in VideoFileExtensions)
{
dict[ext] = ext;
}
return dict;
}
private static readonly Dictionary<string, string> VideoFileExtensionsDictionary = GetVideoFileExtensionsDictionary();
2014-09-10 00:28:59 +00:00
2014-12-26 17:45:06 +00:00
// http://en.wikipedia.org/wiki/Internet_media_type
// Add more as needed
2014-09-10 00:28:59 +00:00
2015-05-31 18:22:51 +00:00
private static Dictionary<string, string> GetMimeTypeLookup()
{
Dictionary<string, string> dict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
dict.Add(".jpg", "image/jpeg");
dict.Add(".jpeg", "image/jpeg");
dict.Add(".tbn", "image/jpeg");
dict.Add(".png", "image/png");
dict.Add(".gif", "image/gif");
2017-06-24 18:31:13 +00:00
dict.Add(".tiff", "image/tiff");
2015-05-31 18:22:51 +00:00
dict.Add(".webp", "image/webp");
dict.Add(".ico", "image/vnd.microsoft.icon");
dict.Add(".mpg", "video/mpeg");
dict.Add(".mpeg", "video/mpeg");
dict.Add(".ogv", "video/ogg");
dict.Add(".mov", "video/quicktime");
dict.Add(".webm", "video/webm");
dict.Add(".mkv", "video/x-matroska");
dict.Add(".wmv", "video/x-ms-wmv");
dict.Add(".flv", "video/x-flv");
dict.Add(".avi", "video/x-msvideo");
dict.Add(".asf", "video/x-ms-asf");
dict.Add(".m4v", "video/x-m4v");
return dict;
}
private static readonly Dictionary<string, string> MimeTypeLookup = GetMimeTypeLookup();
2014-12-28 17:59:40 +00:00
2014-12-29 20:18:48 +00:00
private static readonly Dictionary<string, string> ExtensionLookup = CreateExtensionLookup();
private static Dictionary<string, string> CreateExtensionLookup()
{
var dict = MimeTypeLookup
.GroupBy(i => i.Value)
.ToDictionary(x => x.Key, x => x.First().Key, StringComparer.OrdinalIgnoreCase);
dict["image/jpg"] = ".jpg";
2016-10-19 06:29:00 +00:00
dict["image/x-png"] = ".png";
2014-12-29 20:18:48 +00:00
return dict;
}
2014-09-10 00:28:59 +00:00
2017-08-04 20:29:34 +00:00
public static string GetMimeType(string path)
{
return GetMimeType(path, true);
}
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the type of the MIME.
/// </summary>
2017-08-04 20:29:34 +00:00
public static string GetMimeType(string path, bool enableStreamDefault)
2013-02-21 01:33:05 +00:00
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
}
2014-09-10 00:28:59 +00:00
2013-02-21 01:33:05 +00:00
var ext = Path.GetExtension(path) ?? string.Empty;
2014-12-26 17:45:06 +00:00
string result;
if (MimeTypeLookup.TryGetValue(ext, out result))
2013-02-21 01:33:05 +00:00
{
2014-12-26 17:45:06 +00:00
return result;
2013-02-21 01:33:05 +00:00
}
2014-12-26 17:45:06 +00:00
// Type video
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".3gp"))
2013-02-21 01:33:05 +00:00
{
return "video/3gpp";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".3g2"))
2013-02-21 01:33:05 +00:00
{
return "video/3gpp2";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".ts"))
2013-02-21 01:33:05 +00:00
{
return "video/mp2t";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".mpd"))
2014-10-12 17:31:41 +00:00
{
return "video/vnd.mpeg.dash.mpd";
}
2013-02-21 01:33:05 +00:00
2014-09-10 00:28:59 +00:00
// Catch-all for all video types that don't require specific mime types
if (VideoFileExtensionsDictionary.ContainsKey(ext))
{
return "video/" + ext.TrimStart('.').ToLower();
}
2013-02-21 01:33:05 +00:00
// Type text
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".css"))
2013-02-21 01:33:05 +00:00
{
return "text/css";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".csv"))
2013-02-21 01:33:05 +00:00
{
return "text/csv";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".html"))
{
return "text/html; charset=UTF-8";
}
if (StringHelper.EqualsIgnoreCase(ext, ".htm"))
2013-02-21 01:33:05 +00:00
{
return "text/html; charset=UTF-8";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".txt"))
2013-02-21 01:33:05 +00:00
{
return "text/plain";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".xml"))
2013-02-21 01:33:05 +00:00
{
return "application/xml";
}
2013-08-31 18:04:06 +00:00
// Type document
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".pdf"))
2013-08-31 18:04:06 +00:00
{
return "application/pdf";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".mobi"))
2013-08-31 18:04:06 +00:00
{
return "application/x-mobipocket-ebook";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".epub"))
{
return "application/epub+zip";
}
if (StringHelper.EqualsIgnoreCase(ext, ".cbz"))
2013-08-31 18:04:06 +00:00
{
return "application/epub+zip";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".cbr"))
2013-08-31 18:20:10 +00:00
{
2015-05-31 18:22:51 +00:00
return "application/epub+zip";
2013-08-31 18:20:10 +00:00
}
2013-08-31 18:04:06 +00:00
2014-09-10 00:28:59 +00:00
// Type audio
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".mp3"))
2013-02-21 01:33:05 +00:00
{
return "audio/mpeg";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".m4a"))
2013-02-21 01:33:05 +00:00
{
return "audio/mp4";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".aac"))
{
return "audio/mp4";
}
if (StringHelper.EqualsIgnoreCase(ext, ".webma"))
2013-02-21 01:33:05 +00:00
{
return "audio/webm";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".wav"))
2013-02-21 01:33:05 +00:00
{
return "audio/wav";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".wma"))
2013-02-21 01:33:05 +00:00
{
return "audio/x-ms-wma";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".flac"))
2013-02-21 01:33:05 +00:00
{
return "audio/flac";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".aac"))
2013-02-21 01:33:05 +00:00
{
return "audio/x-aac";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".ogg"))
{
return "audio/ogg";
}
if (StringHelper.EqualsIgnoreCase(ext, ".oga"))
2013-02-21 01:33:05 +00:00
{
return "audio/ogg";
2015-12-24 18:43:45 +00:00
}
if (StringHelper.EqualsIgnoreCase(ext, ".opus"))
{
2016-08-24 15:48:23 +00:00
return "audio/ogg";
2017-02-10 20:06:52 +00:00
}
if (StringHelper.EqualsIgnoreCase(ext, ".ac3"))
{
return "audio/ac3";
}
2017-06-15 17:22:05 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".dsf"))
{
return "audio/dsf";
}
if (StringHelper.EqualsIgnoreCase(ext, ".m4b"))
{
return "audio/m4b";
}
if (StringHelper.EqualsIgnoreCase(ext, ".xsp"))
{
return "audio/xsp";
}
if (StringHelper.EqualsIgnoreCase(ext, ".dsp"))
{
return "audio/dsp";
}
2013-02-21 01:33:05 +00:00
// Playlists
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".m3u8"))
2013-02-21 01:33:05 +00:00
{
return "application/x-mpegURL";
}
// Misc
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".dll"))
2013-02-21 01:33:05 +00:00
{
return "application/octet-stream";
2013-02-21 01:33:05 +00:00
}
// Web
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".js"))
2013-02-21 01:33:05 +00:00
{
return "application/x-javascript";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".json"))
2014-08-22 15:47:44 +00:00
{
2014-12-26 17:45:06 +00:00
return "application/json";
2014-08-22 15:47:44 +00:00
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".map"))
{
return "application/x-javascript";
}
2013-02-21 01:33:05 +00:00
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".woff"))
2013-02-21 01:33:05 +00:00
{
return "font/woff";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".ttf"))
2013-02-21 01:33:05 +00:00
{
return "font/ttf";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".eot"))
2013-02-21 01:33:05 +00:00
{
return "application/vnd.ms-fontobject";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".svg"))
{
return "image/svg+xml";
}
if (StringHelper.EqualsIgnoreCase(ext, ".svgz"))
2013-02-21 01:33:05 +00:00
{
return "image/svg+xml";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".srt"))
2013-12-26 03:44:26 +00:00
{
return "text/plain";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".vtt"))
{
return "text/vtt";
}
2015-05-31 18:22:51 +00:00
if (StringHelper.EqualsIgnoreCase(ext, ".ttml"))
2014-08-06 02:26:12 +00:00
{
return "application/ttml+xml";
}
2017-08-04 20:29:34 +00:00
if (enableStreamDefault)
{
return "application/octet-stream";
}
return null;
2013-02-21 01:33:05 +00:00
}
2014-10-11 20:38:13 +00:00
public static string ToExtension(string mimeType)
{
2014-12-28 17:59:40 +00:00
if (string.IsNullOrEmpty(mimeType))
{
throw new ArgumentNullException("mimeType");
}
2017-03-07 19:05:44 +00:00
// handle text/html; charset=UTF-8
mimeType = mimeType.Split(';')[0];
2014-12-28 17:59:40 +00:00
string result;
if (ExtensionLookup.TryGetValue(mimeType, out result))
{
return result;
}
2015-09-10 18:28:22 +00:00
return null;
2014-10-11 20:38:13 +00:00
}
2013-02-21 01:33:05 +00:00
}
2015-09-20 17:34:05 +00:00
}