Skip to content

DEGoodmanWilson/libmime

Repository files navigation

libmime

Download Build Status

A port to C++ of the node.js module mime-types https://github.com/jshttp/mime-types

It takes a filename, and gives you a mime type :D

Install

libmime installs with Conan

$ conan install libmime/[~= 0.1]@DEGoodmanWilson/stable

Project setup

If you handle multiple dependencies in your project is better to add a conanfile.txt

[requires]
libmime/[~= 0.1]@DEGoodmanWilson/stable

[generators]
txt

Complete the installation of requirements for your project running:

$ mkdir build && cd build && conan install ..

Note: It is recommended that you run conan install from a build directory and not the root of the project directory. This is because conan generates conanbuildinfo files specific to a single build configuration which by default comes from an autodetected default profile located in ~/.conan/profiles/default . If you pass different build configuration options to conan install, it will generate different conanbuildinfo files. Thus, they should not be added to the root of the project, nor committed to git.

Adding Types

All mime types are based on mime-db, so open a PR there if you"d like to add mime types.

API

#include <mime/mime.h>

All functions throw std::out_of_range if input is invalid or not found.

mime::lookup(path)

Lookup the content-type associated with a file.

mime::lookup("json");             // "application/json"
mime::lookup(".md");              // "text/markdown"
mime::lookup("file.html");        // "text/html"
mime::lookup("folder/file.js");   // "application/javascript"
mime::lookup("folder/.htaccess"); // throws std::out_of_range

mime::lookup("cats"); // throws std::out_of_range

mime::content_type(type)

Create a full content-type header given a content-type or extension. If you need to pull the extension off of a full path, you can call mime::get_extension_from_path() first.

mime::content_type("markdown");  // "text/x-markdown; charset=utf-8"
mime::content_type("file.json"); // "application/json; charset=utf-8"

// from a full path
mime::content_type(mime::get_extension_from_path("/path/to/file.json")); // "application/json; charset=utf-8"

mime::extension(type)

Get the default extension for a content-type.

mime::extension("application/octet-stream"); // "bin"

mime::charset(type)

Lookup the implied default charset of a content-type.

mime::charset("text/markdown"); // "UTF-8"

License

MIT

About

A C++ library for guessing MIME types from filenames

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published