新增 Rust 实现的多线程下载器,优化下载可靠性

This commit is contained in:
2023-11-05 15:56:48 +08:00
parent 3409a8597f
commit 5bc7024fe7
17 changed files with 1379 additions and 421 deletions

View File

@ -1,163 +0,0 @@
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:starcitizen_doctor/base/ui.dart';
/// https://github.com/qiaoshouqing/dio_range_download/blob/master/lib/dio_range_download.dart
class RangeDownload {
static Future<Response> downloadWithChunks(
url,
savePath, {
bool isRangeDownload = true,
ProgressCallback? onReceiveProgress,
int maxChunk = 6,
Dio? dio,
CancelToken? cancelToken,
}) async {
const firstChunkSize = 102;
int total = 0;
if (dio == null) {
dio = Dio();
dio.options.connectTimeout = const Duration(seconds: 10);
}
var progress = <int>[];
var progressInit = <int>[];
Future mergeTempFiles(chunk) async {
File f = File(savePath + "temp0");
IOSink ioSink = f.openWrite(mode: FileMode.writeOnlyAppend);
for (int i = 1; i < chunk; ++i) {
File f0 = File(savePath + "temp$i");
await ioSink.addStream(f0.openRead());
await f0.delete();
}
await ioSink.close();
await f.rename(savePath);
}
Future mergeFiles(file1, file2, targetFile) async {
File f1 = File(file1);
File f2 = File(file2);
IOSink ioSink = f1.openWrite(mode: FileMode.writeOnlyAppend);
await ioSink.addStream(f2.openRead());
await f2.delete();
await ioSink.close();
await f1.rename(targetFile);
}
createCallback(no) {
return (int received, rangeTotal) async {
if (received >= rangeTotal) {
var path = savePath + "temp$no";
var oldPath = savePath + "temp${no}_pre";
File oldFile = File(oldPath);
if (oldFile.existsSync()) {
await mergeFiles(oldPath, path, path);
}
}
try {
progress[no] = progressInit[no] + received;
} catch (e) {
dPrint(e);
}
if (onReceiveProgress != null && total != 0) {
onReceiveProgress(progress.reduce((a, b) => a + b), total);
}
};
}
Future<Response> downloadChunk(url, start, end, no,
{isMerge = true}) async {
int initLength = 0;
--end;
var path = savePath + "temp$no";
File targetFile = File(path);
if (await targetFile.exists() && isMerge) {
dPrint("good job start:$start length:${File(path).lengthSync()}");
if (start + await targetFile.length() < end) {
initLength = await targetFile.length();
start += initLength;
var preFile = File(path + "_pre");
if (await preFile.exists()) {
initLength += await preFile.length();
start += await preFile.length();
await mergeFiles(preFile.path, targetFile.path, preFile.path);
} else {
await targetFile.rename(preFile.path);
}
} else {
await targetFile.delete();
}
}
progress.add(initLength);
progressInit.add(initLength);
return dio!.download(
url,
path,
deleteOnError: false,
onReceiveProgress: createCallback(no),
options: Options(
headers: {"range": "bytes=$start-$end"},
),
cancelToken: cancelToken,
);
}
if (isRangeDownload) {
Response response =
await downloadChunk(url, 0, firstChunkSize, 0, isMerge: false);
if (response.statusCode == 206) {
dPrint("This http protocol support range download");
total = int.parse(response.headers
.value(HttpHeaders.contentRangeHeader)!
.split("/")
.last);
int reserved = total -
int.parse(response.headers.value(HttpHeaders.contentLengthHeader)!);
int chunk = (reserved / firstChunkSize).ceil() + 1;
if (chunk > 1) {
int chunkSize = firstChunkSize;
if (chunk > maxChunk + 1) {
chunk = maxChunk + 1;
chunkSize = (reserved / maxChunk).ceil();
}
var futures = <Future>[];
for (int i = 0; i < maxChunk; ++i) {
int start = firstChunkSize + i * chunkSize;
int end;
if (i == maxChunk - 1) {
end = total;
} else {
end = start + chunkSize;
}
futures.add(downloadChunk(url, start, end, i + 1));
}
await Future.wait(futures);
}
await mergeTempFiles(chunk);
return Response(
statusCode: 200,
statusMessage: "Download success.",
data: "Download success.",
requestOptions: RequestOptions(),
);
} else if (response.statusCode == 200) {
dPrint(
"The protocol does not support resumed downloads, and regular downloads will be used.");
return dio.download(url, savePath,
onReceiveProgress: onReceiveProgress,
cancelToken: cancelToken,
deleteOnError: false);
} else {
dPrint("The request encountered a problem, please handle it yourself");
return response;
}
} else {
return dio.download(url, savePath,
onReceiveProgress: onReceiveProgress,
cancelToken: cancelToken,
deleteOnError: false);
}
}
}

