* luci/libs/http: added inline documentation to luci.http.protocol & friends, fixed...
[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 ["doc"] = "application/msword";
48 ["ppt"] = "application/vnd.ms-powerpoint";
49 ["xls"] = "application/vnd.ms-excel";
50 ["odt"] = "application/vnd.oasis.opendocument.text";
51 ["odp"] = "application/vnd.oasis.opendocument.presentation";
52 ["pl"] = "application/x-perl";
53 ["sh"] = "application/x-shellscript";
54 ["php"] = "application/x-php";
55 ["deb"] = "application/x-deb";
56 ["iso"] = "application/x-cd-image";
57 ["tgz"] = "application/x-compressed-tar";
58
59 ["mp3"] = "audio/mpeg";
60 ["ogg"] = "audio/x-vorbis+ogg";
61 ["wav"] = "audio/x-wav";
62
63 ["mpg"] = "video/mpeg";
64 ["mpeg"] = "video/mpeg";
65 ["avi"] = "video/x-msvideo";
66 }
67
68 --- Extract extension from a filename and return corresponding mime-type or
69 -- "application/octet-stream" if the extension is unknown.
70 -- @param filename The filename for which the mime type is guessed
71 -- @return String containign the determined mime type
72 function to_mime(filename)
73 if type(filename) == "string" then
74 local ext = filename:match("[^%.]+$")
75
76 if ext and MIME_TYPES[ext:lower()] then
77 return MIME_TYPES[ext:lower()]
78 end
79 end
80
81 return "application/octet-stream"
82 end
83
84 --- Return corresponding extension for a given mime type or nil if the
85 -- given mime-type is unknown.
86 -- @param mimetype The mimetype to retrieve the extension from
87 -- @return String with the extension or nil for unknown type
88 function to_ext(mimetype)
89 if type(mimetype) == "string" then
90 for ext, type in luci.util.kspairs( MIME_TYPES ) do
91 if type == mimetype then
92 return ext
93 end
94 end
95 end
96
97 return nil
98 end