Skip to content

Commit b89ea3d

Browse files
authored
fix: image and lottie didn't use rhttp (#1666)
1 parent 1caeb69 commit b89ea3d

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

lib/utils/cache_client.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:http/io_client.dart';
66
import 'package:mixin_logger/mixin_logger.dart';
77
import 'package:path/path.dart' as p;
88
import 'package:path_provider/path_provider.dart';
9+
import 'package:rhttp/rhttp.dart';
910

1011
import '../widgets/cache_image.dart';
1112
import 'proxy.dart';
@@ -15,7 +16,7 @@ class CacheClient extends BaseClient {
1516
if (proxyConfig != null) {
1617
_client = IOClient(HttpClient()..setProxy(proxyConfig));
1718
} else {
18-
_client = Client();
19+
_client = RhttpCompatibleClient.createSync();
1920
}
2021
}
2122

lib/widgets/cache_image.dart

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:flutter/widgets.dart';
1010
import 'package:http_client_helper/http_client_helper.dart';
1111
import 'package:path/path.dart';
1212
import 'package:path_provider/path_provider.dart';
13+
import 'package:rhttp/rhttp.dart';
1314

1415
import '../utils/extension/extension.dart';
1516
import '../utils/logger.dart';
@@ -548,22 +549,42 @@ class MixinNetworkImageProvider
548549
) async {
549550
try {
550551
final resolved = Uri.base.resolve(key.url);
551-
final response = await _tryGetResponse(resolved, key.proxyConfig);
552-
if (response == null || response.statusCode != HttpStatus.ok) {
553-
return null;
552+
final Uint8List bytes;
553+
554+
if (key.proxyConfig != null) {
555+
final response = await _tryGetResponse(resolved, key.proxyConfig);
556+
if (response == null || response.statusCode != HttpStatus.ok) {
557+
return null;
558+
}
559+
560+
bytes = await consolidateHttpClientResponseBytes(
561+
response,
562+
onBytesReceived: chunkEvents != null
563+
? (int cumulative, int? total) {
564+
chunkEvents.add(ImageChunkEvent(
565+
cumulativeBytesLoaded: cumulative,
566+
expectedTotalBytes: total,
567+
));
568+
}
569+
: null,
570+
);
571+
} else {
572+
d('no proxy: load image from network: $resolved');
573+
final response = await Rhttp.getBytes(resolved.toString(),
574+
onReceiveProgress: chunkEvents != null
575+
? (int cumulative, int total) {
576+
chunkEvents.add(ImageChunkEvent(
577+
cumulativeBytesLoaded: cumulative,
578+
expectedTotalBytes: total,
579+
));
580+
}
581+
: null);
582+
if (response.statusCode != HttpStatus.ok) {
583+
return null;
584+
}
585+
bytes = response.body;
554586
}
555587

556-
final bytes = await consolidateHttpClientResponseBytes(
557-
response,
558-
onBytesReceived: chunkEvents != null
559-
? (int cumulative, int? total) {
560-
chunkEvents.add(ImageChunkEvent(
561-
cumulativeBytesLoaded: cumulative,
562-
expectedTotalBytes: total,
563-
));
564-
}
565-
: null,
566-
);
567588
if (bytes.lengthInBytes == 0) {
568589
return Future<Uint8List>.error(
569590
StateError('NetworkImage is an empty file: $resolved'));

0 commit comments

Comments
 (0)