changeset 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 900bf04d87c8
children 328869756cf4
files sat_frontends/jp/cmd_file.py
diffstat 1 files changed, 21 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/sat_frontends/jp/cmd_file.py	Thu Oct 06 16:02:05 2022 +0200
+++ b/sat_frontends/jp/cmd_file.py	Thu Oct 06 16:02:05 2022 +0200
@@ -38,6 +38,7 @@
 import json
 
 __commands__ = ["File"]
+DEFAULT_DEST = "downloaded_file"
 
 
 class Send(base.CommandBase):
@@ -475,7 +476,10 @@
             action="store_true",
             help=_("overwrite existing file without confirmation"),
         )
-        self.parser.add_argument("uri", type=str, help=_("URI of the file to retrieve"))
+        self.parser.add_argument(
+            "attachment", type=str,
+            help=_("URI of the file to retrieve or JSON of the whole attachment")
+        )
 
     async def onProgressStarted(self, metadata):
         self.disp(_("File download started"), 2)
@@ -499,11 +503,21 @@
             self.host.quit(C.EXIT_ERROR)
 
     async def start(self):
-        uri = self.args.uri
+        try:
+            attachment = json.loads(self.args.attachment)
+        except json.JSONDecodeError:
+            attachment = {"uri": self.args.attachment}
         dest_file = self.args.dest_file
         if not dest_file:
-            parsed_uri = urlparse(uri)
-            dest_file = Path(parsed_uri.path).name.strip() or "downloaded_file"
+            try:
+                dest_file = attachment["name"].replace("/", "-").strip()
+            except KeyError:
+                try:
+                    dest_file = Path(urlparse(attachment["uri"]).path).name.strip()
+                except KeyError:
+                    pass
+            if not dest_file:
+                dest_file = "downloaded_file"
 
         dest_file = Path(dest_file).expanduser().resolve()
         if dest_file.exists() and not self.args.force:
@@ -515,12 +529,13 @@
         options = {}
 
         try:
-            download_data = await self.host.bridge.fileDownload(
-                uri,
+            download_data_s = await self.host.bridge.fileDownload(
+                data_format.serialise(attachment),
                 str(dest_file),
                 data_format.serialise(options),
                 self.profile,
             )
+            download_data = data_format.deserialise(download_data_s)
         except Exception as e:
             self.disp(f"error while trying to download a file: {e}", error=True)
             self.host.quit(C.EXIT_BRIDGE_ERRBACK)