# HG changeset patch # User bsw # Date 1427041958 -3600 # Node ID 24787d0d931756d261d3d38ca2c8e10c7041c200 # Parent ce9daada32dd866c1ec3843e7649f4cdd0f013a8 Updated delivery and cache control of member images diff -r ce9daada32dd -r 24787d0d9317 app/main/member_image/show.lua --- a/app/main/member_image/show.lua Sun Mar 22 17:32:05 2015 +0100 +++ b/app/main/member_image/show.lua Sun Mar 22 17:32:38 2015 +0100 @@ -1,17 +1,30 @@ local image_type = param.get("image_type") local record = MemberImage:by_pk(param.get_id(), image_type, true) -if record == nil then - local default_file = ({ avatar = "avatar.jpg", photo = nil })[image_type] or 'icons/16/lightning.png' - request.redirect{ static = default_file } - return +local data, content_type + +if record then + data = record.data + assert(record.content_type, "No content-type set for image.") + content_type = record.content_type + +else + local default_file = "avatar.jpg" + content_type = "image/jpeg" + if image_type == "photo" then + default_file = "icons/16/lightning.png" + content_type = "image/png" + end + + local filename = WEBMCP_BASE_PATH .. "static/" .. default_file + + local f = assert(io.open(filename), "Cannot open default image file") + data = f:read("*a") + f:close() + end -assert(record.content_type, "No content-type set for image.") - -slot.set_layout(nil, record.content_type) +request.allow_caching() -if record then - request.add_header("Cache-Control", "max-age=300"); -- let the client cache the image for 5 minutes - slot.put_into("data", record.data) -end +slot.set_layout(nil, content_type) +slot.put_into("data", data) diff -r ce9daada32dd -r 24787d0d9317 config/init.lua --- a/config/init.lua Sun Mar 22 17:32:05 2015 +0100 +++ b/config/init.lua Sun Mar 22 17:32:38 2015 +0100 @@ -112,7 +112,10 @@ end end - + function request.allow_caching() + request.add_header("Cache-Control", "max-age=3600"); + end + return end