Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0054.py @ 3003:e624550d5c24
plugin XEP-0054: reject image without MIME type if it's not PNG
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 14 Jul 2019 16:43:04 +0200 |
parents | 003b8b4b56a7 |
children | ab2696e34d29 |
comparison
equal
deleted
inserted
replaced
3002:6acaa8244220 | 3003:e624550d5c24 |
---|---|
232 """Parse a <PHOTO> photo_elt and save the picture""" | 232 """Parse a <PHOTO> photo_elt and save the picture""" |
233 # XXX: this method is launched in a separate thread | 233 # XXX: this method is launched in a separate thread |
234 try: | 234 try: |
235 mime_type = unicode(photo_elt.elements(NS_VCARD, "TYPE").next()) | 235 mime_type = unicode(photo_elt.elements(NS_VCARD, "TYPE").next()) |
236 except StopIteration: | 236 except StopIteration: |
237 log.warning(u"no MIME type found, assuming image/png") | 237 mime_type = None |
238 mime_type = u"image/png" | |
239 else: | 238 else: |
240 if not mime_type: | 239 if not mime_type: |
241 log.warning(u"empty MIME type, assuming image/png") | 240 # MIME type not know, we'll only support PNG files |
242 mime_type = u"image/png" | 241 # TODO: autodetection using e.g. "magic" module |
242 # (https://pypi.org/project/python-magic/) | |
243 mime_type = None | |
243 elif mime_type not in ("image/gif", "image/jpeg", "image/png"): | 244 elif mime_type not in ("image/gif", "image/jpeg", "image/png"): |
244 if mime_type == "image/x-png": | 245 if mime_type == "image/x-png": |
245 # XXX: this old MIME type is still used by some clients | 246 # XXX: this old MIME type is still used by some clients |
246 mime_type = "image/png" | 247 mime_type = "image/png" |
247 else: | 248 else: |
268 log.warning(u"BINVAL element not found") | 269 log.warning(u"BINVAL element not found") |
269 raise Failure(exceptions.NotFound()) | 270 raise Failure(exceptions.NotFound()) |
270 if not buf: | 271 if not buf: |
271 log.warning(u"empty avatar for {jid}".format(jid=entity_jid.full())) | 272 log.warning(u"empty avatar for {jid}".format(jid=entity_jid.full())) |
272 raise Failure(exceptions.NotFound()) | 273 raise Failure(exceptions.NotFound()) |
274 if mime_type is None: | |
275 log.warning(_(u"no MIME type found for {entity}'s avatar, assuming image/png") | |
276 .format(entity=entity_jid.full())) | |
277 if buf[:8] != b'\x89\x50\x4e\x47\x0d\x0a\x1a\x0a': | |
278 log.warning(u"this is not a PNG file, ignoring it") | |
279 raise Failure(exceptions.DataError()) | |
280 else: | |
281 mime_type = u"image/png" | |
282 | |
273 log.debug(_(u"Decoding binary")) | 283 log.debug(_(u"Decoding binary")) |
274 decoded = b64decode(buf) | 284 decoded = b64decode(buf) |
275 del buf | 285 del buf |
276 image_hash = sha1(decoded).hexdigest() | 286 image_hash = sha1(decoded).hexdigest() |
277 with client.cache.cacheData( | 287 with client.cache.cacheData( |