Skip to content

Commit

Permalink
feat: Enable nullable (#124)
Browse files Browse the repository at this point in the history
* feat: Added nullable enable.

* fix: Revert.
  • Loading branch information
HavenDV authored Jul 3, 2024
1 parent 3741d38 commit d99f765
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
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

0 comments on commit d99f765

Please sign in to comment.