def _finish_file(self): """Save the file to the filename given in __init__.""" if self._finished_file: log.downloads.debug("finish_file called twice, ignored!") return self._finished_file = True log.downloads.debug("All assets downloaded, ready to finish off!") if isinstance(self.target, downloads.FileDownloadTarget): fobj = open(self.target.filename, 'wb') elif isinstance(self.target, downloads.FileObjDownloadTarget): fobj = self.target.fileobj elif isinstance(self.target, downloads.OpenFileDownloadTarget): try: fobj = downloads.temp_download_manager.get_tmpfile( self.tab.title() + '.mhtml') except OSError as exc: msg = "Download error: {}".format(exc) message.error(msg) return else: raise ValueError("Invalid DownloadTarget given: {!r}" .format(self.target)) try: with fobj: self.writer.write_to(fobj) except OSError as error: message.error("Could not save file: {}".format(error)) return log.downloads.debug("File successfully written.") message.info("Page saved as {}".format(self.target)) if isinstance(self.target, downloads.OpenFileDownloadTarget): utils.open_file(fobj.name, self.target.cmdline)
def _finish_file(self): """Save the file to the filename given in __init__.""" if self._finished_file: log.downloads.debug("finish_file called twice, ignored!") return self._finished_file = True log.downloads.debug("All assets downloaded, ready to finish off!") if isinstance(self.target, downloads.FileDownloadTarget): fobj = open(self.target.filename, 'wb') elif isinstance(self.target, downloads.FileObjDownloadTarget): fobj = self.target.fileobj elif isinstance(self.target, downloads.OpenFileDownloadTarget): try: fobj = downloads.temp_download_manager.get_tmpfile( self.tab.title() + '.mhtml') except OSError as exc: msg = "Download error: {}".format(exc) message.error(msg) return else: raise ValueError("Invalid DownloadTarget given: {!r}".format( self.target)) try: with fobj: self.writer.write_to(fobj) except OSError as error: message.error("Could not save file: {}".format(error)) return log.downloads.debug("File successfully written.") message.info("Page saved as {}".format(self.target)) if isinstance(self.target, downloads.OpenFileDownloadTarget): utils.open_file(fobj.name, self.target.cmdline)
def cancel_for_origin(self) -> bool: """Cancel the download based on URL/origin. For some special cases, we want to cancel downloads immediately, before downloading: - file:// downloads from file:// URLs (open the file instead) - http:// downloads from https:// URLs (mixed content) """ origin = self.origin() url = self.url() if not origin.isValid(): return False if url.scheme() == "file" and origin.scheme() == "file": utils.open_file(url.toLocalFile()) self.cancel() return True if (url.scheme() == "http" and origin.isValid() and origin.scheme() == "https" and config.instance.get( "downloads.prevent_mixed_content", url=origin)): self._die("Aborting insecure download from secure page " "(see downloads.prevent_mixed_content).") return True return False
def open_file(self, cmdline=None): """Open the downloaded file. Args: cmdline: The command to use as string. A `{}` is expanded to the filename. None means to use the system's default application or `default-open-dispatcher` if set. If no `{}` is found, the filename is appended to the cmdline. """ assert self.successful filename = self._get_open_filename() if filename is None: # pragma: no cover log.downloads.error("No filename to open the download!") return utils.open_file(filename, cmdline)
def open_file(self, cmdline=None): """Open the downloaded file. Args: cmdline: The command to use as string. A `{}` is expanded to the filename. None means to use the system's default application or `default-open-dispatcher` if set. If no `{}` is found, the filename is appended to the cmdline. """ assert self.successful filename = self._get_open_filename() if filename is None: # pragma: no cover log.downloads.error("No filename to open the download!") return # By using a singleshot timer, we ensure that we return fast. This # is important on systems where process creation takes long, as # otherwise the prompt might hang around and cause bugs # (see issue #2296) QTimer.singleShot(0, lambda: utils.open_file(filename, cmdline))
def open_file(self, cmdline=None): """Open the downloaded file. Args: cmdline: The command to use as string. A `{}` is expanded to the filename. None means to use the system's default application or `downloads.open_dispatcher` if set. If no `{}` is found, the filename is appended to the cmdline. """ assert self.successful filename = self._get_open_filename() if filename is None: # pragma: no cover log.downloads.error("No filename to open the download!") return # By using a singleshot timer, we ensure that we return fast. This # is important on systems where process creation takes long, as # otherwise the prompt might hang around and cause bugs # (see issue #2296) QTimer.singleShot(0, lambda: utils.open_file(filename, cmdline))
def handle_download(self, qt_item): """Start a download coming from a QWebEngineProfile.""" qt_filename = os.path.basename(qt_item.path()) # FIXME use 5.14 API mime_type = qt_item.mimeType() url = qt_item.url() origin = qt_item.page().url() if qt_item.page() else QUrl() # WORKAROUND for https://bugreports.qt.io/browse/QTBUG-90355 if version.qtwebengine_versions().webengine >= utils.VersionNumber( 5, 15, 3): needs_workaround = False elif url.scheme().lower() == 'data': if '/' in url.path().split(',')[-1]: # e.g. a slash in base64 wrong_filename = url.path().split('/')[-1] else: wrong_filename = mime_type.split('/')[1] needs_workaround = qt_filename == wrong_filename else: needs_workaround = False if needs_workaround: suggested_filename = urlutils.filename_from_url( url, fallback='qutebrowser-download') else: suggested_filename = _strip_suffix(qt_filename) use_pdfjs = pdfjs.should_use_pdfjs(mime_type, url) download = DownloadItem(qt_item, manager=self) self._init_item(download, auto_remove=use_pdfjs, suggested_filename=suggested_filename) if self._mhtml_target is not None: download.set_target(self._mhtml_target) self._mhtml_target = None return if use_pdfjs: download.set_target(downloads.PDFJSDownloadTarget()) return filename = downloads.immediate_download_path() if filename is not None: # User doesn't want to be asked, so just use the download_dir target = downloads.FileDownloadTarget(filename) download.set_target(target) return if url.scheme() == "file" and origin.isValid() and origin.scheme( ) == "file": utils.open_file(url.toLocalFile()) qt_item.cancel() return # Ask the user for a filename - needs to be blocking! question = downloads.get_filename_question( suggested_filename=suggested_filename, url=qt_item.url(), parent=self) self._init_filename_question(question, download) message.global_bridge.ask(question, blocking=True)