View source with formatted 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-2018, 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            file_content_type/2,        % +Path, -Type
   39            file_content_type/3         % +Path, ?MediaType, -Type
   40          ]).   41
   42/** <module> Determine mime-type for a file
   43
   44Simple library to guess the mime-type from   the extension of a file. As
   45various applications need  to  do  this   type  ofinferencing  it  seems
   46worthwhile to place this functionality in an extensible library.
   47
   48@tbd    Consider content handling (using the Unix file command)
   49@tbd    Allow parameters? (e.g. text/html; charset=UTF-8)
   50*/
   51
   52:- multifile
   53    mime:mime_extension/2,
   54    mime:text_mimetype/1,
   55    mime:charset/3.   56
   57%!  file_mime_type(+FileName, -MimeType) is semidet.
   58%
   59%   True when MimeType is  the  mime-type   to  be  used for sending
   60%   FileName. The default rules can be overridden and extended using
   61%   the hook mime:mime_extension/2.
   62%
   63%   @param MimeType is a compound term of the form Type/SubType.
   64
   65file_mime_type(File, MimeType) :-
   66    file_name_extension(_, Ext, File),
   67    (   current_prolog_flag(windows, true)
   68    ->  downcase_atom(Ext, Lower),
   69        mime_extension(Lower, MimeType)
   70    ;   mime_extension(Ext, M0)
   71    ->  MimeType = M0
   72    ;   downcase_atom(Ext, Lower),
   73        mime_extension(Lower, MimeType)
   74    ),
   75    !.
   76file_mime_type(File, MimeType) :-
   77    file_base_name(File, Base),
   78    downcase_atom(Base, Lower),
   79    name_mimetype(Lower, Mime),
   80    !,
   81    MimeType = Mime.
   82file_mime_type(_, MimeType) :-
   83    default_mimetype(MimeType).
   84
   85%!  mime:mime_extension(+Ext, -MimeType) is semidet.
   86%
   87%   Hook that is called by file_mime_type/2 before the default table
   88%   is examined.
   89
   90mime_extension(Ext, MimeType) :-
   91    (   mime:mime_extension(Ext, Mime)
   92    ->  MimeType = Mime
   93    ;   ext_mimetype(Ext, Mime)
   94    ->  MimeType = Mime
   95    ).
   96
   97%!  default_mimetype(-MimeType) is semidet.
   98%
   99%   If the mime-type cannot be determined   from the file extension,
  100%   this predicate is used as fallback.  It takes the value from the
  101%   Prolog flag =default_mimetype=. To change the default, use e.g.,
  102%
  103%     ==
  104%     :- set_prolog_flag(default_mimetype, text/plain).
  105%     ==
  106%
  107%   The initial default mime-type   is  =|application/unknown|=. Use
  108%   the value =|-|= to denote there is no default.
  109
  110:- create_prolog_flag(default_mimetype, application/unknown, [keep(true)]).  111
  112default_mimetype(MimeType) :-
  113    current_prolog_flag(default_mimetype, MimeType),
  114    MimeType = _/_.
  115
  116
  117%!  ext_mimetype(+Extension, -MimeType) is semidet.
  118%
  119%   Built-in table of file-name extension to mime-type mappings.
  120%
  121%   @tbd    Update this list, e.g., from
  122%           http://www.webmaster-toolkit.com/mime-types.shtml
  123
  124                                        % plain text
  125ext_mimetype(txt,  text/plain).
  126                                        % markup
  127ext_mimetype(htm,  text/html).
  128ext_mimetype(html, text/html).
  129ext_mimetype(xhtml, application/'xhtml+xml').
  130ext_mimetype(sgml, text/'x-sgml').
  131ext_mimetype(sgm,  text/'x-sgml').
  132ext_mimetype(xml,  text/xml).
  133ext_mimetype(css,  text/css).
  134ext_mimetype(xsl,  text/xml).           % Unclear what this should be.
  135ext_mimetype(md,   text/markdown).
  136                                        % Other data markup
  137ext_mimetype(json, application/json).
  138ext_mimetype(yaml, application/yaml).   % Not official
  139                                        % semantic web stuff
  140ext_mimetype(rdf,  application/'rdf+xml').
  141ext_mimetype(rdfs, application/'rdf+xml').
  142ext_mimetype(owl,  application/'rdf+xml').
  143ext_mimetype(ttl,  application/turtle).
  144ext_mimetype(nt,   application/'n-triples').
  145ext_mimetype(nq,   application/'n-quads').
  146                                        % Prolog source
  147ext_mimetype(pl,   text/plain).
  148                                        % Other languages
  149ext_mimetype(c,    text/'x-c').
  150ext_mimetype(h,    text/'x-c').
  151ext_mimetype(cc,   text/'x-c').
  152ext_mimetype(py,   text/'x-python').
  153ext_mimetype(java, text/'x-java').
  154ext_mimetype(sh,   text/plain).
  155                                        % Packaged formats
  156ext_mimetype(gz,   application/'x-gzip').
  157ext_mimetype(zip,  application/zip).
  158ext_mimetype(tgz,  application/'x-gtar').
  159                                        % Some document formats
  160ext_mimetype(pdf,  application/pdf).
  161ext_mimetype(doc,  application/msword).
  162                                        % Java classes
  163ext_mimetype(class, application/'octet-stream').
  164ext_mimetype(jar,  application/'x-java-archive').
  165                                        % JavaScript and WASM
  166ext_mimetype(js,   text/javascript).
  167ext_mimetype(wasm, application/wasm).
  168ext_mimetype(data, application/'octet-stream').
  169                                        % Visual Basic Script :-(
  170ext_mimetype(vbs,  text/vbscript).
  171                                        % Some image formats
  172ext_mimetype(jpg,  image/jpeg).
  173ext_mimetype(jpeg, image/jpeg).
  174ext_mimetype(gif,  image/gif).
  175ext_mimetype(png,  image/png).
  176ext_mimetype(tif,  image/tiff).
  177ext_mimetype(tiff, image/tiff).
  178ext_mimetype(xpm,  image/'x-xpixmap').
  179ext_mimetype(ico,  image/'x-ico').
  180ext_mimetype(svg,  image/'svg+xml').
  181                                        % Google earth
  182ext_mimetype(kml,  application/'vnd.google-earth.kml+xml').
  183ext_mimetype(kmz,  application/'vnd.google-earth.kmz').
  184
  185                                        % Flash
  186ext_mimetype(swf,  application/'x-shockwave-flash').
  187ext_mimetype(flv,  video/'x-flv').
  188                                        % MP3
  189ext_mimetype(mp3,  audio/mpeg).
  190                                        % Downloads
  191ext_mimetype(rpm,  application/'x-rpm').
  192ext_mimetype(exe,  application/'x-executable').
  193
  194%!  name_mimetype(+DownCaseFileName, -MimeType) is semidet.
  195%
  196%   Determine the mime-type of files based on the entire filename.
  197
  198name_mimetype(makefile,       text/plain).
  199name_mimetype(configure,      text/plain).
  200name_mimetype('configure.in', text/plain).
  201name_mimetype('configure.ac', text/plain).
  202name_mimetype('makefile.in',  text/plain).
  203name_mimetype('makefile.am',  text/plain).
  204name_mimetype('readme.in',    text/plain).
  205
  206%!  text_mimetype(+MimeType) is semidet.
  207%
  208%   True when documents of MimeType are text documents and thus may need
  209%   a charset specification.
  210
  211text_mimetype(MimeType) :-
  212    mime:text_mimetype(MimeType),
  213    !.
  214text_mimetype(text/_).
  215
  216%!  file_content_type(+File:atom, -ContentType:atom) is det.
  217%!  file_content_type(+File:atom, ?MediaType, -ContentType:atom) is det.
  218%
  219%   True if File should be served using =|ContentType:|= ContentType. It
  220%   takes the following steps:
  221%
  222%     1. Determine the media type using file_mime_type/2, unless
  223%        already specified using file_content_type/3.
  224%     2. Determine it is a text file using text_mimetype/1
  225%     3. Use the charset from the Prolog flag `default_charset`
  226%
  227%   The behavior is controlled by several hooks and a flag.
  228%
  229%     - mime:mime_extension/2 defines the media type
  230%     - mime:text_mimetype/1 defines the media type is text
  231%     - mime:charset/3 derives the charset for a file with a given
  232%       media type, if the media type is text according to
  233%	mime:text_mimetype/1.
  234%     - If mime:text_mimetype/1 succeeds and mime:charset/3 fails, the
  235%       flag `default_charset` defines the charset unless it is set
  236%       to `-`.  The flag set by default to =UTF-8= if the Prolog
  237%       flag `encoding` is set to `utf8`.
  238
  239file_content_type(File, ContentType) :-
  240    file_content_type(File, _, ContentType).
  241file_content_type(File, MediaType, ContentType) :-
  242    (   ground(MediaType)
  243    ->  true
  244    ;   file_mime_type(File, MediaType)
  245    ),
  246    (   text_mimetype(MediaType),
  247        (   mime:charset(File, MediaType, Charset0)
  248        ->  Charset = Charset0
  249        ;   default_charset(Charset)
  250        )
  251    ->  format(atom(ContentType), '~w; charset=~w', [MediaType, Charset])
  252    ;   format(atom(ContentType), '~w', [MediaType])
  253    ).
  254
  255%!  mime:charset(+File, +MediaType, -Charset) is semidet.
  256%
  257%   Hook that determines the  Charset  for   File  that  has  media type
  258%   MediaType. This hook allows overruling file_content_type/2.
  259%
  260%   @see mime:text_mimetype/1.
  261
  262default_charset(Charset) :-
  263    current_prolog_flag(default_charset, Charset),
  264    Charset \== (-).
  265
  266set_default_charset :-
  267    current_prolog_flag(default_charset, _),
  268    !.
  269set_default_charset :-
  270    current_prolog_flag(encoding, utf8),
  271    !,
  272    set_prolog_flag(default_charset, 'UTF-8').
  273set_default_charset.
  274
  275:- initialization(set_default_charset).