Class MimeDatabase

High-level class for accessing Shared MIME-info database.

class MimeDatabase ;

Constructors

NameDescription
this Constructor based on MIME paths. It uses MimeDetectorFromCache as MIME type detector and FilesMimeStore as MIME type store.
this Create MimeDatabase object with given store and detector.

Methods

NameDescription
detector Get MIME type detector used in MimeDatabase instance.
mimeType Get mime type by name or alias.
mimeTypeForData Get MIME type for given data.
mimeTypeForFile Get MIME type for file and its data using methods describing in options.
mimeTypeForFile Get MIME type for file using methods describing in options. File contents will be read automatically if needed.
mimeTypeForFileName Get MIME type for given fileName.
store Get MIME types store used in MimeDatabase instance.

Enums

NameDescription
Match Options for mimeTypeForFile

Example

import mime.stores.files;
import mime.detectors.cache;

auto mimePaths = ["./test/mime", "./test/discard", "./test/nonexistent"];

alias FilesMimeStore.Options FOptions;
FOptions foptions;
ubyte opt = FOptions.required;

foptions.types = opt;
foptions.aliases = opt;
foptions.subclasses = opt;
foptions.icons = opt;
foptions.genericIcons = opt;
foptions.XMLnamespaces = opt;
foptions.globs2 = opt;
foptions.globs = opt;
foptions.magic = opt;
foptions.treemagic = opt;

auto store = new FilesMimeStore(mimePaths, foptions);
assert(!store.byMimeType().empty);
auto detector = new MimeDetectorFromCache(mimePaths);

assert(detector.mimeCaches().length == 2);
assert(detector.mimeTypeForFileName("sprite.spr").length);
assert(detector.mimeTypeForFileName("model01.mdl").length);
assert(detector.mimeTypeForFileName("liblist.gam").length);
assert(detector.mimeTypeForFileName("no.exist").empty);
assert(detector.mimeTypeForData("IDSP\x02\x00\x00\x00") == "image/x-hlsprite");
assert(detector.resolveAlias("application/nonexistent") is null);

assert(detector.mimeTypeForNamespaceURI("http://www.w3.org/1999/ent") == "text/x-ent");
assert(detector.mimeTypeForNamespaceURI("nonexistent").empty);

auto database = new MimeDatabase(store, detector);
assert(database.detector() is detector);
assert(database.store() is store);

assert(database.mimeType(string.init) is null);

auto imageSprite = database.mimeType("image/x-hlsprite");
auto appSprite = database.mimeType("application/x-hlsprite");
assert(database.mimeType("application/x-hlsprite", No.resolveAlias) is null);
assert(imageSprite !is null && imageSprite is appSprite);

assert(database.detector().isSubclassOf("text/x-fgd", "text/plain"));
assert(!database.detector().isSubclassOf("text/x-fgd", "application/octet-stream"));

auto fgdType = database.mimeTypeForFileName("name.fgd");
assert(fgdType !is null);
assert(fgdType.name == "text/x-fgd");

//testing Match options
auto iqm = database.mimeTypeForFile("model.iqm", MimeDatabase.Match.globPatterns);
assert(iqm !is null);
assert(iqm.name == "application/x-iqm");

auto spriteType = database.mimeTypeForFile("sprite.spr", MimeDatabase.Match.globPatterns);
assert(spriteType !is null);

auto sprite32 = database.mimeTypeForFile("sprite.spr", "IDSP\x20\x00\x00\x00", MimeDatabase.Match.magicRules);
assert(sprite32 !is null);
assert(sprite32.name == "image/x-sprite32");

auto zeroType = database.mimeTypeForFile("nonexistent", (void[]).init, MimeDatabase.Match.emptyFileFallback);
assert(zeroType !is null);
assert(zeroType.name == "application/x-zerosize");

zeroType = database.mimeTypeForFile("test/emptyfile", MimeDatabase.Match.emptyFileFallback);
assert(zeroType !is null);
assert(zeroType.name == "application/x-zerosize");

auto textType = database.mimeTypeForFile("test/mime/types", MimeDatabase.Match.textFallback);
assert(textType !is null);
assert(textType.name == "text/plain");

auto dirType = database.mimeTypeForFile("test", MimeDatabase.Match.inodeType);
assert(dirType !is null);
assert(dirType.name == "inode/directory");

auto octetStreamType = database.mimeTypeForFile("test/mime/mime.cache", MimeDatabase.Match.octetStreamFallback);
assert(octetStreamType !is null);
assert(octetStreamType.name == "application/octet-stream");

assert(database.mimeTypeForFile("file.unknown", MimeDatabase.Match.globPatterns) is null);

//testing data
auto hlsprite = database.mimeTypeForData("IDSP\x02\x00\x00\x00");
assert(hlsprite !is null);
assert(hlsprite.name == "image/x-hlsprite");

auto qsprite = database.mimeTypeForData("IDSP\x01\x00\x00\x00");
assert(qsprite !is null);
assert(qsprite.name == "image/x-qsprite");

auto q2sprite = database.mimeTypeForData("IDS2");
assert(q2sprite !is null);
assert(q2sprite.name == "image/x-q2sprite");

//testing case-insensitive suffix
auto vpk = database.mimeTypeForFileName("pakdir.vpk");
assert(vpk !is null);
assert(vpk.name == "application/vnd.valve.vpk");

vpk = database.mimeTypeForFileName("pakdir.VPK");
assert(vpk !is null);
assert(vpk.name == "application/vnd.valve.vpk");

//testing generic glob
auto modelseq = database.mimeTypeForFileName("model01.mdl");
assert(modelseq !is null);
assert(modelseq.name == "application/x-hlmdl-sequence");
modelseq = database.mimeTypeForFileName("model01.MDL");
assert(modelseq !is null && modelseq.name == "application/x-hlmdl-sequence");

auto generalGlob = database.mimeTypeForFileName("general_test_long_glob");
assert(generalGlob !is null);
assert(generalGlob.name == "application/x-general-long-glob");
assert(database.detector.mimeTypeForFileName("general_test_long_glob"));

assert(!database.mimeTypeForFileName("pak1.PAK"));
assert(database.mimeTypeForFileName("pak1.pak"));

//testing case-sensitive suffix
assert(database.mimeTypeForFileName("my.shader"));
assert(!database.mimeTypeForFileName("my.SHADER"));

//testing literal
assert(database.mimeTypeForFileName("liblist.gam"));
assert(database.mimeTypeForFileName("makefile"));

//testing discard glob
assert(!database.mimeTypeForFileName("GNUmakefile"));
assert(!database.detector.mimeTypeForFileName("GNUmakefile"));

assert(!database.mimeTypeForFileName("file.qvm3"));
assert(!database.detector.mimeTypeForFileName("file.qvm3"));

assert(!database.mimeTypeForFileName("model01.sequence"));
assert(!database.detector.mimeTypeForFileName("model01.sequence"));

//testing discard magic
assert(!database.mimeTypeForData("PAK"));
assert(!database.detector.mimeTypeForData("PAK"));
assert(!database.mimeTypeForFileName("file.qwad"));

//conflicts
assert(database.mimeTypeForFileName("file.jmf"));
assert(database.mimeTypeForData("PACK"));

//xml
assert(database.mimeTypeForFileName("file.xml").name == "application/xml");
assert(database.mimeTypeForData("<?xml").name == "application/xml");
assert(database.mimeTypeForFile("file.xml", `<start-element xmlns="http://www.w3.org/1999/ent">`).name == "text/x-ent");