Constructor based on MIME paths. It uses mime.detectors.cache.MimeDetectorFromCache as MIME type detector and mime.stores.files.FilesMimeStore as MIME type store.
Create MimeDatabase object with given store and detector.
Options for mimeTypeForFile
Get MIME type detector used in MimeDatabase instance.
Get mime type by name or alias.
Get MIME type for given data. Note: This does NOT provide any fallbacks like text/plain, application/octet-stream or application/x-zerosize.
Get MIME type for file and its data using methods describing in options.
Get MIME type for file using methods describing in options. File contents will be read automatically if needed.
Get MIME type for given fileName.
Get MIME types store used in MimeDatabase instance.
1 import mime.stores.files; 2 import mime.detectors.cache; 3 4 auto mimePaths = ["./test/mime", "./test/discard", "./test/nonexistent"]; 5 6 alias FilesMimeStore.Options FOptions; 7 FOptions foptions; 8 ubyte opt = FOptions.required; 9 10 foptions.types = opt; 11 foptions.aliases = opt; 12 foptions.subclasses = opt; 13 foptions.icons = opt; 14 foptions.genericIcons = opt; 15 foptions.XMLnamespaces = opt; 16 foptions.globs2 = opt; 17 foptions.globs = opt; 18 foptions.magic = opt; 19 foptions.treemagic = opt; 20 21 auto store = new FilesMimeStore(mimePaths, foptions); 22 assert(!store.byMimeType().empty); 23 auto detector = new MimeDetectorFromCache(mimePaths); 24 25 assert(detector.mimeCaches().length == 2); 26 assert(detector.mimeTypeForFileName("sprite.spr").length); 27 assert(detector.mimeTypeForFileName("model01.mdl").length); 28 assert(detector.mimeTypeForFileName("liblist.gam").length); 29 assert(detector.mimeTypeForFileName("no.exist").empty); 30 assert(detector.mimeTypeForData("IDSP\x02\x00\x00\x00") == "image/x-hlsprite"); 31 assert(detector.resolveAlias("application/nonexistent") is null); 32 33 assert(detector.mimeTypeForNamespaceURI("http://www.w3.org/1999/ent") == "text/x-ent"); 34 assert(detector.mimeTypeForNamespaceURI("nonexistent").empty); 35 36 auto database = new MimeDatabase(store, detector); 37 assert(database.detector() is detector); 38 assert(database.store() is store); 39 40 assert(database.mimeType(string.init) is null); 41 42 auto imageSprite = database.mimeType("image/x-hlsprite"); 43 auto appSprite = database.mimeType("application/x-hlsprite"); 44 assert(database.mimeType("application/x-hlsprite", No.resolveAlias) is null); 45 assert(imageSprite !is null && imageSprite is appSprite); 46 47 assert(database.detector().isSubclassOf("text/x-fgd", "text/plain")); 48 assert(!database.detector().isSubclassOf("text/x-fgd", "application/octet-stream")); 49 50 auto fgdType = database.mimeTypeForFileName("name.fgd"); 51 assert(fgdType !is null); 52 assert(fgdType.name == "text/x-fgd"); 53 54 //testing Match options 55 auto iqm = database.mimeTypeForFile("model.iqm", MimeDatabase.Match.globPatterns); 56 assert(iqm !is null); 57 assert(iqm.name == "application/x-iqm"); 58 59 auto spriteType = database.mimeTypeForFile("sprite.spr", MimeDatabase.Match.globPatterns); 60 assert(spriteType !is null); 61 62 auto sprite32 = database.mimeTypeForFile("sprite.spr", "IDSP\x20\x00\x00\x00", MimeDatabase.Match.magicRules); 63 assert(sprite32 !is null); 64 assert(sprite32.name == "image/x-sprite32"); 65 66 auto zeroType = database.mimeTypeForFile("nonexistent", (void[]).init, MimeDatabase.Match.emptyFileFallback); 67 assert(zeroType !is null); 68 assert(zeroType.name == "application/x-zerosize"); 69 70 zeroType = database.mimeTypeForFile("test/emptyfile", MimeDatabase.Match.emptyFileFallback); 71 assert(zeroType !is null); 72 assert(zeroType.name == "application/x-zerosize"); 73 74 auto textType = database.mimeTypeForFile("test/mime/types", MimeDatabase.Match.textFallback); 75 assert(textType !is null); 76 assert(textType.name == "text/plain"); 77 78 auto dirType = database.mimeTypeForFile("test", MimeDatabase.Match.inodeType); 79 assert(dirType !is null); 80 assert(dirType.name == "inode/directory"); 81 82 auto octetStreamType = database.mimeTypeForFile("test/mime/mime.cache", MimeDatabase.Match.octetStreamFallback); 83 assert(octetStreamType !is null); 84 assert(octetStreamType.name == "application/octet-stream"); 85 86 assert(database.mimeTypeForFile("file.unknown", MimeDatabase.Match.globPatterns) is null); 87 88 //testing data 89 auto hlsprite = database.mimeTypeForData("IDSP\x02\x00\x00\x00"); 90 assert(hlsprite !is null); 91 assert(hlsprite.name == "image/x-hlsprite"); 92 93 auto qsprite = database.mimeTypeForData("IDSP\x01\x00\x00\x00"); 94 assert(qsprite !is null); 95 assert(qsprite.name == "image/x-qsprite"); 96 97 auto q2sprite = database.mimeTypeForData("IDS2"); 98 assert(q2sprite !is null); 99 assert(q2sprite.name == "image/x-q2sprite"); 100 101 //testing case-insensitive suffix 102 auto vpk = database.mimeTypeForFileName("pakdir.vpk"); 103 assert(vpk !is null); 104 assert(vpk.name == "application/vnd.valve.vpk"); 105 106 vpk = database.mimeTypeForFileName("pakdir.VPK"); 107 assert(vpk !is null); 108 assert(vpk.name == "application/vnd.valve.vpk"); 109 110 //testing generic glob 111 auto modelseq = database.mimeTypeForFileName("model01.mdl"); 112 assert(modelseq !is null); 113 assert(modelseq.name == "application/x-hlmdl-sequence"); 114 modelseq = database.mimeTypeForFileName("model01.MDL"); 115 assert(modelseq !is null && modelseq.name == "application/x-hlmdl-sequence"); 116 117 auto generalGlob = database.mimeTypeForFileName("general_test_long_glob"); 118 assert(generalGlob !is null); 119 assert(generalGlob.name == "application/x-general-long-glob"); 120 assert(database.detector.mimeTypeForFileName("general_test_long_glob")); 121 122 assert(!database.mimeTypeForFileName("pak1.PAK")); 123 assert(database.mimeTypeForFileName("pak1.pak")); 124 125 //testing case-sensitive suffix 126 assert(database.mimeTypeForFileName("my.shader")); 127 assert(!database.mimeTypeForFileName("my.SHADER")); 128 129 //testing literal 130 assert(database.mimeTypeForFileName("liblist.gam")); 131 assert(database.mimeTypeForFileName("makefile")); 132 133 //testing discard glob 134 assert(!database.mimeTypeForFileName("GNUmakefile")); 135 assert(!database.detector.mimeTypeForFileName("GNUmakefile")); 136 137 assert(!database.mimeTypeForFileName("file.qvm3")); 138 assert(!database.detector.mimeTypeForFileName("file.qvm3")); 139 140 assert(!database.mimeTypeForFileName("model01.sequence")); 141 assert(!database.detector.mimeTypeForFileName("model01.sequence")); 142 143 //testing discard magic 144 assert(!database.mimeTypeForData("PAK")); 145 assert(!database.detector.mimeTypeForData("PAK")); 146 assert(!database.mimeTypeForFileName("file.qwad")); 147 148 //conflicts 149 assert(database.mimeTypeForFileName("file.jmf")); 150 assert(database.mimeTypeForData("PACK")); 151 152 //xml 153 assert(database.mimeTypeForFileName("file.xml").name == "application/xml"); 154 assert(database.mimeTypeForData("<?xml").name == "application/xml"); 155 assert(database.mimeTypeForFile("file.xml", `<start-element xmlns="http://www.w3.org/1999/ent">`).name == "text/x-ent");
High-level class for accessing Shared MIME-info database.