View source with raw comments or as raw
   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          ]).
  39
  40/** <module> Determine mime-type for a file
  41
  42Simple library to guess the mime-type from   the extension of a file. As
  43various applications need  to  do  this   type  ofinferencing  it  seems
  44worthwhile to place this functionality in an extensible library.
  45
  46@tbd    Consider content handling (using the Unix file command)
  47@tbd    Allow parameters? (e.g. text/html; charset=UTF-8)
  48*/
  49
  50%!  file_mime_type(+FileName, -MimeType) is semidet.
  51%
  52%   True when MimeType is  the  mime-type   to  be  used for sending
  53%   FileName. The default rules can be overridden and extended using
  54%   the hook mime:mime_extension/2.
  55%
  56%   @param MimeType is a compound term of the form Type/SubType.
  57
  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).
  77
  78%!  mime:mime_extension(+Ext, -MimeType) is semidet.
  79%
  80%   Hook that is called by file_mime_type/2 before the default table
  81%   is examined.
  82
  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    ).
  92
  93%!  default_mimetype(-MimeType) is semidet.
  94%
  95%   If the mime-type cannot be determined   from the file extension,
  96%   this predicate is used as fallback.  It takes the value from the
  97%   Prolog flag =default_mimetype=. To change the default, use e.g.,
  98%
  99%     ==
 100%     :- set_prolog_flag(default_mimetype, text/plain).
 101%     ==
 102%
 103%   The initial default mime-type   is  =|application/unknown|=. Use
 104%   the value =|-|= to denote there is no default.
 105
 106:- create_prolog_flag(default_mimetype, application/unknown, []).
 107
 108default_mimetype(MimeType) :-
 109    current_prolog_flag(default_mimetype, MimeType),
 110    MimeType = _/_.
 111
 112
 113%!  ext_mimetype(+Extension, -MimeType) is semidet.
 114%
 115%   Built-in table of file-name extension to mime-type mappings.
 116%
 117%   @tbd    Update this list, e.g., from
 118%           http://www.webmaster-toolkit.com/mime-types.shtml
 119
 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').
 184
 185%!  name_mimetype(+DownCaseFileName, -MimeType) is semidet.
 186%
 187%   Determine the mime-type of files based on the entire filename.
 188
 189name_mimetype(makefile,       text/plain).
 190name_mimetype(configure,      text/plain).
 191name_mimetype('configure.in', text/plain).
 192name_mimetype('makefile.in',  text/plain).
 193name_mimetype('readme.in',    text/plain).