Changeset

11331:7a915fa49373

mod_http_file_share: Extract all file properties into variables earlier A step towards adding caching, which will unpack into the same variables.
author Kim Alvefur <zash@zash.se>
date Thu, 28 Jan 2021 16:23:38 +0100
parents 11330:f2c9492e3d25
children 11332:3e0dcdf6283e
files plugins/mod_http_file_share.lua
diffstat 1 files changed, 12 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_http_file_share.lua	Wed Jan 27 23:38:14 2021 +0100
+++ b/plugins/mod_http_file_share.lua	Thu Jan 28 16:23:38 2021 +0100
@@ -227,13 +227,21 @@
 	local request, response = event.request, event.response;
 	local slot_id = path:match("^[^/]+");
 	-- TODO cache
+	local basename, filetime, filetype, filesize;
 	local slot, when = errors.coerce(uploads:get(nil, slot_id));
 	if not slot then
 		module:log("debug", "uploads:get(%q) --> not-found, %s", slot_id, when);
+	else
+		basename = slot.attr.filename;
+		filesize = slot.attr.size;
+		filetype = slot.attr["content-type"];
+		filetime = when;
+	end
+	if not basename then
 		return 404;
 	end
 	module:log("debug", "uploads:get(%q) --> %s, %d", slot_id, slot, when);
-	local last_modified = os.date('!%a, %d %b %Y %H:%M:%S GMT', when);
+	local last_modified = os.date('!%a, %d %b %Y %H:%M:%S GMT', filetime);
 	if request.headers.if_modified_since == last_modified then
 		return 304;
 	end
@@ -243,9 +251,9 @@
 		return ferr or 410;
 	end
 	response.headers.last_modified = last_modified;
-	response.headers.content_length = slot.attr.size;
-	response.headers.content_type = slot.attr["content-type"] or "application/octet-stream";
-	response.headers.content_disposition = string.format("attachment; filename=%q", slot.attr.filename);
+	response.headers.content_length = filesize;
+	response.headers.content_type = filetype or "application/octet-stream";
+	response.headers.content_disposition = string.format("attachment; filename=%q", basename);
 
 	response.headers.cache_control = "max-age=31556952, immutable";
 	response.headers.content_security_policy =  "default-src 'none'; frame-ancestors 'none';"