comparison mod_http_dir_listing/http_dir_listing/mod_http_dir_listing.lua @ 1064:5db8debb4531

mod_http_dir_listing: Attach the MIME type to list items for use in CSS
author Kim Alvefur <zash@zash.se>
date Fri, 14 Jun 2013 12:09:00 +0200
parents a2259870495c
children cb21928bca1d
comparison
equal deleted inserted replaced
1063:b2a4679e7d20 1064:5db8debb4531
11 local stat = lfs.attributes; 11 local stat = lfs.attributes;
12 local build_path = require"socket.url".build_path; 12 local build_path = require"socket.url".build_path;
13 local base64_encode = require"util.encodings".base64.encode; 13 local base64_encode = require"util.encodings".base64.encode;
14 local tag = require"util.stanza".stanza; 14 local tag = require"util.stanza".stanza;
15 local template = require"util.template"; 15 local template = require"util.template";
16
17 module:depends"http_files";
18
19 local mime = module:shared("/*/http_files/mime");
16 20
17 local function get_resource(resource) 21 local function get_resource(resource)
18 local fh = assert(module:load_resource(resource)); 22 local fh = assert(module:load_resource(resource));
19 local data = fh:read"*a"; 23 local data = fh:read"*a";
20 fh:close(); 24 fh:close();
31 local filelist = tag("ul", { class = "filelist" } ):text"\n"; 35 local filelist = tag("ul", { class = "filelist" } ):text"\n";
32 if path ~= "/" then 36 if path ~= "/" then
33 filelist:tag("li", { class = "parent directory" }) 37 filelist:tag("li", { class = "parent directory" })
34 :tag("a", { href = "..", rel = "up" }):text("Parent Directory"):up():up():text"\n" 38 :tag("a", { href = "..", rel = "up" }):text("Parent Directory"):up():up():text"\n"
35 end 39 end
40 local mime_map = mime.types;
36 for file in lfs.dir(full_path) do 41 for file in lfs.dir(full_path) do
37 if file:sub(1,1) ~= "." then 42 if file:sub(1,1) ~= "." then
38 local attr = stat(full_path..file) or {}; 43 local attr = stat(full_path..file) or {};
39 local path = { file }; 44 local path = { file };
45 local file_ext = file:match"%.(.-)$";
46 local type = attr.mode == "file" and mime_map and mime_map[file_ext] or nil;
47 local class = table.concat({ attr.mode or "unknown", file_ext, type and type:match"^[^/]+" }, " ");
40 path.is_directory = attr.mode == "directory"; 48 path.is_directory = attr.mode == "directory";
41 filelist:tag("li", { class = attr.mode }) 49 filelist:tag("li", { class = class })
42 :tag("a", { href = build_path(path) }):text(file):up() 50 :tag("a", { href = build_path(path), type = type }):text(file):up()
43 :up():text"\n"; 51 :up():text"\n";
44 end 52 end
45 end 53 end
46 return "<!DOCTYPE html>\n"..tostring(dir_index_template.apply{ 54 return "<!DOCTYPE html>\n"..tostring(dir_index_template.apply{
47 path = path, 55 path = path,