File extensions and MIME Types in .NET

Viewed 76006

I want to get a MIME Content-Type from a given extension (preferably without accessing the physical file). I have seen some questions about this and the methods described to perform this can be resumed in:

  1. Use registry information.
  2. Use urlmon.dll's FindMimeFromData.
  3. Use IIS information.
  4. Roll your own MIME mapping function. Based on this table, for example.

I've been using no.1 for some time but I realized that the information provided by the registry is not consistent and depends on the software installed on the machine. Some extensions, like .zip don't use to have a Content-Type specified.

Solution no.2 forces me to have the file on disk in order to read the first bytes, which is something slow but may get good results.

The third method is based on Directory Services and all that stuff, which is something I don't like much because I have to add COM references and I'm not sure it's consistent between IIS6 and IIS7. Also, I don't know the performance of this method.

Finally, I didn't want to use my own table but at the end seems the best option if I want a decent performance and consistency of the results between platforms (even Mono).

Do you think there's a better option than using my own table or one of other described methods are better? What's your experience?

7 Answers

Nisus - would you be willing to post the entire source code for your utility somewhere? that would really be helpful. thanks!

Never mind....

I edited the apache definition file to only contain entries with defined extensions, then extended the code to load in the types/extensions from the text file at run time. Not elegant perhaps but sure beats creating/maintaining 630 lines of source code for the mime types.

[in the constructor for MimeTypeCollection instead of this stuff: this.Add(new mimeTypeInfo("application/applixware",new List(new[] { ".aw" })));]

        // import mime/extension definition list to facilitate maintenance
    string dir = AppDomain.CurrentDomain.BaseDirectory;
    using (TextReader streamReader = new StreamReader(Path.Combine(dir, "MimeDefinitions.txt")))
    {
      string input;
      while ((input = streamReader.ReadLine()) != null)
      {
        if (input.Substring(0, 1) != "#")
        {
          // text line format ::= [contenttype]<tab>[space delimited list of extensions, without dot]
          string contentType = input.Group("0\t1");
          string extensionList = input.Group("1\t1");
          string[] extensions = extensionList.Split(" ".ToCharArray());
          List<string> extensionSet = new List<string>();
          foreach (string term in extensions)
          {
            extensionSet.Add("."+term);
          }
          this.Add(new mimeTypeInfo(contentType, extensionSet));
        }
      }
    }

I also found that the Init() method would be called and the _extensions and _mime members would not be completely initialized so I changed it to read:

if (_extensions == null || _mimes == null || _mimes.Count != this.Count)

Anyway, I now how a class that can handle the external defs and local registry I needed.

Thanks!

I would just like to caution you against retrieving the Mime / content type of a file without validating it against the actual content of the file, as it could pose a serious security risk to your system and users.

Although there are valid use cases for something like this, attackers can potentially attack your system or use it as a mechanism to propagate malicious files to other users, by disguising executables or scripts as other, non-suspect file types.

It is advisable that you seriously consider sandboxing and performing deep content inspection on uploaded files before continuing to process it (or making it available for download).

Related