cli (file/get): handle full JSON attachment following change in backend:

rel 379
This commit is contained in:
Goffi 2022-10-06 16:02:05 +02:00
commit cb44cb1991

View file

@ -38,6 +38,7 @@ import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI
import json
__commands__ = ["File"]
DEFAULT_DEST = "downloaded_file"
class Send(base.CommandBase):
@ -475,7 +476,10 @@ class Get(base.CommandBase):
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 @@ class Get(base.CommandBase):
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 @@ class Get(base.CommandBase):
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)