2019-01-13 13:02:23 -07:00
|
|
|
using System;
|
2018-12-27 16:27:57 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-01-13 12:26:15 -07:00
|
|
|
using MediaBrowser.Model.Extensions;
|
2018-12-27 16:27:57 -07:00
|
|
|
|
|
|
|
namespace MediaBrowser.Model.Net
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Class MimeTypes
|
|
|
|
/// </summary>
|
|
|
|
public static class MimeTypes
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-02-13 08:37:18 -07:00
|
|
|
/// 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
|
|
|
{
|
2019-02-13 08:37:18 -07: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"
|
|
|
|
};
|
2018-12-27 16:27:57 -07:00
|
|
|
|
|
|
|
// http://en.wikipedia.org/wiki/Internet_media_type
|
|
|
|
// 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
|
|
|
|
{ ".cbz", "application/x-cbz" },
|
|
|
|
{ ".cbr", "application/epub+zip" },
|
|
|
|
{ ".eot", "application/vnd.ms-fontobject" },
|
|
|
|
{ ".epub", "application/epub+zip" },
|
|
|
|
{ ".js", "application/x-javascript" },
|
|
|
|
{ ".json", "application/json" },
|
|
|
|
{ ".map", "application/x-javascript" },
|
|
|
|
{ ".pdf", "application/pdf" },
|
|
|
|
{ ".ttml", "application/ttml+xml" },
|
|
|
|
{ ".m3u8", "application/x-mpegURL" },
|
|
|
|
{ ".mobi", "application/x-mobipocket-ebook" },
|
|
|
|
{ ".xml", "application/xml" },
|
|
|
|
|
|
|
|
// Type image
|
|
|
|
{ ".jpg", "image/jpeg" },
|
|
|
|
{ ".jpeg", "image/jpeg" },
|
|
|
|
{ ".tbn", "image/jpeg" },
|
|
|
|
{ ".png", "image/png" },
|
|
|
|
{ ".gif", "image/gif" },
|
|
|
|
{ ".tiff", "image/tiff" },
|
|
|
|
{ ".webp", "image/webp" },
|
|
|
|
{ ".ico", "image/vnd.microsoft.icon" },
|
|
|
|
{ ".svg", "image/svg+xml" },
|
|
|
|
{ ".svgz", "image/svg+xml" },
|
|
|
|
|
|
|
|
// Type font
|
|
|
|
{ ".ttf" , "font/ttf" },
|
|
|
|
{ ".woff" , "font/woff" },
|
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" },
|
|
|
|
{ ".txt", "text/plain" },
|
|
|
|
{ ".vtt", "text/vtt" },
|
2018-12-27 16:27:57 -07:00
|
|
|
|
2019-02-13 08:37:18 -07:00
|
|
|
// Type video
|
|
|
|
{ ".mpg", "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" },
|
|
|
|
{ ".m4s", "video/mp4" },
|
|
|
|
{ ".3gp", "video/3gpp" },
|
|
|
|
{ ".3g2", "video/3gpp2" },
|
|
|
|
{ ".mpd", "video/vnd.mpeg.dash.mpd" },
|
|
|
|
{ ".ts", "video/mp2t" },
|
2018-12-27 16:27:57 -07:00
|
|
|
|
2019-02-13 08:37:18 -07:00
|
|
|
// Type audio
|
|
|
|
{ ".mp3", "audio/mpeg" },
|
|
|
|
{ ".m4a", "audio/mp4" },
|
|
|
|
{ ".aac", "audio/mp4" },
|
|
|
|
{ ".webma", "audio/webm" },
|
|
|
|
{ ".wav", "audio/wav" },
|
|
|
|
{ ".wma", "audio/x-ms-wma" },
|
|
|
|
{ ".ogg", "audio/ogg" },
|
|
|
|
{ ".oga", "audio/ogg" },
|
|
|
|
{ ".opus", "audio/ogg" },
|
|
|
|
{ ".ac3", "audio/ac3" },
|
|
|
|
{ ".dsf", "audio/dsf" },
|
|
|
|
{ ".m4b", "audio/m4b" },
|
|
|
|
{ ".xsp", "audio/xsp" },
|
|
|
|
{ ".dsp", "audio/dsp" },
|
2019-08-31 22:15:34 -07:00
|
|
|
{ ".flac", "audio/flac" },
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-13 08:37:18 -07:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(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
|
2019-02-13 08:37:18 -07:00
|
|
|
if (StringHelper.EqualsIgnoreCase(ext, ".html")
|
|
|
|
|| StringHelper.EqualsIgnoreCase(ext, ".htm"))
|
2018-12-27 16:27:57 -07:00
|
|
|
{
|
|
|
|
return "text/html; charset=UTF-8";
|
|
|
|
}
|
|
|
|
|
2019-02-13 08:37:18 -07:00
|
|
|
if (StringHelper.EqualsIgnoreCase(ext, ".log")
|
|
|
|
|| StringHelper.EqualsIgnoreCase(ext, ".srt"))
|
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
|
|
|
|
if (StringHelper.EqualsIgnoreCase(ext, ".dll"))
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(mimeType))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|