samuelneff / MimeTypeMap

Provides a huge dictionary of file extensions to mime types.
MIT License
622 stars 201 forks source link

Define TryGetMimeType & GetMimeType #112

Closed HunteRoi closed 3 years ago

HunteRoi commented 3 years ago

Good afternoon!

I came across your repository while searching for a solution to make sure that the filename of a file matches its content-type.

I was wondering if it would be possible to implement TryGetMimeType & GetMimeType using your private _mappings variable.

You can find below the possible signature of such methods:

public static bool TryGetMimeType(string? fileName, [NotNullWhen(true)] out string? mimeType);

public static string GetMimeType(string filename);
samuelneff commented 3 years ago

Both are good ideas. When I find time I'll implement, or you're welcome to submit a PR in the interim.

GetMimeType exists already so it would be refactored to accept either an extension or a filename and check both.

TryGetMimeType is obviously new and should support the same filename/extension semantics of GetMimeType without duplicating any code (GetMimeType can use TryGetMimeType internally).