diff --git a/GC-DOWNLOAD/DownloadCenter.cs b/GC-DOWNLOAD/DownloadCenter.cs
index c5e13e8..de035f7 100644
--- a/GC-DOWNLOAD/DownloadCenter.cs
+++ b/GC-DOWNLOAD/DownloadCenter.cs
@@ -56,12 +56,14 @@ public sealed partial class DownloadJob : ObservableObject, IDownloadJob
partial void OnErrorChanged(string? value) => RefreshText();
}
-/// 下载中心:最多 2 个并发,任务在「后台列表」里折叠展示。
+/// 下载中心:最多 4 个并发,任务在「后台列表」里折叠展示。
public sealed class DownloadCenter : IDownloadCenter
{
private readonly ModelRepository _repository;
private readonly AppPaths _paths;
- private readonly SemaphoreSlim _slots = new(2);
+ // 断点续传后单任务失败重来的代价小了很多,可以适当提高并发;
+ // 更新服务那边已是 6,模型下载保持 4,兼顾弱网吞吐与源站压力。
+ private readonly SemaphoreSlim _slots = new(4);
public ObservableCollection Jobs { get; } = new();
public event Action? Installed;
diff --git a/GC-DOWNLOAD/ModelSourceClient.cs b/GC-DOWNLOAD/ModelSourceClient.cs
index 2e864be..e284737 100644
--- a/GC-DOWNLOAD/ModelSourceClient.cs
+++ b/GC-DOWNLOAD/ModelSourceClient.cs
@@ -5,6 +5,8 @@ using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
+using System.Security.Cryptography;
+using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GC.Models;
@@ -158,18 +160,21 @@ public sealed class ModelSourceClient : IModelSourceClient
private async Task DownloadZipAsync(string url, string cacheDir, CancellationToken ct)
{
+ // .part 用 URL 哈希做稳定文件名:同一个源重试时命中同一份半成品,
+ // 断点续传才有意义(用 GUID 的话每次重试都是新文件,等于从零开始)。
+ // 解压目录仍用 GUID —— 它只是中间产物,成功后整块移走或失败即删。
+ var key = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(url)))[..16].ToLowerInvariant();
+ var partPath = Path.Combine(_paths.CacheRoot, "tmp", "downloads", key + ".part");
var tmpRoot = Path.Combine(_paths.CacheRoot, "tmp", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(tmpRoot);
var zipPath = Path.Combine(tmpRoot, "source.zip");
try
{
- using (var response = await Http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead, ct))
- {
- response.EnsureSuccessStatusCode();
- await using var input = await response.Content.ReadAsStreamAsync(ct);
- await using var output = File.Create(zipPath);
- await input.CopyToAsync(output, 81920, ct);
- }
+ // 带断点续传的流式下载;网络中断时 .part 保留,下次从这里接上。
+ await ResumableDownloader.DownloadAsync(Http, url, partPath, null, ct)
+ .ConfigureAwait(false);
+ // 下载完整,.part 转正。转正成功即代表这份 zip 已完整,删掉 .part 避免重复下载。
+ File.Move(partPath, zipPath, overwrite: true);
var extractDir = Path.Combine(tmpRoot, "extract");
Directory.CreateDirectory(extractDir);
diff --git a/GC-DOWNLOAD/ResumableDownloader.cs b/GC-DOWNLOAD/ResumableDownloader.cs
new file mode 100644
index 0000000..e659a6a
--- /dev/null
+++ b/GC-DOWNLOAD/ResumableDownloader.cs
@@ -0,0 +1,100 @@
+using System;
+using System.IO;
+using System.Net;
+using System.Net.Http;
+using System.Net.Http.Headers;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace GC.Services;
+
+///
+/// 带断点续传的流式下载器。已下载的部分写在 *.part 文件里,
+/// 完整下载后由调用方把 .part 改成正式文件名。
+///
+/// 为什么需要它:模型源码 zip 可能有上百 MB,弱网中断后如果每次都从头下,
+/// 用户会反复卡在同一个地方。这里用 HTTP Range 从已下载的字节接着下。
+///
+/// 兼容性:服务器不支持 Range(忽略请求、返回 200)时自动退化为整段下载,
+/// 不会卡死或拿到半截文件。 起始位置不符时也从头下。
+///
+/// .part 文件只有在"续传"场景才复用;一旦下载完整、调用方改名为正式文件,
+/// 下次就是全新下载,不会把旧内容接在新内容后面。
+///
+public static class ResumableDownloader
+{
+ ///
+ /// 下载 ,数据写入 。
+ /// 返回最终 .part 文件的字节数。下载完整后由调用方负责改名 / 校验。
+ ///
+ public static async Task DownloadAsync(
+ HttpClient http,
+ string url,
+ string partPath,
+ IProgress<(long Done, long Total)>? progress,
+ CancellationToken ct)
+ {
+ // 已有 .part 就尝试从断点续传;没有则从头下
+ long resumeFrom = 0;
+ if (File.Exists(partPath))
+ {
+ try { resumeFrom = new FileInfo(partPath).Length; }
+ catch { resumeFrom = 0; }
+ }
+
+ using var req = new HttpRequestMessage(HttpMethod.Get, url);
+ if (resumeFrom > 0)
+ req.Headers.Range = new RangeHeaderValue(resumeFrom, null);
+
+ using var response = await http.SendAsync(req, HttpCompletionOption.ResponseHeadersRead, ct)
+ .ConfigureAwait(false);
+ response.EnsureSuccessStatusCode();
+
+ var canResume = response.StatusCode == HttpStatusCode.PartialContent && resumeFrom > 0;
+ long? total = response.Content.Headers.ContentLength;
+
+ if (canResume)
+ {
+ // 校验服务器确实从我们要的位置开始返回:Content-Range: bytes START-END/TOTAL
+ var range = response.Content.Headers.ContentRange;
+ if (range?.HasRange == true && range.From == resumeFrom && range.Length.HasValue)
+ {
+ total = range.Length.Value;
+ }
+ else
+ {
+ // 服务器忽略了 Range 或给出的范围对不上:从头覆盖,避免拼接出损坏的文件
+ canResume = false;
+ resumeFrom = 0;
+ }
+ }
+
+ var mode = canResume ? FileMode.Append : FileMode.Create;
+ var dir = Path.GetDirectoryName(partPath);
+ if (!string.IsNullOrEmpty(dir)) Directory.CreateDirectory(dir);
+
+ await using (var input = await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false))
+ await using (var output = new FileStream(partPath, mode, FileAccess.Write, FileShare.Read,
+ 81920, useAsync: true))
+ {
+ var buffer = new byte[81920];
+ long done = resumeFrom;
+ int read;
+ while ((read = await input.ReadAsync(buffer.AsMemory(), ct).ConfigureAwait(false)) > 0)
+ {
+ await output.WriteAsync(buffer.AsMemory(0, read), ct).ConfigureAwait(false);
+ done += read;
+ // Total 未知时用已下载量当分母,进度条会停在 100% 之前但不报错
+ progress?.Report((done, total ?? done));
+ }
+ }
+
+ return SafeLength(partPath);
+ }
+
+ private static long SafeLength(string path)
+ {
+ try { return new FileInfo(path).Length; }
+ catch { return 0; }
+ }
+}
diff --git a/GC-UI/frontend/lib/pages.dart b/GC-UI/frontend/lib/pages.dart
index 9c14c59..ff4cc5f 100644
--- a/GC-UI/frontend/lib/pages.dart
+++ b/GC-UI/frontend/lib/pages.dart
@@ -1,2333 +1,7 @@
-import 'dart:async';
+// 页面聚合导出:四个页面按职责拆到 pages/ 子目录。
+// 保留这个文件是为了让 shell.dart 等调用方的 import 路径不变。
-import 'package:flutter/material.dart';
-
-import 'api_client.dart';
-import 'controller.dart';
-import 'i18n.dart';
-import 'theme.dart';
-
-// ---------------------------------------------------------------------------
-// 首页:模型库
-// ---------------------------------------------------------------------------
-
-class HomePageView extends StatefulWidget {
- const HomePageView({super.key, required this.controller, this.initialModels});
-
- final ShellController controller;
-
- /// 预置模型列表(仅用于测试 / 截图,正常运行时为 null,由后端提供)。
- final List? initialModels;
-
- @override
- State createState() => _HomePageViewState();
-}
-
-class _HomePageViewState extends State {
- List _models = const [];
- bool _loading = true;
- String? _error;
- String? _notice;
- int _lastRevision = 0;
-
- @override
- void initState() {
- super.initState();
- widget.controller.addListener(_onControllerChanged);
- _lastRevision = widget.controller.modelsRevision;
-
- final preset = widget.initialModels;
- if (preset != null) {
- _models = preset;
- _loading = false;
- } else {
- _load();
- }
- }
-
- @override
- void dispose() {
- widget.controller.removeListener(_onControllerChanged);
- super.dispose();
- }
-
- /// 下载完成 / 删除模型后重新拉列表:页面被 Offstage 保活,不会自己重建。
- void _onControllerChanged() {
- if (!mounted) return;
- final revision = widget.controller.modelsRevision;
- if (revision == _lastRevision) return;
- _lastRevision = revision;
- if (widget.initialModels != null) return; // 截图 / 测试用的预置数据
- _load();
- }
-
- Future _load() async {
- setState(() {
- _loading = true;
- _error = null;
- });
- try {
- final models = await widget.controller.api.models();
- if (!mounted) return;
- setState(() {
- _models = models;
- _loading = false;
- });
- } catch (error) {
- if (!mounted) return;
- setState(() {
- _error = error.toString();
- _loading = false;
- });
- }
- }
-
- Future _launch(ModelInfo model) async {
- setState(() => _notice = null);
- try {
- final result = await widget.controller.api.launchModel(model.id);
- if (!mounted) return;
- setState(() => _notice = result.message);
- } catch (error) {
- if (!mounted) return;
- setState(() => _notice = error.toString());
- }
- }
-
- Future _delete(ModelInfo model) async {
- final confirmed = await showDialog(
- context: context,
- builder: (context) => AlertDialog(
- backgroundColor: GcColors.surface,
- title: Text(T('common.confirmTitle')),
- content: Text(T('home.deleteConfirm',