Skip to content

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

fix(download): 修复因 chunk 为空导致的文件损坏, 并增强日志系统以方便诊断问题

XiaoYouChR86ddb5c8135a
diff --git a/app/common/download_task.py b/app/common/download_task.py
index 18d06601..1eef60e8 100644
--- a/app/common/download_task.py
+++ b/app/common/download_task.py
@@ -202,6 +202,7 @@ class DownloadTask(QThread):
# 主下载逻辑
async def __handleWorker(self, worker: DownloadWorker):
+ logger.debug(f"{self.fileName} task is launching the worker {worker.startPos}-{worker.endPos}...")
if worker.progress < worker.endPos: # 因为可能会创建空线程
finished = False
while not finished:
@@ -212,6 +213,8 @@ class DownloadTask(QThread):
async with worker.client.stream(url=self.url, headers=workingRangeHeaders, timeout=30,
method="GET") as res:
+ if res.status_code != 206:
+ raise httpx.HTTPStatusError(f"服务器拒绝了范围请求,状态码:{res.status_code}")
async for chunk in res.aiter_bytes():
if worker.endPos <= worker.progress:
break
@@ -225,13 +228,15 @@ class DownloadTask(QThread):
if cfg.speedLimitation.value:
if cfg.globalSpeed >= cfg.speedLimitation.value:
await asyncio.sleep(1) # 在锁里面睡,只阻塞 worker, 不阻塞 supervisor
+ else:
+ raise httpx.HTTPStatusError("服务器返回空数据")
if worker.progress >= worker.endPos:
worker.progress = worker.endPos
finished = True
- except httpx.HTTPError as e:
+ except Exception as e:
logger.info(
f"Task: {self.fileName}, Thread {worker} is reconnecting to the server, Error: {repr(e)}")
@@ -386,8 +391,6 @@ class DownloadTask(QThread):
if self.ableToParallelDownload:
for i in self.workers: # 启动 Worker
- logger.debug(f"Task {self.fileName}, starting the thread {i}...")
-
_ = asyncio.create_task(self.__handleWorker(i))
self.tasks.append(_)
diff --git a/app/common/methods.py b/app/common/methods.py
index efac2caa..5d804c6c 100644
--- a/app/common/methods.py
+++ b/app/common/methods.py
@@ -202,10 +202,11 @@ def getLinkInfo(url: str, headers: dict, fileName: str = "", verify: bool = cfg.
url = str(response.url)
# 获取文件大小, 判断是否可以分块下载
- if "content-length" not in head or response.status_code == 200: #状态码为206才是范围请求,200表示服务器拒绝了范围请求同时将发送整个文件
- fileSize = 0
- else:
+ # if "content-length" not in head or response.status_code != 200: #状态码为206才是范围请求,200表示服务器拒绝了范围请求同时将发送整个文件
+ if head.get("accept-ranges"):
fileSize = int(head["content-length"])
+ else:
+ fileSize = 0
# 获取文件名
if not fileName: