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