def on_unsupported_content(self, reply): """Handle an unsupportedContent signal. Most likely this will mean we need to download the reply, but we correct for some common errors the server do. At some point we might want to implement the MIME Sniffing standard here: https://mimesniff.spec.whatwg.org/ """ inline, suggested_filename = http.parse_content_disposition(reply) download_manager = objreg.get('qtnetwork-download-manager') if not inline: # Content-Disposition: attachment -> force download download_manager.fetch(reply, suggested_filename=suggested_filename) return mimetype, _rest = http.parse_content_type(reply) if mimetype == 'image/jpg': # Some servers (e.g. the LinkedIn CDN) send a non-standard # image/jpg (instead of image/jpeg, defined in RFC 1341 section # 7.5). If this is the case, we force displaying with a corrected # mimetype. if reply.isFinished(): self.display_content(reply, 'image/jpeg') else: reply.finished.connect(functools.partial( self.display_content, reply, 'image/jpeg')) elif pdfjs.should_use_pdfjs(mimetype, reply.url()): download_manager.fetch(reply, target=downloads.PDFJSDownloadTarget(), auto_remove=True) else: # Unknown mimetype, so download anyways. download_manager.fetch(reply, suggested_filename=suggested_filename)
def handle_download(self, qt_item): """Start a download coming from a QWebEngineProfile.""" suggested_filename = _get_suggested_filename(qt_item.path()) use_pdfjs = pdfjs.should_use_pdfjs(qt_item.mimeType(), qt_item.url()) download = DownloadItem(qt_item) 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 # 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)
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() # 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 download.cancel_for_origin(): 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)
def download_open(self, cmdline, pdfjs): if pdfjs: target: 'downloads._DownloadTarget' = downloads.PDFJSDownloadTarget() else: target = downloads.OpenFileDownloadTarget(cmdline) self.question.answer = target self.question.done() message.global_bridge.prompt_done.emit(self.KEY_MODE)
def handle_download(self, qt_item): """Start a download coming from a QWebEngineProfile.""" if qt_item.url().scheme().lower() == 'data': # WORKAROUND for an unknown QtWebEngine bug (?) which gives us base64 data # as filename. suggested_filename = urlutils.filename_from_url( qt_item.url(), fallback='qutebrowser-download') else: suggested_filename = _get_suggested_filename(qt_item.path()) use_pdfjs = pdfjs.should_use_pdfjs(qt_item.mimeType(), qt_item.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 # 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)