comparison sat/plugins/plugin_misc_identity.py @ 3326:9e1ba1e1179f

plugin identity: added "filename" metadata for avatar
author Goffi <goffi@goffi.org>
date Sun, 02 Aug 2020 17:40:07 +0200
parents 27d4b71e264a
children 203a491fcd86
comparison
equal deleted inserted replaced
3325:7ebda4b54170 3326:9e1ba1e1179f
143 if value is not None: 143 if value is not None:
144 try: 144 try:
145 cache_uid = value['cache_uid'] 145 cache_uid = value['cache_uid']
146 if not cache_uid: 146 if not cache_uid:
147 raise ValueError 147 raise ValueError
148 filename = value['filename']
149 if not filename:
150 raise ValueError
148 except (ValueError, KeyError): 151 except (ValueError, KeyError):
149 log.warning( 152 log.warning(
150 f"invalid data for {entity} avatar, it will be deleted: " 153 f"invalid data for {entity} avatar, it will be deleted: "
151 f"{value}") 154 f"{value}")
152 to_delete.append(key) 155 to_delete.append(key)
462 file_path = Path(file_path) 465 file_path = Path(file_path)
463 if not file_path.is_file(): 466 if not file_path.is_file():
464 raise ValueError(f"There is no file at {file_path} to use as avatar") 467 raise ValueError(f"There is no file at {file_path} to use as avatar")
465 avatar_data = { 468 avatar_data = {
466 'path': file_path, 469 'path': file_path,
470 'filename': file_path.name,
467 'media_type': image.guess_type(file_path), 471 'media_type': image.guess_type(file_path),
468 } 472 }
469 media_type = avatar_data['media_type'] 473 media_type = avatar_data['media_type']
470 if media_type is None: 474 if media_type is None:
471 raise ValueError(f"Can't identify type of image at {file_path}") 475 raise ValueError(f"Can't identify type of image at {file_path}")
497 if media_type is None: 501 if media_type is None:
498 media_type = image.guess_type(path) 502 media_type = image.guess_type(path)
499 503
500 return { 504 return {
501 "path": path, 505 "path": path,
506 "filename": path.name,
502 "media_type": media_type, 507 "media_type": media_type,
503 "cache_uid": cache_uid, 508 "cache_uid": cache_uid,
504 } 509 }
505 510
506 def avatarUpdateIsNewData(self, client, entity, cached_data, new_data): 511 def avatarUpdateIsNewData(self, client, entity, cached_data, new_data):
507 return new_data['path'] != cached_data['path'] 512 return new_data['path'] != cached_data['path']
508 513
509 async def avatarUpdateDataFilter(self, client, entity, data): 514 async def avatarUpdateDataFilter(self, client, entity, data):
510 if not isinstance(data, dict): 515 if not isinstance(data, dict):
511 raise ValueError(f"Invalid data type ({type(data)}), a dict is expected") 516 raise ValueError(f"Invalid data type ({type(data)}), a dict is expected")
512 mandatory_keys = {'path', 'cache_uid'} 517 mandatory_keys = {'path', 'filename', 'cache_uid'}
513 if not data.keys() >= mandatory_keys: 518 if not data.keys() >= mandatory_keys:
514 raise ValueError(f"missing avatar data keys: {mandatory_keys - data.keys()}") 519 raise ValueError(f"missing avatar data keys: {mandatory_keys - data.keys()}")
515 return data 520 return data
516 521
517 async def nicknamesGetPostTreatment(self, client, entity, plugin_nicknames): 522 async def nicknamesGetPostTreatment(self, client, entity, plugin_nicknames):