Preserve filename in temporary downloads

When downloading a file into a temporary location for external open or pdfjs, it used to
prepend a random string to the filename. This means that downloading a file opened with
pdfjs would have an unwanted mangled filename.

Instead of adding a prefix to the filename, this puts the file with its filename intact
in a unique temporary directory.
This commit is contained in:
Samir Benmendil 2023-03-05 20:12:23 +00:00
parent b0e05ee160
commit 8ece2c92ba
3 changed files with 6 additions and 15 deletions

View File

@ -7,6 +7,7 @@
import re
import sys
import html
import io as _io
import os.path
import collections
import functools
@ -813,7 +814,8 @@ class AbstractDownloadItem(QObject):
if filename is None: # pragma: no cover
log.downloads.error("No filename to open the download!")
return
self.pdfjs_requested.emit(os.path.basename(filename),
dirname = os.path.basename(os.path.dirname(filename))
self.pdfjs_requested.emit(os.path.join(dirname, os.path.basename(filename)),
self.url())
def cancel_for_origin(self) -> bool:
@ -1373,8 +1375,8 @@ class TempDownloadManager:
# Make sure that the filename is not too long
suggested_name = utils.elide_filename(suggested_name, 50)
# pylint: disable=consider-using-with
fobj = tempfile.NamedTemporaryFile(dir=tmpdir.name, delete=False,
suffix='_' + suggested_name)
tmpfiledir = tempfile.mkdtemp(dir=tmpdir.name)
fobj = _io.open(os.path.join(tmpfiledir, suggested_name), 'w+b')
self.files.append(fobj)
return fobj

View File

@ -518,8 +518,6 @@ def qute_pdfjs(url: QUrl) -> _HandlerRet:
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:

View File

@ -9,7 +9,7 @@ import time
import logging
import py.path
from qutebrowser.qt.core import QUrl, QUrlQuery
from qutebrowser.qt.core import QUrl
import pytest
from qutebrowser.browser import qutescheme, pdfjs, downloads
@ -268,15 +268,6 @@ class TestPDFJSHandler:
with pytest.raises(qutescheme.UrlInvalidError):
qutescheme.data_for_url(QUrl('qute://pdfjs/file'))
@pytest.mark.parametrize('sep', ['/', os.sep])
def test_file_pathsep(self, sep):
url = QUrl('qute://pdfjs/file')
query = QUrlQuery()
query.addQueryItem('filename', 'foo{}bar'.format(sep))
url.setQuery(query)
with pytest.raises(qutescheme.RequestDeniedError):
qutescheme.data_for_url(url)
class TestQuteConfigdiff: