comparison sat/plugins/plugin_xep_0329.py @ 3314:5887fb414758

component file sharing: add/parse affiliation when possible
author Goffi <goffi@goffi.org>
date Fri, 17 Jul 2020 13:00:10 +0200
parents 9d0df638c8b4
children bb92085720c8
comparison
equal deleted inserted replaced
3313:624c60293deb 3314:5887fb414758
415 name = os.path.basename(path) 415 name = os.path.basename(path)
416 if os.path.isfile(path): 416 if os.path.isfile(path):
417 size = os.path.getsize(path) 417 size = os.path.getsize(path)
418 mime_type = mimetypes.guess_type(path, strict=False)[0] 418 mime_type = mimetypes.guess_type(path, strict=False)[0]
419 file_elt = self._jf.buildFileElement( 419 file_elt = self._jf.buildFileElement(
420 name=name, size=size, mime_type=mime_type, modified=os.path.getmtime(path) 420 client=client, name=name, size=size, mime_type=mime_type,
421 modified=os.path.getmtime(path)
421 ) 422 )
422 423
423 query_elt.addChild(file_elt) 424 query_elt.addChild(file_elt)
424 # we don't specify hash as it would be too resource intensive to calculate 425 # we don't specify hash as it would be too resource intensive to calculate
425 # it for all files. 426 # it for all files.
588 return 589 return
589 iq_result_elt = xmlstream.toResponse(iq_elt, "result") 590 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
590 query_elt = iq_result_elt.addElement((NS_FIS, "query")) 591 query_elt = iq_result_elt.addElement((NS_FIS, "query"))
591 query_elt["node"] = node_path 592 query_elt["node"] = node_path
592 if not self.host.trigger.point( 593 if not self.host.trigger.point(
593 "XEP-0329_compGetFilesFromNode", client, iq_elt, owner, node_path, files_data 594 "XEP-0329_compGetFilesFromNode",
595 client,
596 iq_elt,
597 iq_result_elt,
598 owner,
599 node_path,
600 files_data
594 ): 601 ):
595 return 602 return
596 for file_data in files_data: 603 for file_data in files_data:
597 if file_data['type'] == C.FILE_TYPE_DIRECTORY: 604 if file_data['type'] == C.FILE_TYPE_DIRECTORY:
598 directory_elt = query_elt.addElement("directory") 605 directory_elt = query_elt.addElement("directory")
599 directory_elt['name'] = file_data['name'] 606 directory_elt['name'] = file_data['name']
607 self.host.trigger.point(
608 "XEP-0329_compGetFilesFromNode_build_directory",
609 client,
610 file_data,
611 directory_elt,
612 owner,
613 node_path,
614 )
600 else: 615 else:
601 file_elt = self._jf.buildFileElementFromDict( 616 file_elt = self._jf.buildFileElementFromDict(
602 file_data, modified=file_data.get("modified", file_data["created"]) 617 client,
618 file_data,
619 modified=file_data.get("modified", file_data["created"])
603 ) 620 )
604 query_elt.addChild(file_elt) 621 query_elt.addChild(file_elt)
605 client.send(iq_result_elt) 622 client.send(iq_result_elt)
606 623
607 def onComponentRequest(self, iq_elt, client): 624 def onComponentRequest(self, iq_elt, client):
608 return self._requestHandler( 625 return self._requestHandler(
609 client, iq_elt, self._compGetRootNodesCb, self._compGetFilesFromNodeCb 626 client, iq_elt, self._compGetRootNodesCb, self._compGetFilesFromNodeCb
610 ) 627 )
611 628
612 def _parseResult(self, iq_elt): 629 def _parseResult(self, iq_elt, client):
613 query_elt = next(iq_elt.elements(NS_FIS, "query")) 630 query_elt = next(iq_elt.elements(NS_FIS, "query"))
614 files = [] 631 files = []
615 632
616 for elt in query_elt.elements(): 633 for elt in query_elt.elements():
617 if elt.name == "file": 634 if elt.name == "file":
618 # we have a file 635 # we have a file
619 try: 636 try:
620 file_data = self._jf.parseFileElement(elt) 637 file_data = self._jf.parseFileElement(client, elt)
621 except exceptions.DataError: 638 except exceptions.DataError:
622 continue 639 continue
623 file_data["type"] = C.FILE_TYPE_FILE 640 file_data["type"] = C.FILE_TYPE_FILE
624 elif elt.name == "directory" and elt.uri == NS_FIS: 641 elif elt.name == "directory" and elt.uri == NS_FIS:
625 # we have a directory 642 # we have a directory
626 643
627 file_data = {"name": elt["name"], "type": C.FILE_TYPE_DIRECTORY} 644 file_data = {"name": elt["name"], "type": C.FILE_TYPE_DIRECTORY}
645 self.host.trigger.point(
646 "XEP-0329_parseResult_directory",
647 client,
648 elt,
649 file_data,
650 )
628 else: 651 else:
629 log.warning( 652 log.warning(
630 _(f"unexpected element, ignoring: {elt.toXml()}") 653 _(f"unexpected element, ignoring: {elt.toXml()}")
631 ) 654 )
632 continue 655 continue
663 iq_elt["to"] = target_jid.full() 686 iq_elt["to"] = target_jid.full()
664 query_elt = iq_elt.addElement((NS_FIS, "query")) 687 query_elt = iq_elt.addElement((NS_FIS, "query"))
665 if path: 688 if path:
666 query_elt["node"] = path 689 query_elt["node"] = path
667 d = iq_elt.send() 690 d = iq_elt.send()
668 d.addCallback(self._parseResult) 691 d.addCallback(self._parseResult, client)
669 return d 692 return d
670 693
671 def _localSharesGet(self, profile): 694 def _localSharesGet(self, profile):
672 client = self.host.getClient(profile) 695 client = self.host.getClient(profile)
673 return self.localSharesGet(client) 696 return self.localSharesGet(client)