Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some const strings, fixed some typos and add default value for GetExtension method #95

Merged
merged 2 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 16 additions & 24 deletions src/MimeTypes/MimeTypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace MimeTypes
{
public static class MimeTypeMap
{
private const string Dot = ".";
private const string DefaultMimeType = "application/octet-stream";
private static readonly Lazy<IDictionary<string, string>> _mappings = new Lazy<IDictionary<string, string>>(BuildMappings);

private static IDictionary<string, string> BuildMappings()
Expand Down Expand Up @@ -667,7 +669,7 @@ private static IDictionary<string, string> BuildMappings()
{"application/x-x509-ca-cert", ".cer"},
{"application/x-zip-compressed", ".zip"},
{"application/xhtml+xml", ".xhtml"},
{"application/xml", ".xml"}, // anomoly, .xml -> text/xml, but application/xml -> many thingss, but all are xml, so safest is .xml
{"application/xml", ".xml"}, // anomaly, .xml -> text/xml, but application/xml -> many things, but all are xml, so safest is .xml
{"audio/aac", ".AAC"},
{"audio/aiff", ".aiff"},
{"audio/basic", ".snd"},
Expand All @@ -680,8 +682,8 @@ private static IDictionary<string, string> BuildMappings()
{"image/bmp", ".bmp"},
{"image/jpeg", ".jpg"},
{"image/pict", ".pic"},
{"image/png", ".png"}, //Defined in [RFC-2045], [RFC-2048]
{"image/x-png", ".png"}, //See https://www.w3.org/TR/PNG/#A-Media-type :"It is recommended that implementations also recognize the media type "image/x-png"."
{"image/png", ".png"}, // Defined in [RFC-2045], [RFC-2048]
{"image/x-png", ".png"}, // See https://www.w3.org/TR/PNG/#A-Media-type :"It is recommended that implementations also recognize the media type "image/x-png"."
{"image/tiff", ".tiff"},
{"image/x-macpaint", ".mac"},
{"image/x-quicktime", ".qti"},
Expand Down Expand Up @@ -723,50 +725,40 @@ public static string GetMimeType(string extension)
{
if (extension == null)
{
throw new ArgumentNullException("extension");
throw new ArgumentNullException(nameof(extension));
}

if (!extension.StartsWith("."))
if (!extension.StartsWith(Dot))
{
extension = "." + extension;
extension = Dot + extension;
}

string mime;

return _mappings.Value.TryGetValue(extension, out mime) ? mime : "application/octet-stream";
return _mappings.Value.TryGetValue(extension, out string mime) ? mime : DefaultMimeType;
}

public static string GetExtension(string mimeType)
{
return GetExtension(mimeType, true);
}

public static string GetExtension(string mimeType, bool throwErrorIfNotFound)
public static string GetExtension(string mimeType, bool throwErrorIfNotFound = true)
{
if (mimeType == null)
{
throw new ArgumentNullException("mimeType");
throw new ArgumentNullException(nameof(mimeType));
}

if (mimeType.StartsWith("."))
{
throw new ArgumentException("Requested mime type is not valid: " + mimeType);
}

string extension;

if (_mappings.Value.TryGetValue(mimeType, out extension))
if (_mappings.Value.TryGetValue(mimeType, out string extension))
{
return extension;
}

if (throwErrorIfNotFound)
{
throw new ArgumentException("Requested mime type is not registered: " + mimeType);
}
else
{
return string.Empty;
}

return string.Empty;
}
}
}
}
3 changes: 0 additions & 3 deletions src/MimeTypes/MimeTypes.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net45;netcoreapp2.0;netstandard2.0;</TargetFrameworks>
<PackageId>MimeTypeMap</PackageId>
Expand All @@ -15,6 +14,4 @@
<AssemblyVersion>2.3.2.0</AssemblyVersion>
<FileVersion>2.3.2.0</FileVersion>
</PropertyGroup>


</Project>