diff --git a/.gitattributes b/.gitattributes index cfb5d9a3..dffe9f2a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ # Set default behaviour to automatically normalize line endings. * text=auto +*.cs text eol=crlf \ No newline at end of file diff --git a/Box.V2.Core/Box.V2.Core.csproj b/Box.V2.Core/Box.V2.Core.csproj index 8eb6c080..d9725f13 100644 --- a/Box.V2.Core/Box.V2.Core.csproj +++ b/Box.V2.Core/Box.V2.Core.csproj @@ -37,7 +37,6 @@ - diff --git a/Box.V2/Box.V2.csproj b/Box.V2/Box.V2.csproj index 99dd3b0f..16e24f4e 100644 --- a/Box.V2/Box.V2.csproj +++ b/Box.V2/Box.V2.csproj @@ -1,4 +1,4 @@ - + @@ -97,7 +97,6 @@ ..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll - @@ -376,4 +375,4 @@ - \ No newline at end of file + diff --git a/Box.V2/Utility/ContentTypeMapper.cs b/Box.V2/Utility/ContentTypeMapper.cs index 11f072a2..eda229e8 100644 --- a/Box.V2/Utility/ContentTypeMapper.cs +++ b/Box.V2/Utility/ContentTypeMapper.cs @@ -1,24 +1,51 @@ +using System.Collections.Generic; +using System.IO; + namespace Box.V2.Utility { + /// + /// Utility class for determining Content-Type header. Should only be used internally by the SDK. + /// public static class ContentTypeMapper { + private static readonly Dictionary _mimeMapping = new Dictionary() + { + { ".jpe", "image/jpeg" }, + { ".jpeg", "image/jpeg" }, + { ".jpg", "image/jpeg" }, + { ".bmp", "image/bmp" }, + { ".png", "image/png" }, + { ".gif", "image/gif" }, + { ".webp", "image/webp" }, + }; + + /// + /// Get Content-Type header from a filename. Supports most common image formats. Should only be used internally by the SDK. + /// + /// full filename with extension + /// Content-Type header as a string public static string GetContentTypeFromFilename(string filename) { const string DefaultContentType = "application/octet-stream"; var contentType = DefaultContentType; -#if NETSTANDARD2_0 - var provider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider(); - - if (provider.TryGetContentType(filename, out var contentTypeFromFile)) + if (TryGetContentType(filename, out var contentTypeFromFile)) { contentType = contentTypeFromFile; } -#else - contentType = System.Web.MimeMapping.GetMimeMapping(filename); -#endif return contentType; } + + private static bool TryGetContentType(string path, out string contentType) + { + var extension = Path.GetExtension(path); + if (extension == null) + { + contentType = null; + return false; + } + return _mimeMapping.TryGetValue(extension, out contentType); + } } }