fix: 更新包过大时分段上传并自动拼回
This commit is contained in:
+38
-24
@@ -23,7 +23,7 @@ _state = {
|
||||
"message": "",
|
||||
"latest": "",
|
||||
"notes": "",
|
||||
"url": "",
|
||||
"urls": [],
|
||||
}
|
||||
_worker: threading.Thread | None = None
|
||||
|
||||
@@ -80,21 +80,32 @@ def public_notes(value: str) -> str:
|
||||
return text.strip()
|
||||
|
||||
|
||||
def release_urls(payload: dict) -> list[str]:
|
||||
assets = payload.get("assets") if isinstance(payload.get("assets"), list) else []
|
||||
named: dict[str, str] = {}
|
||||
for item in assets:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
name = str(item.get("name") or "")
|
||||
url = str(item.get("browser_download_url") or "").strip()
|
||||
if name and url:
|
||||
named[name] = url
|
||||
if named.get(ASSET_NAME):
|
||||
return [named[ASSET_NAME]]
|
||||
parts = sorted(name for name in named if name.startswith(ASSET_NAME + ".part"))
|
||||
return [named[name] for name in parts]
|
||||
|
||||
|
||||
def describe_release(payload: dict, current: str) -> dict | None:
|
||||
if not isinstance(payload, dict) or payload.get("draft") or payload.get("prerelease"):
|
||||
return None
|
||||
latest = normalize_version(str(payload.get("tag_name") or payload.get("name") or ""))
|
||||
if not is_newer(latest, current):
|
||||
return None
|
||||
url = ""
|
||||
assets = payload.get("assets") if isinstance(payload.get("assets"), list) else []
|
||||
for item in assets:
|
||||
if isinstance(item, dict) and item.get("name") == ASSET_NAME:
|
||||
url = str(item.get("browser_download_url") or "").strip()
|
||||
break
|
||||
if not url:
|
||||
urls = release_urls(payload)
|
||||
if not urls:
|
||||
return None
|
||||
return {"latest": latest, "notes": public_notes(str(payload.get("body") or "")), "url": url}
|
||||
return {"latest": latest, "notes": public_notes(str(payload.get("body") or "")), "urls": urls}
|
||||
|
||||
|
||||
def check() -> dict:
|
||||
@@ -112,9 +123,9 @@ def check() -> dict:
|
||||
if found:
|
||||
_state["latest"] = found["latest"]
|
||||
_state["notes"] = found["notes"]
|
||||
_state["url"] = found["url"]
|
||||
_state["urls"] = list(found["urls"])
|
||||
else:
|
||||
_state["url"] = ""
|
||||
_state["urls"] = []
|
||||
return {
|
||||
"available": bool(found),
|
||||
"current": current,
|
||||
@@ -138,7 +149,7 @@ def start() -> dict:
|
||||
if not is_packaged():
|
||||
raise RuntimeError("请使用发布的程序更新")
|
||||
with _lock:
|
||||
if not _state["url"]:
|
||||
if not _state["urls"]:
|
||||
raise RuntimeError("没有可安装的更新")
|
||||
if _worker is not None and _worker.is_alive():
|
||||
return status()
|
||||
@@ -157,11 +168,13 @@ def _set(**values) -> None:
|
||||
|
||||
def _install() -> None:
|
||||
with _lock:
|
||||
url = _state["url"]
|
||||
urls = list(_state["urls"])
|
||||
temp_root = Path(tempfile.mkdtemp(prefix="VideosDownTools-update-"))
|
||||
archive = temp_root / ASSET_NAME
|
||||
try:
|
||||
_download(url, archive)
|
||||
with archive.open("wb") as handle:
|
||||
for index, url in enumerate(urls, start=1):
|
||||
_download(url, handle, index, len(urls))
|
||||
source = _extract(archive, temp_root / "package")
|
||||
_launch_updater(source)
|
||||
except Exception:
|
||||
@@ -171,20 +184,21 @@ def _install() -> None:
|
||||
_set(state="restarting", percent=100, message="正在重新打开")
|
||||
|
||||
|
||||
def _download(url: str, dest: Path) -> None:
|
||||
def _download(url: str, handle, index: int, count: int) -> None:
|
||||
done = 0
|
||||
with httpx.stream("GET", url, timeout=600.0, follow_redirects=True, trust_env=True) as response:
|
||||
response.raise_for_status()
|
||||
total = int(response.headers.get("content-length") or 0)
|
||||
with dest.open("wb") as handle:
|
||||
for chunk in response.iter_bytes(256 * 1024):
|
||||
if not chunk:
|
||||
continue
|
||||
handle.write(chunk)
|
||||
done += len(chunk)
|
||||
if total:
|
||||
_set(percent=min(99, done * 100 // total), message="正在下载更新")
|
||||
if dest.stat().st_size <= 0:
|
||||
for chunk in response.iter_bytes(256 * 1024):
|
||||
if not chunk:
|
||||
continue
|
||||
handle.write(chunk)
|
||||
done += len(chunk)
|
||||
if total:
|
||||
part = min(99, done * 100 // total)
|
||||
overall = int(((index - 1) + part / 100) / count * 99)
|
||||
_set(percent=overall, message="正在下载更新")
|
||||
if done <= 0:
|
||||
raise RuntimeError("更新包是空的")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user