jellyfin/MediaBrowser.Model/Net/MimeTypes.cs

287 lines
9.3 KiB
C#
Raw Normal View History

2013-02-21 01:33:05 +00:00
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>
private static readonly List<string> VideoFileExtensions = new List<string>
{
".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"
};
private static readonly Dictionary<string, string> VideoFileExtensionsDictionary = VideoFileExtensions.ToDictionary(i => i, StringComparer.OrdinalIgnoreCase);
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
2014-12-26 17:45:06 +00:00
private static readonly Dictionary<string, string> MimeTypeLookup =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
2014-09-10 00:28:59 +00:00
{
2014-12-26 17:45:06 +00:00
{".jpg", "image/jpeg"},
{".jpeg", "image/jpeg"},
{".tbn", "image/jpeg"},
{".png", "image/png"},
{".gif", "image/gif"},
{".webp", "image/webp"},
{".ico", "image/vnd.microsoft.icon"},
{".mpg", "video/mpeg"},
{".mpeg", "video/mpeg"},
{".ogv", "video/ogg"},
{".mov", "video/quicktime"},
{".webm", "video/webm"},
{".mkv", "video/x-matroska"},
{".wmv", "video/x-ms-wmv"},
{".flv", "video/x-flv"},
{".avi", "video/x-msvideo"},
{".asf", "video/x-ms-asf"},
{".m4v", "video/x-m4v"}
};
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";
return dict;
}
2014-09-10 00:28:59 +00:00
2013-02-21 01:33:05 +00:00
/// <summary>
/// Gets the type of the MIME.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>System.String.</returns>
2014-12-26 17:45:06 +00:00
/// <exception cref="ArgumentNullException">path</exception>
/// <exception cref="InvalidOperationException">Argument not supported: + path</exception>
2013-02-21 01:33:05 +00:00
public static string GetMimeType(string path)
{
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
2013-02-21 01:33:05 +00:00
if (ext.Equals(".3gp", StringComparison.OrdinalIgnoreCase))
{
return "video/3gpp";
}
if (ext.Equals(".3g2", StringComparison.OrdinalIgnoreCase))
{
return "video/3gpp2";
}
if (ext.Equals(".ts", StringComparison.OrdinalIgnoreCase))
{
return "video/mp2t";
}
2014-10-12 17:31:41 +00:00
if (ext.Equals(".mpd", StringComparison.OrdinalIgnoreCase))
{
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
if (ext.Equals(".css", StringComparison.OrdinalIgnoreCase))
{
return "text/css";
}
if (ext.Equals(".csv", StringComparison.OrdinalIgnoreCase))
{
return "text/csv";
}
if (ext.Equals(".html", StringComparison.OrdinalIgnoreCase) || ext.Equals(".htm", StringComparison.OrdinalIgnoreCase))
{
return "text/html; charset=UTF-8";
}
if (ext.Equals(".txt", StringComparison.OrdinalIgnoreCase))
{
return "text/plain";
}
if (ext.Equals(".xml", StringComparison.OrdinalIgnoreCase))
{
return "application/xml";
}
2013-08-31 18:04:06 +00:00
// Type document
if (ext.Equals(".pdf", StringComparison.OrdinalIgnoreCase))
{
return "application/pdf";
}
if (ext.Equals(".mobi", StringComparison.OrdinalIgnoreCase))
{
return "application/x-mobipocket-ebook";
}
if (ext.Equals(".epub", StringComparison.OrdinalIgnoreCase))
{
return "application/epub+zip";
}
2013-08-31 18:20:10 +00:00
if (ext.Equals(".cbz", StringComparison.OrdinalIgnoreCase) || ext.Equals(".cbr", StringComparison.OrdinalIgnoreCase))
{
return "application/x-cdisplay";
}
2013-08-31 18:04:06 +00:00
2014-09-10 00:28:59 +00:00
// Type audio
2013-02-21 01:33:05 +00:00
if (ext.Equals(".mp3", StringComparison.OrdinalIgnoreCase))
{
return "audio/mpeg";
}
if (ext.Equals(".m4a", StringComparison.OrdinalIgnoreCase) || ext.Equals(".aac", StringComparison.OrdinalIgnoreCase))
{
return "audio/mp4";
}
if (ext.Equals(".webma", StringComparison.OrdinalIgnoreCase))
{
return "audio/webm";
}
if (ext.Equals(".wav", StringComparison.OrdinalIgnoreCase))
{
return "audio/wav";
}
if (ext.Equals(".wma", StringComparison.OrdinalIgnoreCase))
{
return "audio/x-ms-wma";
}
if (ext.Equals(".flac", StringComparison.OrdinalIgnoreCase))
{
return "audio/flac";
}
if (ext.Equals(".aac", StringComparison.OrdinalIgnoreCase))
{
return "audio/x-aac";
}
if (ext.Equals(".ogg", StringComparison.OrdinalIgnoreCase) || ext.Equals(".oga", StringComparison.OrdinalIgnoreCase))
{
return "audio/ogg";
}
// Playlists
if (ext.Equals(".m3u8", StringComparison.OrdinalIgnoreCase))
{
return "application/x-mpegURL";
}
// Misc
if (ext.Equals(".dll", StringComparison.OrdinalIgnoreCase))
{
return "application/octet-stream";
2013-02-21 01:33:05 +00:00
}
// Web
if (ext.Equals(".js", StringComparison.OrdinalIgnoreCase))
{
return "application/x-javascript";
}
2014-08-22 15:47:44 +00:00
if (ext.Equals(".json", StringComparison.OrdinalIgnoreCase))
{
2014-12-26 17:45:06 +00:00
return "application/json";
2014-08-22 15:47:44 +00:00
}
if (ext.Equals(".map", StringComparison.OrdinalIgnoreCase))
{
return "application/x-javascript";
}
2013-02-21 01:33:05 +00:00
if (ext.Equals(".woff", StringComparison.OrdinalIgnoreCase))
{
return "font/woff";
}
if (ext.Equals(".ttf", StringComparison.OrdinalIgnoreCase))
{
return "font/ttf";
}
if (ext.Equals(".eot", StringComparison.OrdinalIgnoreCase))
{
return "application/vnd.ms-fontobject";
}
if (ext.Equals(".svg", StringComparison.OrdinalIgnoreCase) || ext.Equals(".svgz", StringComparison.OrdinalIgnoreCase))
{
return "image/svg+xml";
}
2013-12-26 03:44:26 +00:00
if (ext.Equals(".srt", StringComparison.OrdinalIgnoreCase))
{
return "text/plain";
}
if (ext.Equals(".vtt", StringComparison.OrdinalIgnoreCase))
{
return "text/vtt";
}
2014-08-06 02:26:12 +00:00
if (ext.Equals(".ttml", StringComparison.OrdinalIgnoreCase))
{
return "application/ttml+xml";
}
2015-03-04 17:22:55 +00:00
return "application/octet-stream";
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");
}
string result;
if (ExtensionLookup.TryGetValue(mimeType, out result))
{
return result;
}
throw new ArgumentNullException("Unable to determine extension for mimeType: " + mimeType);
2014-10-11 20:38:13 +00:00
}
2013-02-21 01:33:05 +00:00
}
}