1/* Part of SWI-Prolog 2 3 Author: Jan Wielemaker 4 E-mail: J.Wielemaker@vu.nl 5 WWW: http://www.swi-prolog.org 6 Copyright (c) 2002-2014, University of Amsterdam 7 VU University Amsterdam 8 All rights reserved. 9 10 Redistribution and use in source and binary forms, with or without 11 modification, are permitted provided that the following conditions 12 are met: 13 14 1. Redistributions of source code must retain the above copyright 15 notice, this list of conditions and the following disclaimer. 16 17 2. Redistributions in binary form must reproduce the above copyright 18 notice, this list of conditions and the following disclaimer in 19 the documentation and/or other materials provided with the 20 distribution. 21 22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 POSSIBILITY OF SUCH DAMAGE. 34*/ 35 36:- module(mimetype, 37 [ file_mime_type/2 % +Path, -Type 38 ]).
58file_mime_type(File, MimeType) :- 59 file_name_extension(_, Ext, File), 60 ( current_prolog_flag(windows, true) 61 -> downcase_atom(Ext, Lower), 62 mime_extension(Lower, MimeType) 63 ; mime_extension(Ext, M0) 64 -> MimeType = M0 65 ; downcase_atom(Ext, Lower), 66 mime_extension(Lower, MimeType) 67 ), 68 !. 69file_mime_type(File, MimeType) :- 70 file_base_name(File, Base), 71 downcase_atom(Base, Lower), 72 name_mimetype(Lower, Mime), 73 !, 74 MimeType = Mime. 75file_mime_type(_, MimeType) :- 76 default_mimetype(MimeType).
83:- multifile 84 mime:mime_extension/2. 85 86mime_extension(Ext, MimeType) :- 87 ( mime:mime_extension(Ext, Mime) 88 -> MimeType = Mime 89 ; ext_mimetype(Ext, Mime) 90 -> MimeType = Mime 91 ).
default_mimetype
. To change the default, use e.g.,
:- set_prolog_flag(default_mimetype, text/plain).
The initial default mime-type is application/unknown
. Use
the value -
to denote there is no default.
106:- create_prolog_flag(default_mimetype, application/unknown, []). 107 108default_mimetype(MimeType) :- 109 current_prolog_flag(default_mimetype, MimeType), 110 MimeType = _/_.
120 % plain text 121ext_mimetype(txt, text/plain). 122 % markup 123ext_mimetype(htm, text/html). 124ext_mimetype(html, text/html). 125ext_mimetype(xhtml, application/'xhtml+xml'). 126ext_mimetype(sgml, text/'x-sgml'). 127ext_mimetype(sgm, text/'x-sgml'). 128ext_mimetype(xml, text/xml). 129ext_mimetype(css, text/css). 130ext_mimetype(xsl, text/xml). % Unclear what this should be. 131ext_mimetype(md, text/'x-markdown'). 132 % semantic web stuff 133ext_mimetype(rdf, application/'rdf+xml'). 134ext_mimetype(rdfs, application/'rdf+xml'). 135ext_mimetype(owl, application/'rdf+xml'). 136ext_mimetype(ttl, application/turtle). 137ext_mimetype(nt, application/'n-triples'). 138ext_mimetype(nq, application/'n-quads'). 139 % Prolog source 140ext_mimetype(pl, text/plain). 141 % Other languages 142ext_mimetype(c, text/'x-c'). 143ext_mimetype(h, text/'x-c'). 144ext_mimetype(cc, text/'x-c'). 145ext_mimetype(py, text/'x-python'). 146ext_mimetype(java, text/'x-java'). 147ext_mimetype(sh, text/plain). 148 % Packaged formats 149ext_mimetype(gz, application/'x-gzip'). 150ext_mimetype(zip, application/zip). 151ext_mimetype(tgz, application/'x-gtar'). 152 % Some document formats 153ext_mimetype(pdf, application/pdf). 154ext_mimetype(doc, application/msword). 155 % Java classes 156ext_mimetype(class, application/'octet-stream'). 157ext_mimetype(jar, application/'x-java-archive'). 158 % JavaScript 159ext_mimetype(js, text/javascript). 160 % Visual Basic Script :-( 161ext_mimetype(vbs, text/vbscript). 162 % Some image formats 163ext_mimetype(jpg, image/jpeg). 164ext_mimetype(jpeg, image/jpeg). 165ext_mimetype(gif, image/gif). 166ext_mimetype(png, image/png). 167ext_mimetype(tif, image/tiff). 168ext_mimetype(tiff, image/tiff). 169ext_mimetype(xpm, image/'x-xpixmap'). 170ext_mimetype(ico, image/'x-ico'). 171ext_mimetype(svg, image/'svg+xml'). 172 % Google earth 173ext_mimetype(kml, application/'vnd.google-earth.kml+xml'). 174ext_mimetype(kmz, application/'vnd.google-earth.kmz'). 175 176 % Flash 177ext_mimetype(swf, application/'x-shockwave-flash'). 178ext_mimetype(flv, video/'x-flv'). 179 % MP3 180ext_mimetype(mp3, audio/mpeg). 181 % Downloads 182ext_mimetype(rpm, application/'x-rpm'). 183ext_mimetype(exe, application/'x-executable').
189name_mimetype(makefile, text/plain). 190name_mimetype(configure, text/plain). 191name_mimetype('configure.in', text/plain). 192name_mimetype('configure.ac', text/plain). 193name_mimetype('makefile.in', text/plain). 194name_mimetype('makefile.am', text/plain). 195name_mimetype('readme.in', text/plain)
Determine mime-type for a file
Simple library to guess the mime-type from the extension of a file. As various applications need to do this type ofinferencing it seems worthwhile to place this functionality in an extensible library.