3 HTTP protocol implementation for LuCI - mime handling
4 (c) 2008 Freifunk Leipzig / Jo-Philipp Wich <xm@leipzig.freifunk.net>
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
10 http://www.apache.org/licenses/LICENSE-2.0
16 --- LuCI http protocol implementation - mime helper class.
17 -- This class provides functions to guess mime types from file extensions and
19 module("luci.http.protocol.mime", package.seeall)
23 --- MIME mapping table containg extension - mimetype relations.
26 ["txt"] = "text/plain";
27 ["js"] = "text/javascript";
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";
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";
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";
60 ["mp3"] = "audio/mpeg";
61 ["ogg"] = "audio/x-vorbis+ogg";
62 ["wav"] = "audio/x-wav";
64 ["mpg"] = "video/mpeg";
65 ["mpeg"] = "video/mpeg";
66 ["avi"] = "video/x-msvideo";
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("[^%.]+$")
77 if ext and MIME_TYPES[ext:lower()] then
78 return MIME_TYPES[ext:lower()]
82 return "application/octet-stream"
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