Skip to content

The only downloader you need. 下载器的集大成者。

优化文件名获取逻辑

XiaoYouChR7ee8e1a5e688
diff --git a/app/common/methods.py b/app/common/methods.py
index 85dafb73..e1c59719 100644
--- a/app/common/methods.py
+++ b/app/common/methods.py
@@ -3,7 +3,6 @@ import inspect
import os
import re
import sys
-import urllib
from datetime import datetime, timedelta, timezone
from email.utils import decode_rfc2231
from functools import wraps
@@ -206,18 +205,17 @@ def getLinkInfo(url:str, headers:dict, fileName:str="", verify:bool=False, proxy
match = re.search(r'filename\*\s*=\s*([^;]+)', headerValue, re.IGNORECASE)
if match:
fileName = match.group(1)
- fileName = decode_rfc2231(fileName)
- fileName = urllib.parse.unquote(fileName[2]) # fileName* 后的部分是编码信息
+ fileName = decode_rfc2231(fileName)[2] # fileName* 后的部分是编码信息
# 如果 fileName* 没有成功获取,尝试处理普通的 fileName
if not fileName and 'filename' in headerValue:
match = re.search(r'filename\s*=\s*["\']?([^"\';]+)["\']?', headerValue, re.IGNORECASE)
if match:
fileName = match.group(1)
- fileName = urllib.parse.unquote(fileName)
- # 移除文件名头尾可能存在的引号
+ # 移除文件名头尾可能存在的引号并解码
if fileName:
+ fileName = unquote(fileName)
fileName = fileName.strip('"\'')
else:
raise KeyError
@@ -234,13 +232,12 @@ def getLinkInfo(url:str, headers:dict, fileName:str="", verify:bool=False, proxy
fileName = \
unquote(parse_qs(urlparse(url).query).get('response-content-disposition', [''])[0]).split(
"filename=")[-1]
- # 去掉可能存在的引号
- if fileName.startswith('"') and fileName.endswith('"'):
- fileName = fileName[1:-1]
- elif fileName.startswith("'") and fileName.endswith("'"):
- fileName = fileName[1:-1]
- if not fileName:
+ # 移除文件名头尾可能存在的引号并解码
+ if fileName:
+ fileName = unquote(fileName)
+ fileName = fileName.strip('"\'')
+ else:
raise KeyError
logger.debug(f"方法2获取文件名成功, 文件名:{fileName}")
@@ -248,7 +245,7 @@ def getLinkInfo(url:str, headers:dict, fileName:str="", verify:bool=False, proxy
except (KeyError, IndexError) as e:
logger.info(f"方法2获取文件名失败, KeyError or IndexError:{e}")
- fileName = urlparse(url).path.split('/')[-1]
+ fileName = unquote(urlparse(url).path.split('/')[-1])
if fileName:
logger.debug(f"方法3获取文件名成功, 文件名:{fileName}")