0bcff8a36b83e6e7780e747c8c1ba78694fd51fb
[project/luci.git] / libs / luci-lib-httpprotoutils / luasrc / http / mime.lua
1 -- Copyright 2008 Freifunk Leipzig / Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 -- This class provides functions to guess mime types from file extensions and
5 -- vice versa.
6 module("luci.http.mime", package.seeall)
7
8 require("luci.util")
9
10 MIME_TYPES = {
11 ["txt"] = "text/plain";
12 ["js"] = "text/javascript";
13 ["css"] = "text/css";
14 ["htm"] = "text/html";
15 ["html"] = "text/html";
16 ["patch"] = "text/x-patch";
17 ["c"] = "text/x-csrc";
18 ["h"] = "text/x-chdr";
19 ["o"] = "text/x-object";
20 ["ko"] = "text/x-object";
21
22 ["bmp"] = "image/bmp";
23 ["gif"] = "image/gif";
24 ["png"] = "image/png";
25 ["jpg"] = "image/jpeg";
26 ["jpeg"] = "image/jpeg";
27 ["svg"] = "image/svg+xml";
28
29 ["zip"] = "application/zip";
30 ["pdf"] = "application/pdf";
31 ["xml"] = "application/xml";
32 ["xsl"] = "application/xml";
33 ["doc"] = "application/msword";
34 ["ppt"] = "application/vnd.ms-powerpoint";
35 ["xls"] = "application/vnd.ms-excel";
36 ["odt"] = "application/vnd.oasis.opendocument.text";
37 ["odp"] = "application/vnd.oasis.opendocument.presentation";
38 ["pl"] = "application/x-perl";
39 ["sh"] = "application/x-shellscript";
40 ["php"] = "application/x-php";
41 ["deb"] = "application/x-deb";
42 ["iso"] = "application/x-cd-image";
43 ["tgz"] = "application/x-compressed-tar";
44
45 ["mp3"] = "audio/mpeg";
46 ["ogg"] = "audio/x-vorbis+ogg";
47 ["wav"] = "audio/x-wav";
48
49 ["mpg"] = "video/mpeg";
50 ["mpeg"] = "video/mpeg";
51 ["avi"] = "video/x-msvideo";
52 }
53
54 -- "application/octet-stream" if the extension is unknown.
55 function to_mime(filename)
56 if type(filename) == "string" then
57 local ext = filename:match("[^%.]+$")
58
59 if ext and MIME_TYPES[ext:lower()] then
60 return MIME_TYPES[ext:lower()]
61 end
62 end
63
64 return "application/octet-stream"
65 end
66
67 -- given mime-type is unknown.
68 function to_ext(mimetype)
69 if type(mimetype) == "string" then
70 for ext, type in luci.util.kspairs( MIME_TYPES ) do
71 if type == mimetype then
72 return ext
73 end
74 end
75 end
76
77 return nil
78 end