def qute_pdfjs(url: QUrl) -> _HandlerRet: """Handler for qute://pdfjs. Return the pdf.js viewer or redirect to original URL if the file does not exist. """ if url.path() == '/file': filename = QUrlQuery(url).queryItemValue('filename') if not filename: raise UrlInvalidError("Missing filename") if '/' in filename or os.sep in filename: raise RequestDeniedError("Path separator in filename.") path = _pdf_path(filename) with open(path, 'rb') as f: data = f.read() mimetype = utils.guess_mimetype(filename, fallback=True) return mimetype, data if url.path() == '/web/viewer.html': query = QUrlQuery(url) filename = query.queryItemValue("filename") if not filename: raise UrlInvalidError("Missing filename") path = _pdf_path(filename) if not os.path.isfile(path): source = query.queryItemValue('source') if not source: # This may happen with old URLs stored in history raise UrlInvalidError("Missing source") raise Redirect(QUrl(source)) data = pdfjs.generate_pdfjs_page(filename, url) return 'text/html', data try: data = pdfjs.get_pdfjs_res(url.path()) except pdfjs.PDFJSNotFound as e: # Logging as the error might get lost otherwise since we're not showing # the error page if a single asset is missing. This way we don't lose # information, as the failed pdfjs requests are still in the log. log.misc.warning( "pdfjs resource requested but not found: {}".format(e.path)) raise NotFoundError("Can't find pdfjs resource '{}'".format(e.path)) else: mimetype = utils.guess_mimetype(url.fileName(), fallback=True) return mimetype, data
def qute_resource(url: QUrl) -> _HandlerRet: """Handler for qute://resource.""" path = url.path().lstrip('/') mimetype = utils.guess_mimetype(path, fallback=True) try: data = resources.read_file_binary(path) except FileNotFoundError as e: raise NotFoundError(str(e)) return mimetype, data
def qute_help(url): """Handler for qute://help.""" urlpath = url.path() if not urlpath or urlpath == '/': urlpath = 'index.html' else: urlpath = urlpath.lstrip('/') if not docutils.docs_up_to_date(urlpath): message.error("Your documentation is outdated! Please re-run " "scripts/asciidoc2html.py.") path = 'html/doc/{}'.format(urlpath) if not urlpath.endswith('.html'): try: bdata = utils.read_file(path, binary=True) except OSError as e: raise SchemeOSError(e) mimetype = utils.guess_mimetype(urlpath) return mimetype, bdata try: data = utils.read_file(path) except OSError: # No .html around, let's see if we find the asciidoc asciidoc_path = path.replace('.html', '.asciidoc') if asciidoc_path.startswith('html/doc/'): asciidoc_path = asciidoc_path.replace('html/doc/', '../doc/help/') try: asciidoc = utils.read_file(asciidoc_path) except OSError: asciidoc = None if asciidoc is None: raise preamble = textwrap.dedent(""" There was an error loading the documentation! This most likely means the documentation was not generated properly. If you are running qutebrowser from the git repository, please (re)run scripts/asciidoc2html.py and reload this page. If you're running a released version this is a bug, please use :report to report it. Falling back to the plaintext version. --------------------------------------------------------------- """) return 'text/plain', (preamble + asciidoc).encode('utf-8') else: return 'text/html', data
def qute_pdfjs(url): """Handler for qute://pdfjs. Return the pdf.js viewer.""" if url.path() == '/file': filename = QUrlQuery(url).queryItemValue('filename') if not filename: raise UrlInvalidError("Missing filename") if '/' in filename or os.sep in filename: raise RequestDeniedError("Path separator in filename.") path = os.path.join(downloads.temp_download_manager.get_tmpdir().name, filename) with open(path, 'rb') as f: data = f.read() mimetype = utils.guess_mimetype(filename, fallback=True) return mimetype, data if url.path() == '/web/viewer.html': filename = QUrlQuery(url).queryItemValue("filename") if not filename: raise UrlInvalidError("Missing filename") data = pdfjs.generate_pdfjs_page(filename, url) return 'text/html', data try: data = pdfjs.get_pdfjs_res(url.path()) except pdfjs.PDFJSNotFound as e: # Logging as the error might get lost otherwise since we're not showing # the error page if a single asset is missing. This way we don't lose # information, as the failed pdfjs requests are still in the log. log.misc.warning("pdfjs resource requested but not found: {}".format( e.path)) raise NotFoundError("Can't find pdfjs resource '{}'".format(e.path)) else: mimetype = utils.guess_mimetype(url.fileName(), fallback=True) return mimetype, data
def qute_pdfjs(url): """Handler for qute://pdfjs. Return the pdf.js viewer.""" if url.path() == '/file': filename = QUrlQuery(url).queryItemValue('filename') if not filename: raise UrlInvalidError("Missing filename") if '/' in filename or os.sep in filename: raise RequestDeniedError("Path separator in filename.") path = os.path.join(downloads.temp_download_manager.get_tmpdir().name, filename) with open(path, 'rb') as f: data = f.read() mimetype = utils.guess_mimetype(filename, fallback=True) return mimetype, data if url.path() == '/web/viewer.html': filename = QUrlQuery(url).queryItemValue("filename") if not filename: raise UrlInvalidError("Missing filename") data = pdfjs.generate_pdfjs_page(filename, url) return 'text/html', data try: data = pdfjs.get_pdfjs_res(url.path()) except pdfjs.PDFJSNotFound as e: # Logging as the error might get lost otherwise since we're not showing # the error page if a single asset is missing. This way we don't lose # information, as the failed pdfjs requests are still in the log. log.misc.warning( "pdfjs resource requested but not found: {}".format(e.path)) raise NotFoundError("Can't find pdfjs resource '{}'".format(e.path)) else: mimetype = utils.guess_mimetype(url.fileName(), fallback=True) return mimetype, data
def qute_help(url): """Handler for qute://help.""" urlpath = url.path() if not urlpath or urlpath == '/': urlpath = 'index.html' else: urlpath = urlpath.lstrip('/') if not docutils.docs_up_to_date(urlpath): message.error("Your documentation is outdated! Please re-run " "scripts/asciidoc2html.py.") path = 'html/doc/{}'.format(urlpath) if not urlpath.endswith('.html'): try: bdata = utils.read_file(path, binary=True) except OSError as e: raise SchemeOSError(e) mimetype = utils.guess_mimetype(urlpath) return mimetype, bdata try: data = utils.read_file(path) except OSError: asciidoc = _asciidoc_fallback_path(path) if asciidoc is None: raise preamble = textwrap.dedent(""" There was an error loading the documentation! This most likely means the documentation was not generated properly. If you are running qutebrowser from the git repository, please (re)run scripts/asciidoc2html.py and reload this page. If you're running a released version this is a bug, please use :report to report it. Falling back to the plaintext version. --------------------------------------------------------------- """) return 'text/plain', (preamble + asciidoc).encode('utf-8') else: return 'text/html', data
def _data_url(self, path: str) -> str: """Get a data: url for the broken qutebrowser logo.""" data = resources.read_file_binary(path) mimetype = utils.guess_mimetype(path) return urlutils.data_url(mimetype, data).toString()
def _data_url(self, path): """Get a data: url for the broken qutebrowser logo.""" data = utils.read_file(path, binary=True) filename = utils.resource_filename(path) mimetype = utils.guess_mimetype(filename) return urlutils.data_url(mimetype, data).toString()
def _data_url(self, path): """Get a data: url for the broken qutebrowser logo.""" data = utils.read_file(path, binary=True) filename = utils.resource_filename(path) mimetype = utils.guess_mimetype(filename) return urlutils.data_url(mimetype, data).toString()