File: mimetype.pl

package info (click to toggle)
swi-prolog 9.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 82,408 kB
  • sloc: ansic: 387,503; perl: 359,326; cpp: 6,613; lisp: 6,247; java: 5,540; sh: 3,147; javascript: 2,668; python: 1,900; ruby: 1,594; yacc: 845; makefile: 428; xml: 317; sed: 12; sql: 6
file content (275 lines) | stat: -rw-r--r-- 10,089 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2002-2018, University of Amsterdam
                              VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(mimetype,
          [ file_mime_type/2,           % +Path, -Type
            file_content_type/2,        % +Path, -Type
            file_content_type/3         % +Path, ?MediaType, -Type
          ]).

/** <module> 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.

@tbd    Consider content handling (using the Unix file command)
@tbd    Allow parameters? (e.g. text/html; charset=UTF-8)
*/

:- multifile
    mime:mime_extension/2,
    mime:text_mimetype/1,
    mime:charset/3.

%!  file_mime_type(+FileName, -MimeType) is semidet.
%
%   True when MimeType is  the  mime-type   to  be  used for sending
%   FileName. The default rules can be overridden and extended using
%   the hook mime:mime_extension/2.
%
%   @param MimeType is a compound term of the form Type/SubType.

file_mime_type(File, MimeType) :-
    file_name_extension(_, Ext, File),
    (   current_prolog_flag(windows, true)
    ->  downcase_atom(Ext, Lower),
        mime_extension(Lower, MimeType)
    ;   mime_extension(Ext, M0)
    ->  MimeType = M0
    ;   downcase_atom(Ext, Lower),
        mime_extension(Lower, MimeType)
    ),
    !.
file_mime_type(File, MimeType) :-
    file_base_name(File, Base),
    downcase_atom(Base, Lower),
    name_mimetype(Lower, Mime),
    !,
    MimeType = Mime.
file_mime_type(_, MimeType) :-
    default_mimetype(MimeType).

%!  mime:mime_extension(+Ext, -MimeType) is semidet.
%
%   Hook that is called by file_mime_type/2 before the default table
%   is examined.

mime_extension(Ext, MimeType) :-
    (   mime:mime_extension(Ext, Mime)
    ->  MimeType = Mime
    ;   ext_mimetype(Ext, Mime)
    ->  MimeType = Mime
    ).

%!  default_mimetype(-MimeType) is semidet.
%
%   If the mime-type cannot be determined   from the file extension,
%   this predicate is used as fallback.  It takes the value from the
%   Prolog flag =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.

:- create_prolog_flag(default_mimetype, application/unknown, [keep(true)]).

default_mimetype(MimeType) :-
    current_prolog_flag(default_mimetype, MimeType),
    MimeType = _/_.


%!  ext_mimetype(+Extension, -MimeType) is semidet.
%
%   Built-in table of file-name extension to mime-type mappings.
%
%   @tbd    Update this list, e.g., from
%           http://www.webmaster-toolkit.com/mime-types.shtml

                                        % plain text
ext_mimetype(txt,  text/plain).
                                        % markup
ext_mimetype(htm,  text/html).
ext_mimetype(html, text/html).
ext_mimetype(xhtml, application/'xhtml+xml').
ext_mimetype(sgml, text/'x-sgml').
ext_mimetype(sgm,  text/'x-sgml').
ext_mimetype(xml,  text/xml).
ext_mimetype(css,  text/css).
ext_mimetype(xsl,  text/xml).           % Unclear what this should be.
ext_mimetype(md,   text/markdown).
                                        % Other data markup
ext_mimetype(json, application/json).
ext_mimetype(yaml, application/yaml).   % Not official
                                        % semantic web stuff
ext_mimetype(rdf,  application/'rdf+xml').
ext_mimetype(rdfs, application/'rdf+xml').
ext_mimetype(owl,  application/'rdf+xml').
ext_mimetype(ttl,  application/turtle).
ext_mimetype(nt,   application/'n-triples').
ext_mimetype(nq,   application/'n-quads').
                                        % Prolog source
ext_mimetype(pl,   text/plain).
                                        % Other languages
ext_mimetype(c,    text/'x-c').
ext_mimetype(h,    text/'x-c').
ext_mimetype(cc,   text/'x-c').
ext_mimetype(py,   text/'x-python').
ext_mimetype(java, text/'x-java').
ext_mimetype(sh,   text/plain).
                                        % Packaged formats
ext_mimetype(gz,   application/'x-gzip').
ext_mimetype(zip,  application/zip).
ext_mimetype(tgz,  application/'x-gtar').
                                        % Some document formats
ext_mimetype(pdf,  application/pdf).
ext_mimetype(doc,  application/msword).
                                        % Java classes
ext_mimetype(class, application/'octet-stream').
ext_mimetype(jar,  application/'x-java-archive').
                                        % JavaScript and WASM
ext_mimetype(js,   text/javascript).
ext_mimetype(wasm, application/wasm).
ext_mimetype(data, application/'octet-stream').
                                        % Visual Basic Script :-(
ext_mimetype(vbs,  text/vbscript).
                                        % Some image formats
ext_mimetype(jpg,  image/jpeg).
ext_mimetype(jpeg, image/jpeg).
ext_mimetype(gif,  image/gif).
ext_mimetype(png,  image/png).
ext_mimetype(tif,  image/tiff).
ext_mimetype(tiff, image/tiff).
ext_mimetype(xpm,  image/'x-xpixmap').
ext_mimetype(ico,  image/'x-ico').
ext_mimetype(svg,  image/'svg+xml').
                                        % Google earth
ext_mimetype(kml,  application/'vnd.google-earth.kml+xml').
ext_mimetype(kmz,  application/'vnd.google-earth.kmz').

                                        % Flash
ext_mimetype(swf,  application/'x-shockwave-flash').
ext_mimetype(flv,  video/'x-flv').
                                        % MP3
ext_mimetype(mp3,  audio/mpeg).
                                        % Downloads
ext_mimetype(rpm,  application/'x-rpm').
ext_mimetype(exe,  application/'x-executable').

%!  name_mimetype(+DownCaseFileName, -MimeType) is semidet.
%
%   Determine the mime-type of files based on the entire filename.

name_mimetype(makefile,       text/plain).
name_mimetype(configure,      text/plain).
name_mimetype('configure.in', text/plain).
name_mimetype('configure.ac', text/plain).
name_mimetype('makefile.in',  text/plain).
name_mimetype('makefile.am',  text/plain).
name_mimetype('readme.in',    text/plain).

%!  text_mimetype(+MimeType) is semidet.
%
%   True when documents of MimeType are text documents and thus may need
%   a charset specification.

text_mimetype(MimeType) :-
    mime:text_mimetype(MimeType),
    !.
text_mimetype(text/_).

%!  file_content_type(+File:atom, -ContentType:atom) is det.
%!  file_content_type(+File:atom, ?MediaType, -ContentType:atom) is det.
%
%   True if File should be served using =|ContentType:|= ContentType. It
%   takes the following steps:
%
%     1. Determine the media type using file_mime_type/2, unless
%        already specified using file_content_type/3.
%     2. Determine it is a text file using text_mimetype/1
%     3. Use the charset from the Prolog flag `default_charset`
%
%   The behavior is controlled by several hooks and a flag.
%
%     - mime:mime_extension/2 defines the media type
%     - mime:text_mimetype/1 defines the media type is text
%     - mime:charset/3 derives the charset for a file with a given
%       media type, if the media type is text according to
%	mime:text_mimetype/1.
%     - If mime:text_mimetype/1 succeeds and mime:charset/3 fails, the
%       flag `default_charset` defines the charset unless it is set
%       to `-`.  The flag set by default to =UTF-8= if the Prolog
%       flag `encoding` is set to `utf8`.

file_content_type(File, ContentType) :-
    file_content_type(File, _, ContentType).
file_content_type(File, MediaType, ContentType) :-
    (   ground(MediaType)
    ->  true
    ;   file_mime_type(File, MediaType)
    ),
    (   text_mimetype(MediaType),
        (   mime:charset(File, MediaType, Charset0)
        ->  Charset = Charset0
        ;   default_charset(Charset)
        )
    ->  format(atom(ContentType), '~w; charset=~w', [MediaType, Charset])
    ;   format(atom(ContentType), '~w', [MediaType])
    ).

%!  mime:charset(+File, +MediaType, -Charset) is semidet.
%
%   Hook that determines the  Charset  for   File  that  has  media type
%   MediaType. This hook allows overruling file_content_type/2.
%
%   @see mime:text_mimetype/1.

default_charset(Charset) :-
    current_prolog_flag(default_charset, Charset),
    Charset \== (-).

set_default_charset :-
    current_prolog_flag(default_charset, _),
    !.
set_default_charset :-
    current_prolog_flag(encoding, utf8),
    !,
    set_prolog_flag(default_charset, 'UTF-8').
set_default_charset.

:- initialization(set_default_charset).