Skip to content

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

fix(http): 浏览器扩展发来的大文件下载不完整 #923

XiaoYouChR83871edadb75
diff --git a/CONTEXT.md b/CONTEXT.md
index 82248eb2..4e5edc6f 100644
--- a/CONTEXT.md
+++ b/CONTEXT.md
@@ -63,6 +63,10 @@ _Avoid_: hash、digest、哈希值
**Task Error**:
任务执行过程中的已知失败——服务器错误、磁盘空间不足、运行时未安装等。Task 有单一错误边界。
+**File Size**:
+成品在磁盘上的字节数,即未经传输压缩的长度。浏览器报来的大小可能是压缩后的长度,只作初值,以 Task Run 开始时服务器的回答为准。
+_Avoid_: totalBytes、content-length
+
### 文件选择
**Selectable File**:
diff --git a/browser_extension/app/src/background/download-spec.ts b/browser_extension/app/src/background/download-spec.ts
index b6842763..002db1bd 100644
--- a/browser_extension/app/src/background/download-spec.ts
+++ b/browser_extension/app/src/background/download-spec.ts
@@ -1,11 +1,8 @@
import type {Resource} from "../shared/types";
import {fileExtension, filenameFromUrl} from "../shared/utils";
-// ResourceTaskOptions 的权威。Per ADR-0001, the extension — not the desktop — decides
-// the filename/size/supportsRange for a browser-sourced task, because only the browser has
-// the page context (title/poster) and the media's real response. This module is that
-// authority: pure functions turning a captured 资源 (Resource) into the options the desktop
-// trusts. The two exported entry points are the test surface; the helpers stay internal.
+// The extension names a browser-sourced task because only the browser has the page context;
+// size is just an initial value the desktop corrects, since the browser may report the gzip length.
export interface ResourceTaskOptions {
url: string;
diff --git a/features/http_pack/task.py b/features/http_pack/task.py
index 88b598be..8e345250 100644
--- a/features/http_pack/task.py
+++ b/features/http_pack/task.py
@@ -383,10 +383,16 @@ class HttpTaskStep(TaskStep):
try:
response = await client.get(self.url, headers=probeHeaders)
self._effectiveUrl = str(response.url)
+ total = (response.headers.get("content-range") or b"").rpartition(b"/")[2]
response.close()
finally:
client.close()
+ # 浏览器报来的可能是 gzip 后的长度 #923
+ if isinstance(self.task, HttpTask) and total.isdigit() and int(total) != self.fileSize:
+ self.fileSize = self.task.fileSize = int(total)
+ self._deleteRecord()
+
restored = False
if self.canUseRangeRequests:
loaded = self._loadRecord()
diff --git a/tests/test_http_subworker.py b/tests/test_http_subworker.py
index 1f3694b7..77fc7a19 100644
--- a/tests/test_http_subworker.py
+++ b/tests/test_http_subworker.py
@@ -17,7 +17,7 @@ from aiohttp import web
from app.client import buildClient as realBuildClient
from features.http_pack.task import HttpTask, HttpTaskStep, HttpSubworker, PermanentDownloadError, RangeNotSupportedError
-from app.models.task import TaskStatus, TaskError
+from app.models.task import Task, TaskStatus, TaskError
from tests.helpers import buildFileContent, buildRangeHandler, runStep
@@ -459,6 +459,58 @@ class TestResume:
assert filepath.read_bytes() == content
+# ── File Size correction (#923) ──
+
+
+class TestFileSizeCorrection:
+
+ async def test_browser_compressed_size_is_corrected(self, server, tmp_path):
+ """浏览器报来 gzip 后的长度,成品仍须是完整的 identity 字节。"""
+ content = buildFileContent(1000)
+ url = await server(buildRangeHandler(content))
+ task, step = makeStep(url, tmp_path, fileSize=990, subworkerCount=4)
+ task.setStatus(TaskStatus.RUNNING)
+
+ await runStep(step)
+
+ assert (tmp_path / "test.bin").read_bytes() == content
+ assert step.fileSize == 1000
+ assert task.fileSize == 1000
+
+ async def test_segment_step_keeps_its_size(self, server, tmp_path):
+ """分段下载只要文件中的一段,不按服务器总长校正。"""
+ content = buildFileContent(1000)
+ url = await server(buildRangeHandler(content))
+ step = HttpTaskStep(stepIndex=0, url=url, fileSize=300, httpByteOffset=200,
+ subworkerCount=2, canUseRangeRequests=True, clientProfile="raw")
+ task = Task(name="test.bin", url=url, packId="ffmpeg", outputFolder=tmp_path, fileSize=300, steps=[step])
+ step._bindTask(task)
+ task.setStatus(TaskStatus.RUNNING)
+
+ await runStep(step)
+
+ assert (tmp_path / "test.bin").read_bytes() == content[200:500]
+ assert step.fileSize == 300
+
+ async def test_record_with_wrong_size_starts_fresh(self, server, tmp_path):
+ from struct import pack as struct_pack
+ content = buildFileContent(1000)
+ url = await server(buildRangeHandler(content))
+
+ filepath = tmp_path / "test.bin"
+ filepath.write_bytes(content[:450] + b'\x00' * 540)
+ with open(str(filepath) + ".ghd", "wb") as f:
+ f.write(struct_pack("<QQQ", 0, 450, 494))
+ f.write(struct_pack("<QQQ", 495, 495, 989))
+
+ task, step = makeStep(url, tmp_path, fileSize=990, subworkerCount=2)
+ task.setStatus(TaskStatus.RUNNING)
+
+ await runStep(step)
+
+ assert filepath.read_bytes() == content
+
+
# ── S13c: Transient errors ──