Comparison

plugins/mod_http_file_share.lua @ 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
parent 11330:f2c9492e3d25
child 11332:3e0dcdf6283e
comparison
equal deleted inserted replaced
11330:f2c9492e3d25 11331:7a915fa49373
225 225
226 function handle_download(event, path) -- GET /uploads/:slot+filename 226 function handle_download(event, path) -- GET /uploads/:slot+filename
227 local request, response = event.request, event.response; 227 local request, response = event.request, event.response;
228 local slot_id = path:match("^[^/]+"); 228 local slot_id = path:match("^[^/]+");
229 -- TODO cache 229 -- TODO cache
230 local basename, filetime, filetype, filesize;
230 local slot, when = errors.coerce(uploads:get(nil, slot_id)); 231 local slot, when = errors.coerce(uploads:get(nil, slot_id));
231 if not slot then 232 if not slot then
232 module:log("debug", "uploads:get(%q) --> not-found, %s", slot_id, when); 233 module:log("debug", "uploads:get(%q) --> not-found, %s", slot_id, when);
234 else
235 basename = slot.attr.filename;
236 filesize = slot.attr.size;
237 filetype = slot.attr["content-type"];
238 filetime = when;
239 end
240 if not basename then
233 return 404; 241 return 404;
234 end 242 end
235 module:log("debug", "uploads:get(%q) --> %s, %d", slot_id, slot, when); 243 module:log("debug", "uploads:get(%q) --> %s, %d", slot_id, slot, when);
236 local last_modified = os.date('!%a, %d %b %Y %H:%M:%S GMT', when); 244 local last_modified = os.date('!%a, %d %b %Y %H:%M:%S GMT', filetime);
237 if request.headers.if_modified_since == last_modified then 245 if request.headers.if_modified_since == last_modified then
238 return 304; 246 return 304;
239 end 247 end
240 local filename = get_filename(slot_id); 248 local filename = get_filename(slot_id);
241 local handle, ferr = errors.coerce(io.open(filename)); 249 local handle, ferr = errors.coerce(io.open(filename));
242 if not handle then 250 if not handle then
243 return ferr or 410; 251 return ferr or 410;
244 end 252 end
245 response.headers.last_modified = last_modified; 253 response.headers.last_modified = last_modified;
246 response.headers.content_length = slot.attr.size; 254 response.headers.content_length = filesize;
247 response.headers.content_type = slot.attr["content-type"] or "application/octet-stream"; 255 response.headers.content_type = filetype or "application/octet-stream";
248 response.headers.content_disposition = string.format("attachment; filename=%q", slot.attr.filename); 256 response.headers.content_disposition = string.format("attachment; filename=%q", basename);
249 257
250 response.headers.cache_control = "max-age=31556952, immutable"; 258 response.headers.cache_control = "max-age=31556952, immutable";
251 response.headers.content_security_policy = "default-src 'none'; frame-ancestors 'none';" 259 response.headers.content_security_policy = "default-src 'none'; frame-ancestors 'none';"
252 response.headers.strict_transport_security = "max-age=31556952"; 260 response.headers.strict_transport_security = "max-age=31556952";
253 response.headers.x_content_type_options = "nosniff"; 261 response.headers.x_content_type_options = "nosniff";