From dce7706382aaacae5ee74ba5048b76269e5390e5 Mon Sep 17 00:00:00 2001 From: LukePulverenti Luke Pulverenti luke pulverenti Date: Thu, 9 Aug 2012 11:35:15 -0400 Subject: [PATCH] Added a MimeTypes utility class --- .../MediaBrowser.Common.csproj | 1 + MediaBrowser.Common/Net/MimeTypes.cs | 114 ++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 MediaBrowser.Common/Net/MimeTypes.cs diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index d647903ad..7f6c66748 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -58,6 +58,7 @@ + diff --git a/MediaBrowser.Common/Net/MimeTypes.cs b/MediaBrowser.Common/Net/MimeTypes.cs new file mode 100644 index 000000000..af2843f82 --- /dev/null +++ b/MediaBrowser.Common/Net/MimeTypes.cs @@ -0,0 +1,114 @@ +using System; +using System.IO; + +namespace MediaBrowser.Common.Net +{ + public static class MimeTypes + { + public static string GetMimeType(string path) + { + string ext = Path.GetExtension(path); + + // http://en.wikipedia.org/wiki/Internet_media_type + // Add more as needed + + // Type video + if (ext.EndsWith("mpg", StringComparison.OrdinalIgnoreCase) || ext.EndsWith("mpeg", StringComparison.OrdinalIgnoreCase)) + { + return "video/mpeg"; + } + else if (ext.EndsWith("mp4", StringComparison.OrdinalIgnoreCase)) + { + return "video/mp4"; + } + else if (ext.EndsWith("ogv", StringComparison.OrdinalIgnoreCase)) + { + return "video/ogg"; + } + else if (ext.EndsWith("mov", StringComparison.OrdinalIgnoreCase)) + { + return "video/quicktime"; + } + else if (ext.EndsWith("webm", StringComparison.OrdinalIgnoreCase)) + { + return "video/webm"; + } + else if (ext.EndsWith("mkv", StringComparison.OrdinalIgnoreCase)) + { + return "video/x-matroska"; + } + else if (ext.EndsWith("wmv", StringComparison.OrdinalIgnoreCase)) + { + return "video/x-ms-wmv"; + } + else if (ext.EndsWith("flv", StringComparison.OrdinalIgnoreCase)) + { + return "video/x-flv"; + } + else if (ext.EndsWith("avi", StringComparison.OrdinalIgnoreCase)) + { + return "video/avi"; + } + + // Type text + else if (ext.EndsWith("css", StringComparison.OrdinalIgnoreCase)) + { + return "text/css"; + } + else if (ext.EndsWith("csv", StringComparison.OrdinalIgnoreCase)) + { + return "text/csv"; + } + else if (ext.EndsWith("html", StringComparison.OrdinalIgnoreCase) || ext.EndsWith("html", StringComparison.OrdinalIgnoreCase)) + { + return "text/html"; + } + else if (ext.EndsWith("txt", StringComparison.OrdinalIgnoreCase)) + { + return "text/plain"; + } + + // Type image + else if (ext.EndsWith("gif", StringComparison.OrdinalIgnoreCase)) + { + return "image/gif"; + } + else if (ext.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) || ext.EndsWith("jpeg", StringComparison.OrdinalIgnoreCase)) + { + return "image/jpeg"; + } + else if (ext.EndsWith("png", StringComparison.OrdinalIgnoreCase)) + { + return "image/png"; + } + else if (ext.EndsWith("ico", StringComparison.OrdinalIgnoreCase)) + { + return "image/vnd.microsoft.icon"; + } + + // Type audio + else if (ext.EndsWith("mp3", StringComparison.OrdinalIgnoreCase)) + { + return "audio/mpeg"; + } + else if (ext.EndsWith("m4a", StringComparison.OrdinalIgnoreCase) || ext.EndsWith("aac", StringComparison.OrdinalIgnoreCase)) + { + return "audio/mp4"; + } + else if (ext.EndsWith("webma", StringComparison.OrdinalIgnoreCase)) + { + return "audio/webm"; + } + else if (ext.EndsWith("wav", StringComparison.OrdinalIgnoreCase)) + { + return "audio/wav"; + } + else if (ext.EndsWith("ogg", StringComparison.OrdinalIgnoreCase) || ext.EndsWith("oga", StringComparison.OrdinalIgnoreCase)) + { + return "audio/ogg"; + } + + throw new InvalidOperationException("Argument not supported: " + path); + } + } +}