mirror of
https://github.com/venera-app/venera.git
synced 2025-09-27 15:57:25 +00:00
add onLoadFailed to imageLoadingConfig
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_qjs/flutter_qjs.dart';
|
||||
import 'package:venera/foundation/cache_manager.dart';
|
||||
import 'package:venera/foundation/comic_source/comic_source.dart';
|
||||
import 'package:venera/foundation/consts.dart';
|
||||
@@ -83,16 +84,33 @@ class ImageDownloader {
|
||||
);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> Function()? onLoadFailed;
|
||||
|
||||
var configs = <String, dynamic>{};
|
||||
if (sourceKey != null) {
|
||||
var comicSource = ComicSource.find(sourceKey);
|
||||
configs = (await comicSource!.getImageLoadingConfig
|
||||
?.call(imageKey, cid, eid)) ?? {};
|
||||
?.call(imageKey, cid, eid)) ??
|
||||
{};
|
||||
}
|
||||
var retryLimit = 5;
|
||||
while (true) {
|
||||
try {
|
||||
configs['headers'] ??= {
|
||||
'user-agent': webUA,
|
||||
};
|
||||
|
||||
if (configs['onLoadFailed'] is JSInvokable) {
|
||||
onLoadFailed = () async {
|
||||
dynamic result = configs['onLoadFailed']();
|
||||
if (result is Future) {
|
||||
result = await result;
|
||||
}
|
||||
if (result is! Map<String, dynamic>) return null;
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
var dio = AppDio(BaseOptions(
|
||||
headers: configs['headers'],
|
||||
method: configs['method'] ?? 'GET',
|
||||
@@ -138,6 +156,20 @@ class ImageDownloader {
|
||||
totalBytes: data.length,
|
||||
imageBytes: data,
|
||||
);
|
||||
return;
|
||||
} catch (e) {
|
||||
if(retryLimit < 0 || onLoadFailed == null) {
|
||||
rethrow;
|
||||
}
|
||||
var newConfig = await onLoadFailed();
|
||||
onLoadFailed = null;
|
||||
if(newConfig == null) {
|
||||
rethrow;
|
||||
}
|
||||
configs = newConfig;
|
||||
retryLimit--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user