优化下载逻辑,待测试

This commit is contained in:
g1879 2024-10-23 22:46:36 +08:00
parent 5423976bb2
commit ce9b17e25d
3 changed files with 13 additions and 5 deletions

View File

@ -5,7 +5,6 @@
@Copyright: (c) 2024 by g1879, Inc. All Rights Reserved.
@License : BSD 3-Clause.
"""
from pathlib import Path
from time import perf_counter, sleep
from .waiter import wait_mission
@ -124,6 +123,8 @@ class Clicker(object):
if not self._ele.tab._browser._dl_mgr._running:
self._ele.tab._browser.set.download_path('.')
when_file_exists = None
tmp_path = None
if self._ele.tab._type.endswith('Page'):
obj = browser = self._ele.owner._browser
tid = 'browser'
@ -133,10 +134,16 @@ class Clicker(object):
tid = 'browser'
t_settings = TabDownloadSettings(self._ele.owner.tab_id)
b_settings = TabDownloadSettings('browser')
when_file_exists = b_settings.when_file_exists
b_settings.when_file_exists = t_settings.when_file_exists
b_settings.rename = t_settings.rename
b_settings.suffix = t_settings.suffix
t_settings.rename = None
t_settings.suffix = None
if not save_path:
tmp_path = b_settings.path
b_settings.path = t_settings.path
else:
obj = self._ele.owner._tab
@ -146,8 +153,6 @@ class Clicker(object):
if save_path:
tmp_path = obj.download_path
obj.set.download_path(save_path)
else:
tmp_path = None
if rename or suffix:
obj.set.download_file_name(rename, suffix)
if timeout is None:
@ -159,6 +164,8 @@ class Clicker(object):
if tmp_path:
obj.set.download_path(tmp_path)
if when_file_exists:
browser.set.when_download_file_exists(when_file_exists)
if m and new_tab:
self._ele.owner.browser._dl_mgr._tab_missions.setdefault(self._ele.owner.tab_id, []).append(m)
m.from_tab = self._ele.owner.tab_id

View File

@ -170,6 +170,7 @@ class DownloadManager(object):
to_path = str(get_usable_path(f'{mission.folder}{sep}{mission.name}'))
else:
to_path = f'{mission.folder}{sep}{mission.name}'
Path(mission.folder).mkdir(parents=True, exist_ok=True)
not_moved = True
for _ in range(10):
try:
@ -206,7 +207,7 @@ class TabDownloadSettings(object):
self.rename = None
self.suffix = None
self.path = '' if tab_id == 'browser' else self.TABS['browser'].path
self.when_file_exists = 'rename'
self.when_file_exists = 'rename' if tab_id == 'browser' else self.TABS['browser'].when_file_exists
TabDownloadSettings.TABS[tab_id] = self

View File

@ -131,7 +131,7 @@ class TabDownloadSettings(object):
rename: Optional[str] = ...
suffix: Optional[str] = ...
path: Optional[str] = ...
when_file_exists: str = ...
when_file_exists: FILE_EXISTS = ...
def __init__(self, tab_id: str):
"""