Annotate

mod_http_avatar/mod_http_avatar.lua @ 3082:1cff081abbed

mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 01 Jun 2018 22:05:46 +0200
child 3083:46d71b38bc16
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3082
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 -- Prosody IM
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 -- Copyright (C) 2018 Emmanuel Gil Peyrot
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 --
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 -- This project is MIT/X11 licensed. Please see the
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 -- COPYING file in the source package for more information.
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 local base64 = require"util.encodings".base64;
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 local st = require"util.stanza";
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 module:depends"http";
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 local vcard_storage = module:open_store"vcard";
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 local default_avatar = [[<svg xmlns='http://www.w3.org/2000/svg' version='1.1' viewBox='0 0 150 150'>
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 <rect width='150' height='150' fill='#888' stroke-width='1' stroke='#000'/>
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15 <text x='75' y='100' text-anchor='middle' font-size='100'>?</text>
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 </svg>]];
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 local function get_avatar(event, path)
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 local request, response = event.request, event.response;
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 local photo_type, binval;
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 local vcard, err = vcard_storage:get(path);
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 if vcard then
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 vcard = st.deserialize(vcard);
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 local photo = vcard:get_child("PHOTO", "vcard-temp");
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 if photo then
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 photo_type = photo:get_child_text("TYPE", "vcard-temp");
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 binval = photo:get_child_text("BINVAL", "vcard-temp");
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 end
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 end
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 if not photo_type or not binval then
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 response.status_code = 404;
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 response.headers.content_type = "image/svg+xml";
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 return default_avatar;
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 end
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 response.headers.content_type = photo_type;
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 return base64.decode(binval);
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 end
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 module:provides("http", {
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 route = {
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 ["GET /*"] = get_avatar;
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 };
1cff081abbed mod_http_avatar: Add a module to serve vCard-temp avatars over HTTP.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 });