From 318d357557d2c219f33c5631d73c029619bf0d2b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Thu, 28 Jan 2021 16:23:38 +0100 Subject: mod_http_file_share: Extract all file properties into variables earlier A step towards adding caching, which will unpack into the same variables. --- plugins/mod_http_file_share.lua | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'plugins') diff --git a/plugins/mod_http_file_share.lua b/plugins/mod_http_file_share.lua index 6b9ccace..23454e57 100644 --- a/plugins/mod_http_file_share.lua +++ b/plugins/mod_http_file_share.lua @@ -227,13 +227,21 @@ function handle_download(event, path) -- GET /uploads/:slot+filename 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 @@ function handle_download(event, path) -- GET /uploads/:slot+filename 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';" -- cgit v1.2.3