d85b9025ef1ef106bc082876c2f3eba6036faa8f
[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 module("luci.http.protocol.mime", package.seeall)
17
18 -- MIME mapping
19 MIME_TYPES = {
20 ["txt"] = "text/plain";
21 ["js"] = "text/javascript";
22 ["css"] = "text/css";
23 ["htm"] = "text/html";
24 ["html"] = "text/html";
25
26 ["gif"] = "image/gif";
27 ["png"] = "image/png";
28 ["jpg"] = "image/jpeg";
29 ["jpeg"] = "image/jpeg";
30
31 ["xml"] = "application/xml";
32 }
33
34 -- extract extension from a filename and return corresponding mime-type or
35 -- "application/octet-stream" if the extension is unknown
36 function to_mime(filename)
37 if type(filename) == "string" then
38 local ext = filename:match("[^%.]+$")
39
40 if ext and MIME_TYPES[ext:lower()] then
41 return MIME_TYPES[ext:lower()]
42 end
43 end
44
45 return "application/octet-stream"
46 end
47
48 -- return corresponding extension for a given mime type or nil if the
49 -- given mime-type is unknown
50 function to_ext(mimetype)
51 if type(mimetype) == "string" then
52 for ext, type in luci.util.kspairs( MIME_TYPES ) do
53 if type == mimetype then
54 return ext
55 end
56 end
57 end
58
59 return nil
60 end