### MimeTypesMap usage examples Source: https://github.com/hey-red/mimetypesmap/blob/master/README.md Shows how to use MimeTypesMap to get an extension from a MIME type, get a MIME type from a filename, and add or update a mapping. ```csharp using HeyRed.Mime; MimeTypesMap.GetExtension("image/jpeg"); // => jpeg MimeTypesMap.GetMimeType("filename.jpeg"); // => image/jpeg MimeTypesMap.AddOrUpdate(string mime, string extension); ``` -------------------------------- ### GetExtension basic usage example Source: https://github.com/hey-red/mimetypesmap Demonstrates basic calls: exact match returns "jpeg"; partial/substring matching returns first match in enumeration order (e.g., "mpeg" returns "m3u8" for application/vnd.apple.mpegurl); unknown values fall back to "bin". Requires using HeyRed.Mime. ```csharp using HeyRed.Mime; string ext = MimeTypesMap.GetExtension("image/jpeg"); Console.WriteLine(ext); // "jpeg" // Partial / substring matching (first match in enumeration order wins; // in practice the first matching entry in the generated insertion order): Console.WriteLine(MimeTypesMap.GetExtension("jpeg")); // "jpeg" Console.WriteLine(MimeTypesMap.GetExtension("mpeg")); // "m3u8" (application/vnd.apple.mpegurl) Console.WriteLine(MimeTypesMap.GetExtension("video/mpeg")); // "mpeg" // Fallback for unknown values: Console.WriteLine(MimeTypesMap.GetExtension("application/x-unknown-thing")); // "bin" ``` -------------------------------- ### Install MimeTypesMap via NuGet Source: https://github.com/hey-red/mimetypesmap/blob/master/README.md Install the MimeTypesMap package from NuGet using the Package Manager Console. ```powershell PM> Install-Package MimeTypesMap ``` -------------------------------- ### Basic AddOrUpdate usage example Source: https://github.com/hey-red/mimetypesmap Registers a custom MIME type at startup, verifies both GetMimeType and GetExtension lookups, and overwrites an existing mapping. Demonstrates that the extension key is stored without a dot and that lookups work after mutation. ```csharp using HeyRed.Mime; // Register a custom company MIME type once at startup. MimeTypesMap.AddOrUpdate("application/x-acme-doc", "acme"); // Now both lookups know about it: Console.WriteLine(MimeTypesMap.GetMimeType("report.acme")); // "application/x-acme-doc" Console.WriteLine(MimeTypesMap.GetExtension("application/x-acme-doc")); // "acme" // Overwrite an existing mapping (e.g. correct a default): MimeTypesMap.AddOrUpdate("application/json", "json2"); Console.WriteLine(MimeTypesMap.GetMimeType("data.json2")); // "application/json" ``` -------------------------------- ### GetExtension defensive handling example Source: https://github.com/hey-red/mimetypesmap Shows defensive usage: GetExtension is case-sensitive unlike GetMimeType, so normalize with ToLowerInvariant if needed. Passing null throws ArgumentNullException; the example catches it and prints the parameter name "mime". Requires using System and HeyRed.Mime. ```csharp using System; using HeyRed.Mime; // GetExtension is case-sensitive unlike GetMimeType; normalize if needed. static string ExtFor(string mime) { if (mime is null) { return "bin"; // or throw; GetExtension(null) throws ArgumentNullException } var ext = MimeTypesMap.GetExtension(mime.ToLowerInvariant()); return ext; // still "bin" when unmatched — treat "bin" as "unknown" } try { var ext = MimeTypesMap.GetExtension(null); } catch (ArgumentNullException ex) { Console.WriteLine($"Null MIME rejected: {ex.ParamName}"); // "mime" } ``` -------------------------------- ### GetExtension internal behavior (source-derived) Source: https://github.com/hey-red/mimetypesmap Shows the internal logic: uses FirstOrDefault over dictionary enumeration order (not guaranteed by BCL, in practice insertion order from T4-generated initializer). The first matching key wins; substring matching is case-sensitive; empty string matches every value; unknown values fall back to "bin". ```csharp var ext = _mimeTypeMap.Value.FirstOrDefault(x => x.Value.Contains(mime)).Key; if (ext != null) { return ext; } return _defaultExtension; ``` -------------------------------- ### Handling unknown extensions and null input Source: https://github.com/hey-red/mimetypesmap Shows that unknown extensions return the default MIME type instead of throwing, and that passing null throws ArgumentNullException. Includes a helper method to describe file types. ```csharp using System; using HeyRed.Mime; // The library never throws for unknown extensions; it reports a canonical default. const string Unknown = "application/octet-stream"; static string DescribeFile(string path) { var mime = MimeTypesMap.GetMimeType(path); return mime == Unknown ? $"{path}: unknown type (fallback used)" : $"{path}: {mime}"; } Console.WriteLine(DescribeFile("archive.gz")); // "archive.gz: application/gzip" (mapped) Console.WriteLine(DescribeFile("blob.zzz")); // "blob.zzz: unknown type (fallback used)" try { MimeTypesMap.GetMimeType(null); } catch (ArgumentNullException ex) { Console.WriteLine($"Null filename rejected: {ex.ParamName}"); // "fileName" } ``` -------------------------------- ### Basic usage of MimeTypesMap.GetMimeType Source: https://github.com/hey-red/mimetypesmap Demonstrates calling GetMimeType with various inputs: full file names, bare extensions, paths, and unknown extensions. Outputs are shown in comments. ```csharp using HeyRed.Mime; Console.WriteLine(MimeTypesMap.GetMimeType("photo.jpeg")); // "image/jpeg" Console.WriteLine(MimeTypesMap.GetMimeType("JPG")); // "image/jpeg" (case-insensitive) Console.WriteLine(MimeTypesMap.GetMimeType("jpeg")); // "image/jpeg" (bare extension) Console.WriteLine(MimeTypesMap.GetMimeType("C:\\docs\\a.TXT")); // "text/plain" (paths work) Console.WriteLine(MimeTypesMap.GetMimeType("file.unknown")); // "application/octet-stream" ``` -------------------------------- ### AddOrUpdate validation before mutation Source: https://github.com/hey-red/mimetypesmap Shows a helper method that validates the extension is non-null before calling AddOrUpdate, catching ArgumentNullException. Note that a null mime value is accepted but corrupts lookups, so validation only guards the extension. ```csharp using System; using HeyRed.Mime; static bool Register(string mime, string ext) { if (ext is null) { Console.WriteLine("Extension must not be null."); return false; } try { MimeTypesMap.AddOrUpdate(mime, ext); // mime == null is accepted but corrupts lookups return true; } catch (ArgumentNullException ex) { Console.WriteLine($"Rejected: {ex.ParamName}"); return false; } } Register("application/x-demo", "demo"); // true Register("application/x-demo", null); // false — ArgumentNullException("extension") ``` -------------------------------- ### GetExtension method signature Source: https://github.com/hey-red/mimetypesmap Public static method that resolves a MIME type (or fragment) to a file extension. The mime parameter is not lowercased or trimmed; matching uses case-sensitive string.Contains. Returns the extension (dictionary key, no leading dot) of the first entry whose MIME value contains the input, or the private default "bin" if no match. ```csharp public static string GetExtension(string mime) ``` -------------------------------- ### AddOrUpdate internal indexer assignment Source: https://github.com/hey-red/mimetypesmap Shows the internal implementation line that assigns the MIME value to the extension key using the dictionary indexer. Existing entries are replaced, and the change is global, permanent, and not thread-safe. ```csharp _mimeTypeMap.Value[extension] = mime; ``` -------------------------------- ### MimeTypesMap.GetExtension(string mime) Source: https://github.com/hey-red/mimetypesmap Resolves a MIME type (or fragment) to a file extension using case-sensitive substring matching. Returns the first matching extension or a default of 'bin' if no match is found. ```APIDOC ## MimeTypesMap.GetExtension(string mime) ### Description Resolves a MIME type (or fragment) to a file extension. Matching is case-sensitive and substring-based; the first matching entry in enumeration order wins. Returns the default extension `"bin"` if no match is found. ### Method GetExtension (static method) ### Endpoint `MimeTypesMap.GetExtension(string mime)` ### Parameters #### Method Parameters - **mime** (string) - Required - MIME type (or fragment) to resolve. Not lowercased or trimmed before matching; matched with case-sensitive `string.Contains`. ### Return Type `string` — the extension (dictionary key, no leading dot) of the first entry whose MIME value contains `mime` as a substring. If no match, returns the private default `"bin"`. ### Throws - **ArgumentNullException** (BCL) - If `mime` is `null`. - **NullReferenceException** (BCL) - If any stored MIME value is `null` (only possible after `AddOrUpdate(null, extension)`) and that entry is visited before a match. ### Behavior Notes - Uses `FirstOrDefault` over the dictionary's enumeration order (not guaranteed by the BCL; in practice insertion order from the T4-generated initializer). The first matching key wins. - Matching is **substring-based**, so partial values work: `"jpeg"` matches `"image/jpeg"`, and `"mpeg"` matches every value containing that substring (e.g. `"application/vnd.apple.mpegurl"`, `"audio/mpeg"`, `"video/mpeg"`), with the first in enumeration order winning. - Matching is **case-sensitive** (`string.Contains` default ordinal semantics). `GetExtension("IMAGE/JPEG")` does **not** match `"image/jpeg"`. - An **empty string** (`""`) is contained by every value, so it returns the first key in enumeration order (implementation-defined; in practice the first inserted entry, `"ez"`). - Unknown values with no substring hit (e.g. `"application/x-does-not-exist"`) fall back to `"bin"` — the library does not throw for unknown MIME types. - The dictionary comparer (`OrdinalIgnoreCase`) affects *keys* only; MIME *values* are compared by `Contains`, which is case-sensitive. ### Request Example ```csharp using HeyRed.Mime; string ext = MimeTypesMap.GetExtension("image/jpeg"); Console.WriteLine(ext); // "jpeg" // Partial / substring matching (first match in enumeration order wins; // in practice the first matching entry in the generated insertion order): Console.WriteLine(MimeTypesMap.GetExtension("jpeg")); // "jpeg" Console.WriteLine(MimeTypesMap.GetExtension("mpeg")); // "m3u8" (application/vnd.apple.mpegurl) Console.WriteLine(MimeTypesMap.GetExtension("video/mpeg")); // "mpeg" // Fallback for unknown values: Console.WriteLine(MimeTypesMap.GetExtension("application/x-unknown-thing")); // "bin" ``` ### Request Example — defensive handling ```csharp using System; using HeyRed.Mime; // GetExtension is case-sensitive unlike GetMimeType; normalize if needed. static string ExtFor(string mime) { if (mime is null) { return "bin"; // or throw; GetExtension(null) throws ArgumentNullException } var ext = MimeTypesMap.GetExtension(mime.ToLowerInvariant()); return ext; // still "bin" when unmatched — treat "bin" as "unknown" } try { var ext = MimeTypesMap.GetExtension(null); } catch (ArgumentNullException ex) { Console.WriteLine($"Null MIME rejected: {ex.ParamName}"); // "mime" } ``` ``` -------------------------------- ### AddOrUpdate method signature Source: https://github.com/hey-red/mimetypesmap Public static method signature for MimeTypesMap.AddOrUpdate. Accepts a MIME type and a bare extension (no leading dot) as the dictionary key. Returns void and mutates the process-wide _mimeTypeMap dictionary in place. ```csharp public static void AddOrUpdate(string mime, string extension) ``` -------------------------------- ### GetMimeType internal behavior (extension extraction and lookup) Source: https://github.com/hey-red/mimetypesmap Shows how the method extracts the extension after the last dot, lowercases it, and looks it up in the MIME map. Falls back to the default MIME type for unknown extensions. ```csharp string ext = fileName; var ind = ext.LastIndexOf('.'); if (ind != -1 && ext.Length > ind + 1) { ext = fileName.Substring(ind + 1).ToLower(); } if (_mimeTypeMap.Value.TryGetValue(ext, out string result)) { return result; } return _defaultMimeType; ``` -------------------------------- ### GetMimeType method signature Source: https://github.com/hey-red/mimetypesmap Public static method that returns the MIME type for a given file name or extension. Throws ArgumentNullException if fileName is null. ```csharp public static string GetMimeType(string fileName) ``` -------------------------------- ### MimeTypesMap.GetExtension Source: https://github.com/hey-red/mimetypesmap Looks up a file extension by MIME type using a substring match. Returns the extension string or null if no match is found. ```APIDOC ## GetExtension ### Description Looks up a file extension by MIME type (substring match). ### Method GetExtension ### Signature `public static string GetExtension(string mime)` ### Parameters - **mime** (string) - Required - The MIME type to look up. ### Returns - **string** - The file extension corresponding to the MIME type, or null if not found. ``` -------------------------------- ### MimeTypesMap.GetMimeType Source: https://github.com/hey-red/mimetypesmap Looks up a MIME type by filename or extension using an exact, case-insensitive match. Returns the MIME type string or null if no match is found. ```APIDOC ## GetMimeType ### Description Looks up a MIME type by filename/extension (exact, case-insensitive). ### Method GetMimeType ### Signature `public static string GetMimeType(string fileName)` ### Parameters - **fileName** (string) - Required - The filename or extension to look up. ### Returns - **string** - The MIME type corresponding to the filename/extension, or null if not found. ``` -------------------------------- ### MimeTypesMap.AddOrUpdate Source: https://github.com/hey-red/mimetypesmap Inserts or replaces a mapping in the process-wide dictionary. This method allows adding new MIME type to extension mappings or updating existing ones. ```APIDOC ## AddOrUpdate ### Description Inserts or replaces a mapping in the process-wide dictionary. ### Method AddOrUpdate ### Signature `public static void AddOrUpdate(string mime, string extension)` ### Parameters - **mime** (string) - Required - The MIME type. - **extension** (string) - Required - The file extension to associate with the MIME type. ### Returns - **void** - This method does not return a value. ``` -------------------------------- ### MimeTypesMap class declaration (auto-generated) Source: https://github.com/hey-red/mimetypesmap Shows the static class declaration with a warning that it is auto-generated by T4 and should not be modified manually. The class is in the HeyRed.Mime namespace and cannot be instantiated. ```csharp namespace HeyRed.Mime { // WARNING: This class auto generated by T4. DON'T modify manually. public static class MimeTypesMap { ... } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.