Remove download filename suffixes with QtWebEngine
This commit is contained in:
parent
5b70df15fa
commit
03eea7f62a
@ -19,6 +19,7 @@
|
||||
|
||||
"""QtWebEngine specific code for downloads."""
|
||||
|
||||
import re
|
||||
import os.path
|
||||
import functools
|
||||
|
||||
@ -122,6 +123,21 @@ class DownloadItem(downloads.AbstractDownloadItem):
|
||||
self._qt_item.accept()
|
||||
|
||||
|
||||
def _get_suggested_filename(path):
|
||||
"""Convert a path we got from chromium to a suggested filename.
|
||||
|
||||
Chromium thinks we want to download stuff to ~/Download, so even if we
|
||||
don't, we get downloads with a suffix like (1) for files existing there.
|
||||
|
||||
We simply strip the suffix off via regex.
|
||||
|
||||
See https://bugreports.qt.io/browse/QTBUG-56978
|
||||
"""
|
||||
filename = os.path.basename(path)
|
||||
filename = re.sub(r'\([0-9]+\)$', '', filename)
|
||||
return filename
|
||||
|
||||
|
||||
class DownloadManager(downloads.AbstractDownloadManager):
|
||||
|
||||
"""Manager for currently running downloads."""
|
||||
@ -134,7 +150,7 @@ class DownloadManager(downloads.AbstractDownloadManager):
|
||||
@pyqtSlot(QWebEngineDownloadItem)
|
||||
def handle_download(self, qt_item):
|
||||
"""Start a download coming from a QWebEngineProfile."""
|
||||
suggested_filename = os.path.basename(qt_item.path())
|
||||
suggested_filename = _get_suggested_filename(qt_item.path())
|
||||
|
||||
download = DownloadItem(qt_item)
|
||||
self._init_item(download, auto_remove=False,
|
||||
|
34
tests/unit/browser/webengine/test_webenginedownloads.py
Normal file
34
tests/unit/browser/webengine/test_webenginedownloads.py
Normal file
@ -0,0 +1,34 @@
|
||||
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
||||
|
||||
# Copyright 2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
||||
#
|
||||
# This file is part of qutebrowser.
|
||||
#
|
||||
# qutebrowser is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# qutebrowser is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os.path
|
||||
|
||||
import pytest
|
||||
|
||||
from qutebrowser.browser.webengine import webenginedownloads
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path, expected', [
|
||||
(os.path.join('subfolder', 'foo'), 'foo'),
|
||||
('foo(1)', 'foo'),
|
||||
('foo(a)', 'foo(a)'),
|
||||
('foo1', 'foo1'),
|
||||
])
|
||||
def test_get_suggested_filename(path, expected):
|
||||
assert webenginedownloads._get_suggested_filename(path) == expected
|
Loading…
Reference in New Issue
Block a user