Software /
code /
prosody-modules
Comparison
mod_http_dir_listing2/mod_http_dir_listing2.lua @ 3002:c91c9b87929e
mod_http_dir_listing2: Switch to util.interpolation for HTML rendering
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 16 Apr 2018 21:18:47 +0200 |
parent | 3001:1108a40c3118 |
child | 3003:ec2984aa53db |
comparison
equal
deleted
inserted
replaced
3001:1108a40c3118 | 3002:c91c9b87929e |
---|---|
9 local server = require"net.http.server"; | 9 local server = require"net.http.server"; |
10 local lfs = require "lfs"; | 10 local lfs = require "lfs"; |
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 st = require"util.stanza"; |
15 local template = require"util.template"; | 15 local render = require"util.interpolation".new("%b{}", st.xml_escape); |
16 | 16 |
17 local mime = module:shared("/*/http_files/mime"); | 17 local mime = module:shared("/*/http_files/mime"); |
18 | 18 |
19 local function get_resource(resource) | 19 local function get_resource(resource) |
20 local fh = assert(module:load_resource(resource)); | 20 local fh = assert(module:load_resource(resource)); |
21 local data = fh:read"*a"; | 21 local data = fh:read"*a"; |
22 fh:close(); | 22 fh:close(); |
23 return data; | 23 return data; |
24 end | 24 end |
25 | 25 |
26 local dir_index_template = template(get_resource("resources/template.html")); | 26 local dir_index_template = get_resource("resources/template.html"); |
27 local style = get_resource("resources/style.css"):gsub("url%((.-)%)", function(url) | 27 local style = get_resource("resources/style.css"):gsub("url%((.-)%)", function(url) |
28 --module:log("debug", "Inlineing %s", url); | 28 --module:log("debug", "Inlineing %s", url); |
29 return "url(data:image/png;base64,"..base64_encode(get_resource("resources/"..url))..")"; | 29 return "url(data:image/png;base64,"..base64_encode(get_resource("resources/"..url))..")"; |
30 end); | 30 end); |
31 | 31 |
32 local function generate_directory_index(path, full_path) | 32 local function generate_directory_index(path, full_path) |
33 local filelist = tag("ul", { class = "filelist" } ):text"\n"; | 33 local filelist = {}; |
34 if path ~= "/" then | 34 if path ~= "/" then |
35 filelist:tag("li", { class = "parent directory" }) | 35 table.insert(filelist, { class = "parent directory", href = "..", rel = "up", text = "Parent Directory" }); |
36 :tag("a", { href = "..", rel = "up" }):text("Parent Directory"):up():up():text"\n" | |
37 end | 36 end |
38 local mime_map = mime.types; | 37 local mime_map = mime.types; |
39 for file in lfs.dir(full_path) do | 38 for file in lfs.dir(full_path) do |
40 if file:sub(1,1) ~= "." then | 39 if file:sub(1,1) ~= "." then |
41 local attr = stat(full_path..file) or {}; | 40 local attr = stat(full_path..file) or {}; |
42 local path = { file }; | 41 local path = { file }; |
43 local file_ext = file:match"%.([^.]+)$"; | 42 local file_ext = file:match"%.([^.]+)$"; |
44 local type = attr.mode == "file" and file_ext and mime_map and mime_map[file_ext] or nil; | 43 local type = attr.mode == "file" and file_ext and mime_map and mime_map[file_ext] or nil; |
45 local class = table.concat({ attr.mode or "unknown", file_ext, type and type:match"^[^/]+" }, " "); | 44 local class = table.concat({ attr.mode or "unknown", file_ext, type and type:match"^[^/]+" }, " "); |
46 path.is_directory = attr.mode == "directory"; | 45 path.is_directory = attr.mode == "directory"; |
47 filelist:tag("li", { class = class }) | 46 table.insert(filelist, { class = class, href = build_path(path), type = type, text = file }); |
48 :tag("a", { href = build_path(path), type = type }):text(file):up() | |
49 :up():text"\n"; | |
50 end | 47 end |
51 end | 48 end |
52 return "<!DOCTYPE html>\n"..tostring(dir_index_template.apply{ | 49 return render(dir_index_template, { |
53 path = path, | 50 path = path, |
54 style = style, | 51 style = style, |
55 filelist = filelist, | 52 filelist = filelist, |
56 footer = "Prosody "..prosody.version, | 53 footer = "Prosody "..prosody.version, |
57 }); | 54 }); |