# HG changeset patch # User Kim Alvefur # Date 1611847418 -3600 # Node ID 7a915fa4937319eec60d5cbd13f5bbfac0a9e03b # Parent f2c9492e3d258f3ea4501a31b5dd1908651876f7 mod_http_file_share: Extract all file properties into variables earlier A step towards adding caching, which will unpack into the same variables. diff -r f2c9492e3d25 -r 7a915fa49373 plugins/mod_http_file_share.lua --- 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';"