changeset 886:1647b4fac445

mod_http_dir_index: Add. Handle generation of directory listings for mod_http_files. Included icons from the Tango project are Public Domain
author Kim Alvefur <zash@zash.se>
date Sun, 23 Dec 2012 15:36:33 +0100
parents 292ea8df7267
children a2259870495c
files mod_http_dir_index/http_dir_index/mod_http_dir_index.lua mod_http_dir_index/http_dir_index/resources/folder.png mod_http_dir_index/http_dir_index/resources/style.css mod_http_dir_index/http_dir_index/resources/template.html mod_http_dir_index/http_dir_index/resources/text-x-generic.png mod_http_dir_index/http_dir_index/resources/user-home.png
diffstat 6 files changed, 82 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_http_dir_index/http_dir_index/mod_http_dir_index.lua	Sun Dec 23 15:36:33 2012 +0100
@@ -0,0 +1,58 @@
+-- Prosody IM
+-- Copyright (C) 2012 Kim Alvefur
+-- 
+-- This project is MIT/X11 licensed. Please see the
+-- COPYING file in the source package for more information.
+--
+
+module:set_global();
+local server = require"net.http.server";
+local lfs = require "lfs";
+local stat = lfs.attributes;
+local build_path = require"socket.url".build_path;
+local base64_encode = require"util.encodings".base64.encode;
+local tag = require"util.stanza".stanza;
+local template = require"util.template";
+
+local function get_resource(resource)
+	local fh = assert(module:load_resource(resource));
+	local data = fh:read"*a";
+	fh:close();
+	return data;
+end
+
+local dir_index_template = template(get_resource("resources/template.html"));
+local style = get_resource("resources/style.css"):gsub("url%((.-)%)", function(url)
+	--module:log("debug", "Inlineing %s", url);
+	return "url(data:image/png;base64,"..base64_encode(get_resource("resources/"..url))..")";
+end);
+
+local function generate_directory_index(path, full_path)
+	local filelist = tag("ul", { class = "filelist" } ):text"\n";
+	if path ~= "/" then
+		filelist:tag("li", { class = "parent directory" })
+			:tag("a", { href = "..", rel = "up" }):text("Parent Directory"):up():up():text"\n"
+	end
+	for file in lfs.dir(full_path) do
+		if file:sub(1,1) ~= "." then
+			local attr = stat(full_path..file) or {};
+			local path = { file };
+			path.is_directory = attr.mode == "directory";
+			filelist:tag("li", { class = attr.mode })
+				:tag("a", { href = build_path(path) }):text(file):up()
+			:up():text"\n";
+		end
+	end
+	return "<!DOCTYPE html>\n"..tostring(dir_index_template.apply{
+		path = path,
+		style = style,
+		filelist = filelist,
+		footer = "Prosody "..prosody.version,
+	});
+end
+
+module:hook_object_event(server, "directory-index", function (event)
+	local ok, data = pcall(generate_directory_index, event.path, event.full_path);
+	if ok then return data end
+	module:log("warn", data);
+end);
Binary file mod_http_dir_index/http_dir_index/resources/folder.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_http_dir_index/http_dir_index/resources/style.css	Sun Dec 23 15:36:33 2012 +0100
@@ -0,0 +1,10 @@
+
+body{background-color:#eeeeec;font-family:sans-serif;}
+h1{font-size:xx-large;}
+a:link,a:visited{color:#2e3436;text-decoration:none;}
+a:link:hover,a:visited:hover{color:#3465a4;}
+.filelist{background-color:white;padding:1em;list-style-position:inside;-moz-column-width:20em;-webkit-column-width:20em;-ms-column-width:20em;column-width:20em;}
+.file{list-style-image:url(text-x-generic.png);}
+.directory{list-style-image:url(folder.png);}
+.parent{list-style-image:url(user-home.png);}
+footer{margin-top:1ex;font-size:smaller;color:#babdb6;}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_http_dir_index/http_dir_index/resources/template.html	Sun Dec 23 15:36:33 2012 +0100
@@ -0,0 +1,14 @@
+<html>
+  <head>
+    <title>Index of {path}</title>
+    <meta charset="utf-8"/>
+    <style>{style}</style>
+  </head>
+  <body>
+    <h1>Index of {path}</h1>
+
+		{filelist}
+
+    <footer>{footer}</footer>
+  </body>
+</html>
Binary file mod_http_dir_index/http_dir_index/resources/text-x-generic.png has changed
Binary file mod_http_dir_index/http_dir_index/resources/user-home.png has changed