Skip to content

mediatypes #

Media Types

mediatypes module allows you to manipulate media types, also known as MIME types.

Applications can manage types through the MediaTypeDatabase abstraction. Types can be pre-defined in the application, and additional arbitrary (including custom) types can be loaded at runtime from memory or from files that adhere to the /etc/mime.types format.

See also:

Usage

For example load the system-wide media types mapping file /etc/mime.types and try to detect the files media type by extension:

import os
import mediatypes

fn main() {
    mut mime_types_file := os.open('/etc/mime.types')!
    defer {
        mime_types_file.close()
    }

    mime_types := mediatypes.load(mut mime_types_file)

    test_data := {
        'style.css':     'text/css'
        'image.png':     'image/png'
        'manifest.json': 'application/json'
    }

    for file_name, expected_type in test_data {
        mime_type := mime_types.lookup(os.file_ext(file_name))
        dump(mime_type)
        assert mime_type.name() == expected_type
    }
}

You can add any custom media types and associate media types with file extensions as you need by manipulating the MediaTypeDatabase object:

mut mime_types := mediatypes.new() // or use mediatypes.load()
mime_types.add(mediatypes.MediaType{
    type_name:  'application'
    subtype:    'my-custom-type'
    extensions: ['custom']
})

Embedding media types database into application

To add built-in support for media types to an application, you need to define constants with type data in the application's source code. For large lists of types, this is too tedious, so a special code generator script is included with the mediatypes lib.

Use embed_media_types.vsh to get the .v file content with embedded media types database.

The following command will print the .v file content to the standard output:

v run embed_media_types.vsh

By default script will download the mime.types file maintained by Apache HTTP Server project.

Run v run embed_media_types.vsh -help to see available script options.

embed_media_types.vsh script skips type definitions for which a list of file extensions is not specified.

The script output is not formatted be default. Use v fmt to format the generated file.

Constants #

const default_media_type = MediaType{
	@type:   'application'
	subtype: 'octet-stream'
}

fn construct #

fn construct(types map[string]MediaType, exts map[string]string) &MediaTypeDatabase

construct creates the new media type database initialized with existing types and exts maps.

The type/subtype media type name is expected in types map key. exts map should contain key-value pairs where key is file extension without leading dot and value is a type/subtype media type name.

fn load #

fn load(mut r io.Reader) &MediaTypeDatabase

load creates the new media types database from reader.

fn new #

fn new() &MediaTypeDatabase

new creates empty media type database.

fn parse #

fn parse(mut r io.Reader) map[string]MediaType

parse parses the multiline MIME-types file content e.g. /etc/mime.types. The resulting map keys are type/subtype media type names.

Example

import os
import mediatypes
mut file := os.open('/etc/mime.types')!
defer { file.close() }
mime_types := mediatypes.parse(mut file)
println(mime_types)

fn parse_content_type #

fn parse_content_type(header string) !MediaType

parse_content_type parses the Content-Type header and returns the media type.

fn parse_line #

fn parse_line(line string) !MediaType

parse_line parses the MIME-type definition string such as application/json json.

fn parse_string #

fn parse_string(data string) map[string]MediaType

parse_string parses the multiline MIME-types file content e.g. /etc/mime.types. The resulting map keys are type/subtype media type names.

fn (MediaTypeDatabase) add #

fn (mut m MediaTypeDatabase) add(typ MediaType, params MediaTypeAddParams)

add adds the new media type to the database.

fn (MediaTypeDatabase) add_map #

fn (mut m MediaTypeDatabase) add_map(types map[string]MediaType, params MediaTypeAddParams)

add_map adds a map of media types to the database.

fn (MediaTypeDatabase) get #

fn (mut m MediaTypeDatabase) get(name string) ?MediaType

get returns the media type from database by name or none.

fn (MediaTypeDatabase) has #

fn (mut m MediaTypeDatabase) has(name string) bool

has reports is the media type present in the database.

fn (MediaTypeDatabase) delete #

fn (mut m MediaTypeDatabase) delete(name string)

delete deletes the media type from database by name.

fn (MediaTypeDatabase) lookup #

fn (mut m MediaTypeDatabase) lookup(ext string) MediaType

lookup returns the media type associated with the ext (starting with dot or not). If no media type found the default application/octet-stream type will be returned.

fn (MediaTypeDatabase) lookup_opt #

fn (mut m MediaTypeDatabase) lookup_opt(ext string) ?MediaType

lookup_opt returns the media type associated with the ext (starting with dot or not) or none if no associated media type found.

enum MediaTypeStringOpt #

@[flag]
enum MediaTypeStringOpt {
	default
	lowercase // force lower case.
	compact   // eliminate the spaces from string.
}

struct MediaType #

struct MediaType {
pub:
	@type      string
	subtype    string
	extensions []string
	parameters map[string]string
}

fn (MediaType) name #

fn (t MediaType) name() string

name returns the name of media type as type/subtype string.

fn (MediaType) string #

fn (t MediaType) string(params MediaTypeFormatParams) string

string returns the string with media type name with parameters suitable for use as HTTP or email Content-Type header value. Example: text/plain; charset=UTF-8. See also parse_content_type().

struct MediaTypeAddParams #

@[params]
struct MediaTypeAddParams {
pub:
	override bool
}

struct MediaTypeFormatParams #

@[params]
struct MediaTypeFormatParams {
pub:
	flags MediaTypeStringOpt
}