comparison mod_http_pep_avatar/mod_http_pep_avatar.lua @ 4038:02d238799537

mod_http_pep_avatar: Serve multiple avatars with a user/hash syntax
author Kim Alvefur <zash@zash.se>
date Tue, 02 Jun 2020 20:00:03 +0200
parents 00bdecb12779
children
comparison
equal deleted inserted replaced
4037:991090cb5d18 4038:02d238799537
9 local urlencode = require "util.http".urlencode; 9 local urlencode = require "util.http".urlencode;
10 10
11 module:depends("http") 11 module:depends("http")
12 module:provides("http", { 12 module:provides("http", {
13 route = { 13 route = {
14 ["GET /*"] = function (event, user) 14 ["GET /*"] = function (event, path)
15 if user == "" then 15 if path == "" then
16 return [[<h1>Hello from mod_http_pep_avatar</h1><p>This module provides access to public avatars of local users.</p>]]; 16 return [[<h1>Hello from mod_http_pep_avatar</h1><p>This module provides access to public avatars of local users.</p>]];
17 end; 17 end;
18 18
19 local request, response = event.request, event.response; 19 local request, response = event.request, event.response;
20 local actor = request.ip; 20 local actor = request.ip;
21 21
22 local user, item_id = path:match("^([^/]+)/(%x+)$");
23 if not user then user = path; end
22 local prepped = nodeprep(user); 24 local prepped = nodeprep(user);
23 if not prepped then return 400; end 25 if not prepped then return 400; end
24 if prepped ~= user then 26 if prepped ~= user then
25 response.headers.location = module:http_url() .. "/" .. urlencode(prepped); 27 response.headers.location = module:http_url() .. "/" .. urlencode(prepped);
26 return 302; 28 return 302;
35 37
36 if not ok or not avatar_hash then 38 if not ok or not avatar_hash then
37 return 404; 39 return 404;
38 end 40 end
39 41
40 if avatar_hash == request.headers.if_none_match then 42 if (item_id or avatar_hash) == request.headers.if_none_match then
41 return 304; 43 return 304;
42 end 44 end
43 45
44 local data_ok, avatar_data = pep_service:get_items("urn:xmpp:avatar:data", actor, avatar_hash); 46 local data_ok, avatar_data = pep_service:get_items("urn:xmpp:avatar:data", actor, item_id or avatar_hash);
45 if not data_ok or type(avatar_data) ~= "table" or not avatar_data[avatar_hash] then 47 if not data_ok or type(avatar_data) ~= "table" or not avatar_data[item_id or avatar_hash] then
46 return 404; 48 return 404;
47 end 49 end
48 50
49 response.headers.etag = avatar_hash; 51 local info = avatar_meta.tags[1]:get_child("info");
52 if item_id and info.attr.id ~= item_id then
53 info = nil;
54 for altinfo in avatar_meta.tags[1]:childtags("info") do
55 if altinfo.attr.id == item_id then
56 info = altinfo;
57 end
58 end
59 end
50 60
51 local info = avatar_meta.tags[1]:get_child("info"); 61 if not info then
62 return 404;
63 end
64
65 response.headers.etag = item_id or avatar_hash;
66
52 response.headers.content_type = info and info.attr.type or "application/octet-stream"; 67 response.headers.content_type = info and info.attr.type or "application/octet-stream";
53 68
54 local data = avatar_data[avatar_hash]; 69 local data = avatar_data[item_id or avatar_hash];
55 return base64_decode(data.tags[1]:get_text()); 70 return base64_decode(data.tags[1]:get_text());
56 end; 71 end;
57 } 72 }
58 }); 73 });