mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
download函数会去除路径中的非法字符,并返回完整路径
This commit is contained in:
parent
f54d33a19a
commit
a5f90f5dd9
@ -7,7 +7,7 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from random import random
|
from random import randint
|
||||||
from time import time
|
from time import time
|
||||||
from typing import Union, List
|
from typing import Union, List
|
||||||
from urllib.parse import urlparse, quote
|
from urllib.parse import urlparse, quote
|
||||||
@ -128,7 +128,7 @@ class SessionPage(object):
|
|||||||
:param kwargs: 连接参数
|
:param kwargs: 连接参数
|
||||||
:param file_exists: 若存在同名文件,可选择'rename', 'overwrite', 'skip'方式处理
|
:param file_exists: 若存在同名文件,可选择'rename', 'overwrite', 'skip'方式处理
|
||||||
:param show_msg: 是否显示下载信息
|
:param show_msg: 是否显示下载信息
|
||||||
:return: 元组,bool和状态信息(成功时信息为文件名)
|
:return: 元组,bool和状态信息(成功时信息为文件路径)
|
||||||
"""
|
"""
|
||||||
goal_path = goal_path or OptionsManager().get_value('paths', 'global_tmp_path')
|
goal_path = goal_path or OptionsManager().get_value('paths', 'global_tmp_path')
|
||||||
if not goal_path:
|
if not goal_path:
|
||||||
@ -150,7 +150,7 @@ class SessionPage(object):
|
|||||||
elif os.path.basename(file_url):
|
elif os.path.basename(file_url):
|
||||||
file_name = os.path.basename(file_url).split("?")[0]
|
file_name = os.path.basename(file_url).split("?")[0]
|
||||||
else:
|
else:
|
||||||
file_name = f'untitled_{time()}_{random.randint(0, 100)}'
|
file_name = f'untitled_{time()}_{randint(0, 100)}'
|
||||||
|
|
||||||
if rename: # 重命名文件,不改变扩展名
|
if rename: # 重命名文件,不改变扩展名
|
||||||
ext_name = file_name.split('.')[-1]
|
ext_name = file_name.split('.')[-1]
|
||||||
@ -161,7 +161,14 @@ class SessionPage(object):
|
|||||||
else:
|
else:
|
||||||
full_name = file_name
|
full_name = file_name
|
||||||
|
|
||||||
|
full_name = re.sub(r'[\\/*:|<>?"]', '', full_name).strip()
|
||||||
|
goal_Path = Path(goal_path)
|
||||||
|
goal_path = ''
|
||||||
|
for key, i in enumerate(goal_Path.parts): # 去除路径中的非法字符
|
||||||
|
goal_path += goal_Path.drive if key == 0 and goal_Path.drive else re.sub(r'[*:|<>?"]', '', i).strip()
|
||||||
|
goal_path += '\\' if i != '\\' and key < len(goal_Path.parts) - 1 else ''
|
||||||
full_path = Path(f'{goal_path}\\{full_name}')
|
full_path = Path(f'{goal_path}\\{full_name}')
|
||||||
|
|
||||||
if full_path.exists():
|
if full_path.exists():
|
||||||
if file_exists == 'skip':
|
if file_exists == 'skip':
|
||||||
return False, 'A file with the same name already exists.'
|
return False, 'A file with the same name already exists.'
|
||||||
@ -172,6 +179,8 @@ class SessionPage(object):
|
|||||||
full_path = Path(f'{goal_path}\\{full_name}')
|
full_path = Path(f'{goal_path}\\{full_name}')
|
||||||
else:
|
else:
|
||||||
raise ValueError("file_exists can only be selected in 'skip', 'overwrite', 'rename'")
|
raise ValueError("file_exists can only be selected in 'skip', 'overwrite', 'rename'")
|
||||||
|
Path(goal_path).mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
# 打印要下载的文件
|
# 打印要下载的文件
|
||||||
if show_msg:
|
if show_msg:
|
||||||
print_txt = full_name if file_name == full_name else f'{file_name} -> {full_name}'
|
print_txt = full_name if file_name == full_name else f'{file_name} -> {full_name}'
|
||||||
@ -206,7 +215,7 @@ class SessionPage(object):
|
|||||||
# -------------------显示并返回值-------------------
|
# -------------------显示并返回值-------------------
|
||||||
if show_msg:
|
if show_msg:
|
||||||
print(info)
|
print(info)
|
||||||
info = full_name if download_status else info
|
info = f'{goal_path}\\{full_name}' if download_status else info
|
||||||
return download_status, info
|
return download_status, info
|
||||||
|
|
||||||
def _make_response(self, url: str, mode: str = 'get', data: dict = None, **kwargs) -> Union[HTMLResponse, bool]:
|
def _make_response(self, url: str, mode: str = 'get', data: dict = None, **kwargs) -> Union[HTMLResponse, bool]:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user