-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'sdk-3886-ai-api' of https://github.com/box/box-windows-…
…sdk-v2 into sdk-3886-ai-api
- Loading branch information
Showing
4 changed files
with
37 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# Set default behaviour to automatically normalize line endings. | ||
* text=auto | ||
*.cs text eol=crlf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,51 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
namespace Box.V2.Utility | ||
{ | ||
/// <summary> | ||
/// Utility class for determining Content-Type header. Should only be used internally by the SDK. | ||
/// </summary> | ||
public static class ContentTypeMapper | ||
{ | ||
private static readonly Dictionary<string, string> _mimeMapping = new Dictionary<string, string>() | ||
{ | ||
{ ".jpe", "image/jpeg" }, | ||
{ ".jpeg", "image/jpeg" }, | ||
{ ".jpg", "image/jpeg" }, | ||
{ ".bmp", "image/bmp" }, | ||
{ ".png", "image/png" }, | ||
{ ".gif", "image/gif" }, | ||
{ ".webp", "image/webp" }, | ||
}; | ||
|
||
/// <summary> | ||
/// Get Content-Type header from a filename. Supports most common image formats. Should only be used internally by the SDK. | ||
/// </summary> | ||
/// <param name="filename">full filename with extension</param> | ||
/// <returns>Content-Type header as a string</returns> | ||
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); | ||
} | ||
} | ||
} |