diff cagou/core/simple_xhtml.py @ 335:597cc207c8e7

core (simple_xhtml): handle `aesgcm` schemes: when an `aesgcm` scheme is found, the file is download/decrypted using fileDownload, then opened.
author Goffi <goffi@goffi.org>
date Fri, 20 Dec 2019 12:29:37 +0100
parents 5868a5575e01
children 83697218b9b2
line wrap: on
line diff
--- a/cagou/core/simple_xhtml.py	Fri Dec 20 12:29:37 2019 +0100
+++ b/cagou/core/simple_xhtml.py	Fri Dec 20 12:29:37 2019 +0100
@@ -20,13 +20,18 @@
 
 import webbrowser
 from xml.etree import ElementTree as ET
+from urllib.parse import urlparse
+from pathlib import Path
 from kivy.uix.stacklayout import StackLayout
 from kivy.uix.label import Label
 from kivy.utils import escape_markup
 from kivy.metrics import sp, dp
 from kivy import properties
 from sat.core import log as logging
+from sat.tools.common import files_utils
 from sat_frontends.tools import css_color, strings as sat_strings
+from sat_frontends.quick_frontend.quick_widgets import QuickWidget
+from cagou import G
 from cagou.core.image import AsyncImage
 from cagou.core.constants import Const as C
 
@@ -79,7 +84,28 @@
 
     def on_ref_press(self, ref):
         url = self.ref_urls[ref]
-        webbrowser.open(url)
+        parsed_url = urlparse(url)
+        if parsed_url.scheme == "aesgcm":
+            # aesgcm files need to be decrypted first
+            # so we download them before opening
+            name = Path(parsed_url.path).name.strip() or "unnamed_file"
+            log.info(f"downloading/decrypting file {name!r}")
+            dest_path = files_utils.get_unique_name(Path(G.host.downloads_dir)/name)
+            quick_widget = G.host.getAncestorWidget(self, QuickWidget)
+            if quick_widget is None:
+                log.error("Can't find ancestor QuickWidget")
+                return
+            profile = quick_widget.profile
+            G.host.bridge.fileDownloadComplete(
+                url,
+                str(dest_path),
+                '',
+                profile,
+                callback=webbrowser.open,
+                errback=G.host.errback
+            )
+        else:
+            webbrowser.open(url)
 
 
 class SimpleXHTMLWidgetText(Label):