comparison sat_frontends/jp/cmd_file.py @ 3926:1877c5c477ec

cli (file/get): handle full JSON attachment following change in backend: rel 379
author Goffi <goffi@goffi.org>
date Thu, 06 Oct 2022 16:02:05 +0200
parents 04283582966f
children 4c3361e2bf55
comparison
equal deleted inserted replaced
3925:900bf04d87c8 3926:1877c5c477ec
36 import tempfile 36 import tempfile
37 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI 37 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
38 import json 38 import json
39 39
40 __commands__ = ["File"] 40 __commands__ = ["File"]
41 DEFAULT_DEST = "downloaded_file"
41 42
42 43
43 class Send(base.CommandBase): 44 class Send(base.CommandBase):
44 def __init__(self, host): 45 def __init__(self, host):
45 super(Send, self).__init__( 46 super(Send, self).__init__(
473 "-f", 474 "-f",
474 "--force", 475 "--force",
475 action="store_true", 476 action="store_true",
476 help=_("overwrite existing file without confirmation"), 477 help=_("overwrite existing file without confirmation"),
477 ) 478 )
478 self.parser.add_argument("uri", type=str, help=_("URI of the file to retrieve")) 479 self.parser.add_argument(
480 "attachment", type=str,
481 help=_("URI of the file to retrieve or JSON of the whole attachment")
482 )
479 483
480 async def onProgressStarted(self, metadata): 484 async def onProgressStarted(self, metadata):
481 self.disp(_("File download started"), 2) 485 self.disp(_("File download started"), 2)
482 486
483 async def onProgressFinished(self, metadata): 487 async def onProgressFinished(self, metadata):
497 else: 501 else:
498 self.disp(_("Can't download file"), error=True) 502 self.disp(_("Can't download file"), error=True)
499 self.host.quit(C.EXIT_ERROR) 503 self.host.quit(C.EXIT_ERROR)
500 504
501 async def start(self): 505 async def start(self):
502 uri = self.args.uri 506 try:
507 attachment = json.loads(self.args.attachment)
508 except json.JSONDecodeError:
509 attachment = {"uri": self.args.attachment}
503 dest_file = self.args.dest_file 510 dest_file = self.args.dest_file
504 if not dest_file: 511 if not dest_file:
505 parsed_uri = urlparse(uri) 512 try:
506 dest_file = Path(parsed_uri.path).name.strip() or "downloaded_file" 513 dest_file = attachment["name"].replace("/", "-").strip()
514 except KeyError:
515 try:
516 dest_file = Path(urlparse(attachment["uri"]).path).name.strip()
517 except KeyError:
518 pass
519 if not dest_file:
520 dest_file = "downloaded_file"
507 521
508 dest_file = Path(dest_file).expanduser().resolve() 522 dest_file = Path(dest_file).expanduser().resolve()
509 if dest_file.exists() and not self.args.force: 523 if dest_file.exists() and not self.args.force:
510 message = _("File {path} already exists! Do you want to overwrite?").format( 524 message = _("File {path} already exists! Do you want to overwrite?").format(
511 path=dest_file 525 path=dest_file
513 await self.host.confirmOrQuit(message, _("file download cancelled")) 527 await self.host.confirmOrQuit(message, _("file download cancelled"))
514 528
515 options = {} 529 options = {}
516 530
517 try: 531 try:
518 download_data = await self.host.bridge.fileDownload( 532 download_data_s = await self.host.bridge.fileDownload(
519 uri, 533 data_format.serialise(attachment),
520 str(dest_file), 534 str(dest_file),
521 data_format.serialise(options), 535 data_format.serialise(options),
522 self.profile, 536 self.profile,
523 ) 537 )
538 download_data = data_format.deserialise(download_data_s)
524 except Exception as e: 539 except Exception as e:
525 self.disp(f"error while trying to download a file: {e}", error=True) 540 self.disp(f"error while trying to download a file: {e}", error=True)
526 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 541 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
527 else: 542 else:
528 await self.gotId(download_data) 543 await self.gotId(download_data)