samuelneff / MimeTypeMap

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

Suggestion: Windows Registry Fallback static method #80

Closed loopyd closed 5 years ago

loopyd commented 6 years ago

I was browsing around and found this rather interesting MIME ContentType provider via the Windows Registry:

        private static string _GetContentTypeRegistry(string fileName)
        {
            var extension = Path.GetExtension(fileName);

            if (String.IsNullOrWhiteSpace(extension))
            {
                return null;
            }

            var registryKey = Registry.ClassesRoot.OpenSubKey(extension);

            if (registryKey == null)
            {
                return null;
            }

            var value = registryKey.GetValue("Content Type") as string;

            return String.IsNullOrWhiteSpace(value) ? null : value;
        }

It has already been stated before this method is system-specific and unreliable, but I feel as though it would be a good fallback alternative should something not be found in your MIMEType database.

Splitting the MIME Mapping into your original, this, and a validator which simply contains logic that if the Winodws Registry method returns null or "application/octlet-stream" (invalid), grab it from your Mappings database, seems like a more robust solution.

samuelneff commented 6 years ago

The list was originally seeded with the list from Windows registry, among many other sources. In general the Windows registry listing tends to be very limited and random and based on what software is installed.

If you do find any listings in your local registry that are not in this list, I'd be very happy to receive a PR to add those other items.

Adding windows registry support brings in additional dependencies that many users would not be ok with. You're welcome to fork and make your own version, but I'm not going to merge this PR.

Thanks for your interest.

Sam

markusschaber commented 6 years ago

If anything, I'd consult the mappings database first, and only fall back to the Windows Registry if our database doesn't contain a matching entry. The main advantage is that it's more efficient (Checking a dictionary vs reading the Registry) and deterministic (always returns the same matches for entries in the dictionary) this way. But I agree with @samuelneff that this brings in a platform specific dependency, which won't work with .NET Core or Xamarin on non-windows platforms.