Giter Club home page Giter Club logo

mimetypemap's Introduction

MimeTypeMap

English | 简体中文

Summary

Provides a huge two-way mapping of file extensions to mime types and mime types to file extensions, e.g.:

...
{".jpe", "image/jpeg"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".js", "application/javascript"},
{".json", "application/json"},
...

Most mime types that have multiple possible extensions are pre-defined to get the most common extension when looking up extension by mime type. Since multiple extensions can map to the same mime type, it is not necessary that GetExtension(GetMimeType(ext)) returns the original extension - it will return the most common extension.

Originally posted on StackOverflow here: http://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension

NuGet

There are several packages on NuGet built from this repo. The official one is at this URL.

https://www.nuget.org/packages/MimeTypeMapOfficial

Note that the NuGet package sometimes lags behind the code in GitHub when there are contributions from others and I forget to update. If you find this to be the case and need some of the updates, create an issue here and I'll update NuGet.

Usage

After installation MimeTypeMap, include the following using statement in your class:

using MimeTypes;

Getting the mime type to an extension

Console.WriteLine("txt -> " + MimeTypeMap.GetMimeType("txt"));  // "text/plain"

Pass in a string extension and get a mime type back. Optionally include the period. If not it will be added before looking up the mime type.

If no mime type is found then the generic "application/octet-stream" is returned.

Getting the extension to a mime type

Console.WriteLine("audio/wav -> " + MimeTypeMap.GetExtension("audio/wav")); // ".wav"

Pass in a mime type and get an extension back. If the mime type is not registered, an error is thrown.

Collaboration & Contribution Principles

All code is licensed under MIT license.

Please submit pull requests for any additions while sticking to these principles for contributions:

  • MimeTypeMap.cs - the primary list of mime types
    • Maintain the existing groups of extension to mime type (primary is extension to mime-type)
    • Only add new mime-type to extension mappings if it is necessary to disambiguate the mapping based onreversing the extension to mime-type mappings.
    • Within each group of mappings, maintain alphabetical order by key.
    • Comments on a particular mapping should be on the same line after the mapping
      • Comments are optional.
      • Always keep comments in a single line regardless of length.
      • Comments to the source for less common mime-types are appreciated.
      • Non-obvious additions are validated before accepting a PR; a comment helps with this tremendously.
      • Comments must be in English. Do not worry about spelling or grammar if it is not your native language; I can correct comments and will still appreciate the comment. Translation with Google Translate is fine.
    • Do not change any code outside the mapping without asking about it first; you might waste your time otherwise.
    • All changes must maintain backwards compatibility. This includes changing what the mime-type to extension maps to unless the primary mapping is clearly wrong (far less common).
  • Project configuration changes
    • Changes to update the project to support the latest version of .NET are appreciated.
    • We must maintain backwards compatibility with all versions still commonly used in the wild.
  • CONTRIBUTORS acknowledgement and file
    • All contributions are greatly appreciated; both the individual contributions and of course the contributor.
    • Contributors are clearly visible on the GitHub project page.
    • Therefore there is no separate CONTRIBUTORS file to maintain.

Thank you,

Sam

mimetypemap's People

Contributors

samuelneff avatar william-gross avatar 0x1f9f1 avatar paal123 avatar davebrothers avatar dubrowgn avatar ice1e0 avatar seuleuzeuh avatar stefh avatar bjarmuz avatar wongkayong avatar za-creature avatar tsauvajon avatar hunteroi avatar tomoneill avatar fawdlstty avatar hugohilario avatar illfang avatar kaiseral avatar lettucebo avatar pekkanikander avatar lajjne avatar rookev avatar jorrit avatar jimismith avatar ezelargo avatar dbeuchler avatar christianackman avatar carlos0202 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.