* libs/http: removed protocol.filter, added mimetypes to protocol.mime
[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 require("luci.util")
19
20
21 -- MIME mapping
22 MIME_TYPES = {
23     ["txt"]   = "text/plain";
24     ["js"]    = "text/javascript";
25     ["css"]   = "text/css";
26     ["htm"]   = "text/html";
27     ["html"]  = "text/html";
28     ["patch"] = "text/x-patch";
29     ["c"]     = "text/x-csrc";
30     ["h"]     = "text/x-chdr";
31     ["o"]     = "text/x-object";
32     ["ko"]    = "text/x-object";
33
34     ["bmp"]   = "image/bmp";
35     ["gif"]   = "image/gif";
36     ["png"]   = "image/png";
37     ["jpg"]   = "image/jpeg";
38     ["jpeg"]  = "image/jpeg";
39     ["svg"]   = "image/svg+xml";
40
41     ["zip"]   = "application/zip";
42     ["pdf"]   = "application/pdf";
43     ["xml"]   = "application/xml";
44     ["doc"]   = "application/msword";
45     ["ppt"]   = "application/vnd.ms-powerpoint";
46     ["xls"]   = "application/vnd.ms-excel";
47     ["odt"]   = "application/vnd.oasis.opendocument.text";
48     ["odp"]   = "application/vnd.oasis.opendocument.presentation";
49     ["pl"]    = "application/x-perl";
50     ["sh"]    = "application/x-shellscript";
51     ["php"]   = "application/x-php";
52     ["deb"]   = "application/x-deb";
53     ["iso"]   = "application/x-cd-image";
54     ["tgz"]   = "application/x-compressed-tar";
55
56     ["mp3"]   = "audio/mpeg";
57     ["ogg"]   = "audio/x-vorbis+ogg";
58     ["wav"]   = "audio/x-wav";
59
60     ["mpg"]   = "video/mpeg";
61     ["mpeg"]  = "video/mpeg";
62     ["avi"]   = "video/x-msvideo";
63 }
64
65 -- extract extension from a filename and return corresponding mime-type or
66 -- "application/octet-stream" if the extension is unknown
67 function to_mime(filename)
68         if type(filename) == "string" then
69                 local ext = filename:match("[^%.]+$")
70
71                 if ext and MIME_TYPES[ext:lower()] then
72                         return MIME_TYPES[ext:lower()]
73                 end
74         end
75
76         return "application/octet-stream"
77 end
78
79 -- return corresponding extension for a given mime type or nil if the
80 -- given mime-type is unknown
81 function to_ext(mimetype)
82         if type(mimetype) == "string" then
83                 for ext, type in luci.util.kspairs( MIME_TYPES ) do
84                         if type == mimetype then
85                                 return ext
86                         end
87                 end
88         end
89
90         return nil
91 end