View File

@ -54,7 +54,7 @@ class DownloaderDialogUI extends BaseUI<DownloaderDialogUIModel> {
String getStatus(DownloaderDialogUIModel model) {
if (model.progress == null && !model.isInMerging) return "准备中...";
if (model.isInMerging) return "正在合并文件...";
if (model.isInMerging) return "正在处理文件...";
return "${model.progress?.toStringAsFixed(2) ?? "0"}% ";
}
}

View File

@ -1,10 +1,8 @@
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:file_picker/file_picker.dart';
import 'package:starcitizen_doctor/base/ui_model.dart';
import 'dio_range_download.dart';
import 'package:starcitizen_doctor/common/rust/ffi.dart';
class DownloaderDialogUIModel extends BaseUIModel {
final String fileName;
@ -16,16 +14,12 @@ class DownloaderDialogUIModel extends BaseUIModel {
DownloaderDialogUIModel(this.fileName, this.savePath, this.downloadUrl,
{this.showChangeSavePathDialog = false, this.threadCount = 1});
CancelToken? downloadCancelToken;
int? downloadTaskId;
bool isInMerging = false;
String? downloadTaskId;
double? progress;
int? speed;
DateTime? lastUpdateTime;
int? lastUpdateCount;
int? count;
int? total;
@ -52,48 +46,66 @@ class DownloaderDialogUIModel extends BaseUIModel {
savePath = userSelect;
dPrint(savePath);
notifyListeners();
} else {
savePath = "$savePath/$fileName";
}
// start download
if (savePath.endsWith("\\$fileName")) {
savePath = savePath.substring(0, savePath.length - fileName.length - 1);
}
final downloaderSavePath = "$savePath//$fileName.downloading";
try {
downloadCancelToken = CancelToken();
final r = await RangeDownload.downloadWithChunks(downloadUrl, savePath,
maxChunk: 10,
cancelToken: downloadCancelToken,
isRangeDownload: true, onReceiveProgress: (int count, int total) {
lastUpdateTime ??= DateTime.now();
if ((DateTime.now().difference(lastUpdateTime ?? DateTime.now()))
.inSeconds >=
1) {
lastUpdateTime = DateTime.now();
speed = (count - (lastUpdateCount ?? 0));
lastUpdateCount = count;
notifyListeners();
rustFii
.startDownload(
url: downloadUrl,
savePath: savePath,
fileName: "$fileName.downloading",
connectionCount: 10)
.listen((event) async {
dPrint(
"id == ${event.id} p ==${event.progress} t==${event.total} s==${event.speed} st==${event.status}");
downloadTaskId = event.id;
count = event.progress;
if (event.total != 0) {
total = event.total;
}
this.count = count;
this.total = total;
progress = count / total * 100;
if (count == total) {
isInMerging = true;
speed = event.speed;
if (total != null && total != 0 && event.progress != 0) {
progress = (event.progress / total!) * 100;
}
notifyListeners();
if (progress != null &&
progress != 0 &&
event.status == const MyDownloaderStatus.noStart()) {
Navigator.pop(context!, "cancel");
return;
}
if (event.status == const MyDownloaderStatus.finished()) {
count = total;
isInMerging = true;
notifyListeners();
await File(downloaderSavePath)
.rename(downloaderSavePath.replaceAll(".downloading", ""));
final bsonFile = File("$downloaderSavePath.bson");
if (await bsonFile.exists()) {
bsonFile.delete();
}
Navigator.pop(context!, "$savePath\\$fileName");
}
});
if (r.statusCode == 200) {
Navigator.pop(context!, savePath);
}
} catch (e) {
if (e is DioException && e.type != DioExceptionType.cancel) {
Navigator.pop(context!, e);
}
Navigator.pop(context!, e);
}
}
doCancel() {
try {
downloadCancelToken?.cancel();
downloadCancelToken = null;
if (downloadTaskId != null) {
rustFii.cancelDownload(id: downloadTaskId!);
}
} catch (_) {}
Navigator.pop(context!, "cancel");
}
}

View File

@ -333,7 +333,7 @@ class ToolsUIModel extends BaseUIModel {
}
Future<void> _downloadP4k() async {
final downloadUrl = AppConf.networkVersionData?.p4kDownloadUrl;
var downloadUrl = AppConf.networkVersionData?.p4kDownloadUrl;
if (downloadUrl == null || downloadUrl.isEmpty) {
showToast(context!, "该功能维护中,请稍后再试!");
return;