mirror of
https://github.com/wgh136/pixes.git
synced 2025-09-27 12:57:24 +00:00
batch download
This commit is contained in:
@@ -101,16 +101,29 @@ class DownloadingTask {
|
||||
}
|
||||
imagePaths.add(path);
|
||||
_downloadingIndex++;
|
||||
_retryCount = 0;
|
||||
}
|
||||
onCompleted?.call(this);
|
||||
}
|
||||
catch(e, s) {
|
||||
error = e.toString();
|
||||
_stop = true;
|
||||
_handleError(e);
|
||||
Log.error("Download", "Download error: $e\n$s");
|
||||
}
|
||||
}
|
||||
|
||||
int _retryCount = 0;
|
||||
|
||||
void _handleError(Object error) async{
|
||||
_retryCount++;
|
||||
if(_retryCount > 3) {
|
||||
_stop = true;
|
||||
error = error.toString();
|
||||
return;
|
||||
}
|
||||
await Future.delayed(Duration(seconds: 1 << _retryCount));
|
||||
_download();
|
||||
}
|
||||
|
||||
static String _generateFilePath(Illust illust, int index, String ext) {
|
||||
final String downloadPath = appdata.settings["downloadPath"];
|
||||
String subPathPatten = appdata.settings["downloadSubPath"];
|
||||
@@ -297,4 +310,32 @@ class DownloadManager {
|
||||
''', [illustId]);
|
||||
return res.map((e) => e["path"] as String).toList();
|
||||
}
|
||||
|
||||
Future<void> batchDownload(Future<Res<List<Illust>>> request, int maxCount) async{
|
||||
List<Illust> all = [];
|
||||
String? nextUrl;
|
||||
int retryCount = 0;
|
||||
while(nextUrl != "end" && all.length < maxCount) {
|
||||
if(nextUrl != null) {
|
||||
request = Network().getIllustsWithNextUrl(nextUrl);
|
||||
}
|
||||
var res = await request;
|
||||
if(res.error) {
|
||||
retryCount++;
|
||||
if(retryCount > 3) {
|
||||
throw res.error;
|
||||
}
|
||||
await Future.delayed(Duration(seconds: 1 << retryCount));
|
||||
continue;
|
||||
}
|
||||
all.addAll(res.data);
|
||||
nextUrl = res.subData ?? "end";
|
||||
}
|
||||
int i = 0;
|
||||
for(var illust in all) {
|
||||
if(i > maxCount) return;
|
||||
addDownloadingTask(illust);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user