From cb0c3134044aff00c79e5150164266a3c72a5268 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 6 Sep 2018 00:32:11 +0200 Subject: [PATCH] Add qute://pdfjs/file to get files Cross-origin requests aren't supported for qute:// URLs, and qute:// can't access file://, so we need to get the files that way. --- qutebrowser/browser/pdfjs.py | 8 +++++++- qutebrowser/browser/qutescheme.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/pdfjs.py b/qutebrowser/browser/pdfjs.py index 60e51e6cd..a51490d0a 100644 --- a/qutebrowser/browser/pdfjs.py +++ b/qutebrowser/browser/pdfjs.py @@ -155,7 +155,13 @@ def should_use_pdfjs(mimetype): def get_main_url(filename): """Get the URL to be opened to view a local PDF.""" url = QUrl('qute://pdfjs/web/viewer.html') + + file_url = QUrl('qute://pdfjs/file') + file_url_query = QUrlQuery() + file_url_query.addQueryItem('filename', filename) + file_url.setQuery(file_url_query) + query = QUrlQuery() - query.addQueryItem('file', QUrl.fromLocalFile(filename).toString()) + query.addQueryItem('file', file_url.toString()) url.setQuery(query) return url diff --git a/qutebrowser/browser/qutescheme.py b/qutebrowser/browser/qutescheme.py index ec198bc13..00c5d44bd 100644 --- a/qutebrowser/browser/qutescheme.py +++ b/qutebrowser/browser/qutescheme.py @@ -523,6 +523,16 @@ def qute_pastebin_version(_url): @add_handler('pdfjs') def qute_pdfjs(url): """Handler for qute://pdfjs. Return the pdf.js viewer.""" + # FIXME be more strict about allowed files here + if url.path() == '/file': + filename = QUrlQuery(url).queryItemValue('filename') + with open(filename, 'rb') as f: + data = f.read() + mimetype, _encoding = mimetypes.guess_type(filename) + if mimetype is None: + mimetype = 'application/octet-stream' + return mimetype, data + try: data = pdfjs.get_pdfjs_res(url.path()) except pdfjs.PDFJSNotFound as e: