libs/http: map *.xsl files to application/xml
[project/luci.git] / libs / http / luasrc / http / protocol / mime.lua
1 --[[
2
3 HTTP protocol implementation for LuCI - mime handling
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 --- LuCI http protocol implementation - mime helper class.
17 -- This class provides functions to guess mime types from file extensions and
18 -- vice versa.
19 module("luci.http.protocol.mime", package.seeall)
20
21 require("luci.util")
22
23 --- MIME mapping table containg extension - mimetype relations.
24 -- @class table
25 MIME_TYPES = {
26 ["txt"] = "text/plain";
27 ["js"] = "text/javascript";
28 ["css"] = "text/css";
29 ["htm"] = "text/html";
30 ["html"] = "text/html";
31 ["patch"] = "text/x-patch";
32 ["c"] = "text/x-csrc";
33 ["h"] = "text/x-chdr";
34 ["o"] = "text/x-object";
35 ["ko"] = "text/x-object";
36
37 ["bmp"] = "image/bmp";
38 ["gif"] = "image/gif";
39 ["png"] = "image/png";
40 ["jpg"] = "image/jpeg";
41 ["jpeg"] = "image/jpeg";
42 ["svg"] = "image/svg+xml";
43
44 ["zip"] = "application/zip";
45 ["pdf"] = "application/pdf";
46 ["xml"] = "application/xml";
47 ["xsl"] = "application/xml";
48 ["doc"] = "application/msword";
49 ["ppt"] = "application/vnd.ms-powerpoint";
50 ["xls"] = "application/vnd.ms-excel";
51 ["odt"] = "application/vnd.oasis.opendocument.text";
52 ["odp"] = "application/vnd.oasis.opendocument.presentation";
53 ["pl"] = "application/x-perl";
54 ["sh"] = "application/x-shellscript";
55 ["php"] = "application/x-php";
56 ["deb"] = "application/x-deb";
57 ["iso"] = "application/x-cd-image";
58 ["tgz"] = "application/x-compressed-tar";
59
60 ["mp3"] = "audio/mpeg";
61 ["ogg"] = "audio/x-vorbis+ogg";
62 ["wav"] = "audio/x-wav";
63
64 ["mpg"] = "video/mpeg";
65 ["mpeg"] = "video/mpeg";
66 ["avi"] = "video/x-msvideo";
67 }
68
69 --- Extract extension from a filename and return corresponding mime-type or
70 -- "application/octet-stream" if the extension is unknown.
71 -- @param filename The filename for which the mime type is guessed
72 -- @return String containign the determined mime type
73 function to_mime(filename)
74 if type(filename) == "string" then
75 local ext = filename:match("[^%.]+$")
76
77 if ext and MIME_TYPES[ext:lower()] then
78 return MIME_TYPES[ext:lower()]
79 end
80 end
81
82 return "application/octet-stream"
83 end
84
85 --- Return corresponding extension for a given mime type or nil if the
86 -- given mime-type is unknown.
87 -- @param mimetype The mimetype to retrieve the extension from
88 -- @return String with the extension or nil for unknown type
89 function to_ext(mimetype)
90 if type(mimetype) == "string" then
91 for ext, type in luci.util.kspairs( MIME_TYPES ) do
92 if type == mimetype then
93 return ext
94 end
95 end
96 end
97
98 return nil
99 end