changeset 420:83d184393fe1

core: downloadURL helper method
author Goffi <goffi@goffi.org>
date Wed, 26 Feb 2020 16:38:17 +0100
parents b047d14e4be5
children ddca5a837029
files cagou/core/cagou_main.py cagou/core/constants.py
diffstat 2 files changed, 45 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/cagou_main.py	Wed Feb 26 16:32:27 2020 +0100
+++ b/cagou/core/cagou_main.py	Wed Feb 26 16:38:17 2020 +0100
@@ -21,6 +21,7 @@
 import glob
 import sys
 from pathlib import Path
+from urllib import parse as urlparse
 from functools import partial
 from sat.core.i18n import _
 from . import kivy_hack
@@ -37,7 +38,9 @@
 from sat_frontends.tools import jid
 from sat.tools import utils as sat_utils
 from sat.tools import config
+from sat.tools.common import data_format
 from sat.tools.common import dynamic_import
+from sat.tools.common import files_utils
 import kivy
 kivy.require('1.11.0')
 import kivy.support
@@ -1049,6 +1052,44 @@
             log.error(e)
             self.closeUI()
 
+    def downloadURL(
+        self, url, callback, errback=None, options=None, dest=C.FILE_DEST_DOWNLOAD,
+        profile=C.PROF_KEY_NONE):
+        """Download an URL (decrypt it if necessary)
+
+        @param url(str, parse.SplitResult): url to download
+        @param callback(callable): method to call when download is complete
+        @param errback(callable, None): method to call in case of error
+            if None, default errback will be called
+        @param dest(str): where the file should be downloaded:
+            - C.FILE_DEST_DOWNLOAD: in platform download directory
+            - C.FILE_DEST_CACHE: in SàT cache
+        @param options(dict, None): options to pass to bridge.fileDownloadComplete
+        """
+        if not isinstance(url, urlparse.ParseResult):
+            url = urlparse.urlparse(url)
+        if errback is None:
+            errback = partial(
+                self.errback,
+                title=_("Download error"),
+                message=_("Error while downloading {url}: {{msg}}").format(url=url.geturl()))
+        name = Path(url.path).name.strip() or C.FILE_DEFAULT_NAME
+        log.info(f"downloading/decrypting file {name!r}")
+        if dest == C.FILE_DEST_DOWNLOAD:
+            dest_path = files_utils.get_unique_name(Path(self.downloads_dir)/name)
+        elif dest == C.FILE_DEST_CACHE:
+            dest_path = ''
+        else:
+            raise exceptions.InternalError(f"Invalid dest_path: {dest_path!r}")
+        self.bridge.fileDownloadComplete(
+            url.geturl(),
+            str(dest_path),
+            '' if not options else data_format.serialise(options),
+            profile,
+            callback=callback,
+            errback=errback
+        )
+
     def notify(self, type_, entity=None, message=None, subject=None, callback=None,
                cb_args=None, widget=None, profile=C.PROF_KEY_NONE):
         super().notify(
--- a/cagou/core/constants.py	Wed Feb 26 16:32:27 2020 +0100
+++ b/cagou/core/constants.py	Wed Feb 26 16:38:17 2020 +0100
@@ -55,3 +55,7 @@
     # values are in dp
     IMG_MAX_WIDTH = 500
     IMG_MAX_HEIGHT = 500
+
+    # files
+    FILE_DEST_DOWNLOAD = "DOWNLOAD"
+    FILE_DEST_CACHE = "CACHE"