Mercurial > prosody-modules
comparison 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 |
| parents | |
| children | 46d71b38bc16 |
comparison
equal
deleted
inserted
replaced
| 3081:e0ef90e96931 | 3082:1cff081abbed |
|---|---|
| 1 -- Prosody IM | |
| 2 -- Copyright (C) 2018 Emmanuel Gil Peyrot | |
| 3 -- | |
| 4 -- This project is MIT/X11 licensed. Please see the | |
| 5 -- COPYING file in the source package for more information. | |
| 6 | |
| 7 local base64 = require"util.encodings".base64; | |
| 8 local st = require"util.stanza"; | |
| 9 module:depends"http"; | |
| 10 | |
| 11 local vcard_storage = module:open_store"vcard"; | |
| 12 | |
| 13 local default_avatar = [[<svg xmlns='http://www.w3.org/2000/svg' version='1.1' viewBox='0 0 150 150'> | |
| 14 <rect width='150' height='150' fill='#888' stroke-width='1' stroke='#000'/> | |
| 15 <text x='75' y='100' text-anchor='middle' font-size='100'>?</text> | |
| 16 </svg>]]; | |
| 17 | |
| 18 local function get_avatar(event, path) | |
| 19 local request, response = event.request, event.response; | |
| 20 local photo_type, binval; | |
| 21 local vcard, err = vcard_storage:get(path); | |
| 22 if vcard then | |
| 23 vcard = st.deserialize(vcard); | |
| 24 local photo = vcard:get_child("PHOTO", "vcard-temp"); | |
| 25 if photo then | |
| 26 photo_type = photo:get_child_text("TYPE", "vcard-temp"); | |
| 27 binval = photo:get_child_text("BINVAL", "vcard-temp"); | |
| 28 end | |
| 29 end | |
| 30 if not photo_type or not binval then | |
| 31 response.status_code = 404; | |
| 32 response.headers.content_type = "image/svg+xml"; | |
| 33 return default_avatar; | |
| 34 end | |
| 35 response.headers.content_type = photo_type; | |
| 36 return base64.decode(binval); | |
| 37 end | |
| 38 | |
| 39 module:provides("http", { | |
| 40 route = { | |
| 41 ["GET /*"] = get_avatar; | |
| 42 }; | |
| 43 }); |
