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

feat: Enable nullable #124

Merged
merged 2 commits into from
Jul 3, 2024
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
4 changes: 2 additions & 2 deletions src/MimeMapping/KnownMimeTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7149,7 +7149,7 @@ public static class FileExtensions


// Switch-case instead of dictionary since it does the hashing at compile time rather than run time
internal static string LookupType(string type)
internal static string? LookupType(string type)
{
switch (type)
{
Expand Down Expand Up @@ -10213,7 +10213,7 @@ internal static string LookupType(string type)
}

// Switch-case instead of dictionary since it does the hashing at compile time rather than run time
internal static string[] LookupMimeType(string mimeType)
internal static string[]? LookupMimeType(string mimeType)
{
switch (mimeType)
{
Expand Down
4 changes: 2 additions & 2 deletions src/MimeMapping/KnownMimeTypes.tt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ namespace MimeMapping


// Switch-case instead of dictionary since it does the hashing at compile time rather than run time
internal static string LookupType(string type)
internal static string? LookupType(string type)
{
switch (type)
{
Expand All @@ -181,7 +181,7 @@ namespace MimeMapping
}

// Switch-case instead of dictionary since it does the hashing at compile time rather than run time
internal static string[] LookupMimeType(string mimeType)
internal static string[]? LookupMimeType(string mimeType)
{
switch (mimeType)
{
Expand Down
2 changes: 2 additions & 0 deletions src/MimeMapping/MimeMapping.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<PropertyGroup>
<!-- https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/cross-platform-targeting -->
<TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Description>Constants for (almost) all MIME types and method to determine MIME type from a file name.
Contains just over 1000 mime types.

Expand Down
14 changes: 7 additions & 7 deletions src/MimeMapping/MimeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ public static class MimeUtility
/// </summary>
public const string UnknownMimeType = "application/octet-stream";

private static readonly Lazy<ReadOnlyDictionary<string, string>> _lazyDictExtensions = new Lazy<ReadOnlyDictionary<string, string>>(
() => new ReadOnlyDictionary<string, string>(KnownMimeTypes.ALL_EXTS.Value.ToDictionary(e => e, e => KnownMimeTypes.LookupType(e)))
private static readonly Lazy<ReadOnlyDictionary<string, string?>> _lazyDictExtensions = new Lazy<ReadOnlyDictionary<string, string?>>(
() => new ReadOnlyDictionary<string, string?>(KnownMimeTypes.ALL_EXTS.Value.ToDictionary(e => e, e => KnownMimeTypes.LookupType(e)))
);

private static readonly Lazy<ReadOnlyDictionary<string, string[]>> _lazyDictMimeTypes = new Lazy<ReadOnlyDictionary<string, string[]>>(
() => new ReadOnlyDictionary<string, string[]>(KnownMimeTypes.ALL_MIMETYPES.Value.Distinct().ToDictionary(e => e, e => KnownMimeTypes.LookupMimeType(e)))
private static readonly Lazy<ReadOnlyDictionary<string, string[]?>> _lazyDictMimeTypes = new Lazy<ReadOnlyDictionary<string, string[]?>>(
() => new ReadOnlyDictionary<string, string[]?>(KnownMimeTypes.ALL_MIMETYPES.Value.Distinct().ToDictionary(e => e, e => KnownMimeTypes.LookupMimeType(e)))
);

/// <summary>
/// Dictionary of all available types by extension (lazy loaded on first call)
/// </summary>
public static ReadOnlyDictionary<string, string> TypeMap => _lazyDictExtensions.Value;
public static ReadOnlyDictionary<string, string?> TypeMap => _lazyDictExtensions.Value;

/// <summary>
/// Dictionary of all available types by mimetype (lazy loaded on first call)
/// </summary>
public static ReadOnlyDictionary<string, string[]> TypeToExtensionsMap => _lazyDictMimeTypes.Value;
public static ReadOnlyDictionary<string, string[]?> TypeToExtensionsMap => _lazyDictMimeTypes.Value;

/// <param name="file">The file extensions (ex: "zip"), the file name, or file path</param>
/// <returns>The mime type string, returns "application/octet-stream" if no known type was found</returns>
Expand All @@ -53,7 +53,7 @@ public static string GetMimeMapping(string file)

/// <param name="mimeType">The mime type string, e.g. "application/json"</param>
/// <returns>One or more extensions matching the mime type or null if no match</returns>
public static string[] GetExtensions(string mimeType)
public static string[]? GetExtensions(string mimeType)
{
if (string.IsNullOrEmpty(mimeType)) throw new ArgumentNullException(mimeType);

Expand Down