Mercurial > xmpp-account-manager
comparison avatar.js @ 0:2a8d4e8600d0
Initial commit.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 21 Dec 2018 21:34:17 +0100 |
parents | |
children | d6df73b466f6 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2a8d4e8600d0 |
---|---|
1 'use strict'; | |
2 | |
3 function initAvatar(connection) { | |
4 const DEFAULT_AVATAR = 'data:image/svg+xml,<?xml version="1.0"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 150 150"><rect width="150" height="150" fill="#888" stroke-width="1" stroke="#000"/><text x="75" y="100" text-anchor="middle" font-size="100">?</text></svg>'; | |
5 | |
6 const avatar_data = {}; | |
7 const avatar_img = document.getElementById('avatar'); | |
8 const avatar_size = document.getElementById('avatar-size'); | |
9 const avatar_file = document.getElementById('avatar-file'); | |
10 const avatar_upload = document.getElementById('avatar-upload'); | |
11 const avatar_change = document.getElementById('avatar-change'); | |
12 | |
13 avatar_img.src = DEFAULT_AVATAR; | |
14 const iq = $iq({type: 'get'}) | |
15 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
16 .c('items', {node: 'urn:xmpp:avatar:metadata'}); | |
17 connection.sendIQ(iq, onAvatarMetadata, onAvatarRetrievalError.bind(null, 'PubSub metadata query failed.')); | |
18 | |
19 function onAvatarMetadata(result_iq) | |
20 { | |
21 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item'); | |
22 if (item == null) | |
23 return onAvatarRetrievalError('no item found.'); | |
24 const id = item.getAttributeNS(null, 'id'); | |
25 const info = parseXPath(item, './avatar_metadata:metadata/avatar_metadata:info'); | |
26 if (info == null) | |
27 return onAvatarRetrievalError('no info found, your avatar metadata node is broken.'); | |
28 if (id != info.getAttributeNS(null, 'id')) | |
29 return onAvatarRetrievalError('invalid id in metadata.'); | |
30 | |
31 const parsed_info = { | |
32 id: id, | |
33 type: info.getAttributeNS(null, 'type'), | |
34 bytes: info.getAttributeNS(null, 'bytes'), | |
35 width: info.getAttributeNS(null, 'width'), | |
36 height: info.getAttributeNS(null, 'height'), | |
37 }; | |
38 const iq = $iq({type: 'get'}) | |
39 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
40 .c('items', {node: 'urn:xmpp:avatar:data'}) | |
41 .c('item', {id: id}); | |
42 connection.sendIQ(iq, onAvatarData.bind(null, parsed_info), onAvatarRetrievalError.bind(null, 'PubSub data query failed.')); | |
43 } | |
44 | |
45 function onAvatarData(info, result_iq) | |
46 { | |
47 const item = parseXPath(result_iq, './pubsub:pubsub/pubsub:items/pubsub:item'); | |
48 if (item == null) | |
49 return onAvatarRetrievalError('no item found.'); | |
50 if (info.id != item.getAttributeNS(null, 'id')) | |
51 return onAvatarRetrievalError('invalid id in data.'); | |
52 | |
53 const data = parseXPath(item, './avatar_data:data').textContent; | |
54 const url = 'data:' + info.type + ';base64,' + data; | |
55 // TODO: validate the bytes too. | |
56 /* | |
57 // TODO: figure out why this didn’t work. | |
58 avatar_img.onload = function (evt) { | |
59 const img = evt.target; | |
60 if (img.naturalWidth != info.width || img.naturalHeight != info.height) | |
61 return onAvatarRetrievalError('invalid width or height in image data.'); | |
62 avatar_img.onload = null; | |
63 }; | |
64 */ | |
65 avatar_img.src = url; | |
66 } | |
67 | |
68 function onAvatarRetrievalError(string) | |
69 { | |
70 console.log('Failed to retrieve avatar, an empty one is displayed instead: ' + string); | |
71 avatar_img.src = DEFAULT_AVATAR; | |
72 } | |
73 | |
74 avatar_upload.addEventListener('click', function (evt) { | |
75 avatar_file.click(); | |
76 }); | |
77 | |
78 avatar_change.addEventListener('click', function (evt) { | |
79 const metadata_iq = $iq({type: 'set'}) | |
80 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
81 .c('publish', {node: 'urn:xmpp:avatar:metadata'}) | |
82 .c('item', {id: avatar_data.id}) | |
83 .c('metadata', {xmlns: 'urn:xmpp:avatar:metadata'}) | |
84 .c('info', { | |
85 id: avatar_data.id, | |
86 type: avatar_data.type, | |
87 bytes: avatar_data.bytes, | |
88 width: avatar_img.naturalWidth, | |
89 height: avatar_img.naturalHeight, | |
90 }); | |
91 connection.sendIQ(metadata_iq, onAvatarMetadataUpload, onAvatarUploadError); | |
92 const data_iq = $iq({type: 'set'}) | |
93 .c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
94 .c('publish', {node: 'urn:xmpp:avatar:data'}) | |
95 .c('item', {id: avatar_data.id}) | |
96 .c('data', {xmlns: 'urn:xmpp:avatar:data'}) | |
97 .t(avatar_data.data); | |
98 connection.sendIQ(data_iq, onAvatarDataUpload, onAvatarUploadError); | |
99 }); | |
100 | |
101 function onAvatarMetadataUpload(iq) | |
102 { | |
103 console.log("onAvatarMetadataUpload", iq); | |
104 } | |
105 | |
106 function onAvatarDataUpload(iq) | |
107 { | |
108 console.log('Avatar successfully uploaded!', iq); | |
109 avatar_change.disabled = true; | |
110 avatar_size.innerHTML = ''; | |
111 } | |
112 | |
113 function onAvatarUploadError(iq) | |
114 { | |
115 console.log("onAvatarUploadError", iq); | |
116 } | |
117 | |
118 avatar_file.addEventListener('change', function (evt) { | |
119 const file = evt.target.files[0]; | |
120 avatar_data.type = file.type; | |
121 avatar_data.bytes = file.size; | |
122 | |
123 // Set the preview. | |
124 avatar_img.src = URL.createObjectURL(file); | |
125 const [size, unit] = friendlyDataSize(file.size); | |
126 avatar_size.innerHTML = Math.round(size) + ' ' + unit; | |
127 | |
128 // Obtain the base64 version of this file. | |
129 const base64_reader = new FileReader(); | |
130 base64_reader.onload = function (evt) { | |
131 const data = evt.target.result; | |
132 avatar_data.data = data.substr(data.indexOf(',') + 1); | |
133 } | |
134 base64_reader.readAsDataURL(file); | |
135 | |
136 // Compute the sha1 of this file. | |
137 const sha1_reader = new FileReader(); | |
138 sha1_reader.onload = async function (evt) { | |
139 const data = evt.target.result; | |
140 const digest = await window.crypto.subtle.digest('SHA-1', data); | |
141 const sha1 = (Array | |
142 .from(new Uint8Array(digest)) | |
143 .map(b => b.toString(16).padStart(2, "0")) | |
144 .join("")); | |
145 avatar_data.id = sha1; | |
146 avatar_change.disabled = false; | |
147 } | |
148 sha1_reader.readAsArrayBuffer(file); | |
149 }); | |
150 | |
151 function nsResolver(prefix) { | |
152 return { | |
153 pubsub: 'http://jabber.org/protocol/pubsub', | |
154 avatar_metadata: 'urn:xmpp:avatar:metadata', | |
155 avatar_data: 'urn:xmpp:avatar:data', | |
156 }[prefix] || null; | |
157 } | |
158 | |
159 function parseXPath(elem, xpath) | |
160 { | |
161 return elem.getRootNode().evaluate(xpath, elem, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue; | |
162 } | |
163 | |
164 function friendlyDataSize(bytes) { | |
165 let unit = 'B' | |
166 if (bytes >= 1024) { | |
167 bytes /= 1024; | |
168 unit = 'KiB'; | |
169 } | |
170 if (bytes >= 1024) { | |
171 bytes /= 1024; | |
172 unit = 'MiB'; | |
173 } | |
174 if (bytes >= 1024) { | |
175 bytes /= 1024; | |
176 unit = 'GiB'; | |
177 } | |
178 if (bytes >= 1024) { | |
179 bytes /= 1024; | |
180 unit = 'TiB'; | |
181 } | |
182 return [bytes, unit]; | |
183 } | |
184 } |