Skip to content

Commit 0e23de3

Browse files
committed
Add support for specifying mime type directory in file APIs
1 parent 673dddb commit 0e23de3

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

src/Dibix.Http.Server/Formatters/HttpFileResponseFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected override object FormatResult(HttpActionDefinition actionDefinition, ob
1515
if (file == null)
1616
return requestMessage.CreateResponse(HttpStatusCode.NotFound);
1717

18-
string mediaType = MimeTypes.GetMimeType(file.Type);
18+
string mediaType = MimeTypes.IsRegistered(file.Type) ? file.Type : MimeTypes.GetMimeType(file.Type);
1919

2020
HttpResponseMessage response = requestMessage.CreateResponse();
2121
response.Content = new ByteArrayContent(file.Data);

src/Dibix.Http.Server/Utilities/MimeTypes.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Dibix.Http.Server
1111
internal static class MimeTypes
1212
{
1313
private const string DefaultMimeType = "application/octet-stream";
14-
private static readonly IDictionary<string, string> _mappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
14+
private static readonly IDictionary<string, string> MimeTypeMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
1515
{
1616
{ "323", "text/h323" },
1717
{ "3g2", "video/3gpp2" },
@@ -574,6 +574,9 @@ internal static class MimeTypes
574574
{ "z", "application/x-compress" },
575575
{ "zip", "application/x-zip-compressed" }
576576
};
577+
private static readonly HashSet<string> AllMimeTypes = new HashSet<string>(MimeTypeMap.Values, StringComparer.OrdinalIgnoreCase);
578+
579+
public static bool IsRegistered(string mimeType) => AllMimeTypes.Contains(mimeType);
577580

578581
public static string GetMimeType(string fileNameOrExtension)
579582
{
@@ -586,7 +589,7 @@ public static string GetMimeType(string fileNameOrExtension)
586589
fileNameOrExtension = fileNameOrExtension.TrimStart('.');
587590

588591
string mimeType;
589-
return _mappings.TryGetValue(fileNameOrExtension, out mimeType) ? mimeType : DefaultMimeType;
592+
return MimeTypeMap.TryGetValue(fileNameOrExtension, out mimeType) ? mimeType : DefaultMimeType;
590593
}
591594
}
592595
}

0 commit comments

Comments
 (0)