修复一个下载路径设置问题

This commit is contained in:
g1879 2024-01-10 17:47:15 +08:00
parent d896eebb32
commit e2acf94b19
3 changed files with 12 additions and 8 deletions

View File

@ -22,6 +22,7 @@ class DownloadManager(object):
self._browser = browser
self._page = browser.page
self._when_download_file_exists = 'rename'
self._save_path = None
t = TabDownloadSettings(self._page.tab_id)
t.path = self._page.download_path
@ -46,18 +47,19 @@ class DownloadManager(object):
"""返回所有未完成的下载任务"""
return self._missions
def set_path(self, tab_id, path):
def set_path(self, tab, path):
"""设置某个tab的下载路径
:param tab_id: tab id
:param tab: 页面对象
:param path: 下载路径绝对路径str
:return: None
"""
TabDownloadSettings(tab_id).path = path
if tab_id == self._page.tab_id or not self._running:
TabDownloadSettings(tab.tab_id).path = path
if tab is self._page or not self._running:
self._browser.driver.set_callback('Browser.downloadProgress', self._onDownloadProgress)
self._browser.driver.set_callback('Browser.downloadWillBegin', self._onDownloadWillBegin)
r = self._browser.run_cdp('Browser.setDownloadBehavior', downloadPath=path,
behavior='allowAndName', eventsEnabled=True)
self._save_path = path
if 'error' in r:
print('浏览器版本太低无法使用下载管理功能。')
self._running = True
@ -182,7 +184,7 @@ class DownloadManager(object):
elif settings.when_file_exists == 'overwrite':
goal_path.unlink()
m = DownloadMission(self, tab_id, guid, settings.path, name, kwargs['url'], self._page.download_path)
m = DownloadMission(self, tab_id, guid, settings.path, name, kwargs['url'], self._save_path)
self._missions[guid] = m
if self.get_flag(tab_id) is False: # 取消该任务
@ -210,7 +212,7 @@ class DownloadManager(object):
return
mission.received_bytes = kwargs['receivedBytes']
mission.total_bytes = kwargs['totalBytes']
form_path = f'{mission.path}{sep}{mission.id}'
form_path = f'{mission.save_path}{sep}{mission.id}'
to_path = str(get_usable_path(f'{mission.path}{sep}{mission.name}'))
move(form_path, to_path)
self.set_done(mission, 'completed', final_path=to_path)

View File

@ -8,6 +8,7 @@
from typing import Dict, Optional, Union, Literal
from .._base.browser import Browser
from .._pages.chromium_base import ChromiumBase
from .._pages.chromium_page import ChromiumPage
@ -18,13 +19,14 @@ class DownloadManager(object):
_tab_missions: dict = ...
_flags: dict = ...
_running: bool = ...
_save_path: Optional[str] = ...
def __init__(self, browser: Browser): ...
@property
def missions(self) -> Dict[str, DownloadMission]: ...
def set_path(self, tab_id: str, path: str) -> None: ...
def set_path(self, tab: ChromiumBase, path: str) -> None: ...
def set_rename(self, tab_id: str, rename: str = None, suffix: str = None) -> None: ...

View File

@ -185,7 +185,7 @@ class TabSetter(ChromiumBaseSetter):
"""
path = str(Path(path).absolute())
self._page._download_path = path
self._page.browser._dl_mgr.set_path(self._page.tab_id, path)
self._page.browser._dl_mgr.set_path(self._page, path)
if self._page._DownloadKit:
self._page._DownloadKit.set.goal_path(path)