Skip to content

Commit

Permalink
fix: image and lottie didn't use rhttp (#1666)
Browse files Browse the repository at this point in the history
  • Loading branch information
boyan01 authored Nov 5, 2024
1 parent 1caeb69 commit b89ea3d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/utils/cache_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:http/io_client.dart';
import 'package:mixin_logger/mixin_logger.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:rhttp/rhttp.dart';

import '../widgets/cache_image.dart';
import 'proxy.dart';
Expand All @@ -15,7 +16,7 @@ class CacheClient extends BaseClient {
if (proxyConfig != null) {
_client = IOClient(HttpClient()..setProxy(proxyConfig));
} else {
_client = Client();
_client = RhttpCompatibleClient.createSync();
}
}

Expand Down
49 changes: 35 additions & 14 deletions lib/widgets/cache_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter/widgets.dart';
import 'package:http_client_helper/http_client_helper.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:rhttp/rhttp.dart';

import '../utils/extension/extension.dart';
import '../utils/logger.dart';
Expand Down Expand Up @@ -548,22 +549,42 @@ class MixinNetworkImageProvider
) async {
try {
final resolved = Uri.base.resolve(key.url);
final response = await _tryGetResponse(resolved, key.proxyConfig);
if (response == null || response.statusCode != HttpStatus.ok) {
return null;
final Uint8List bytes;

if (key.proxyConfig != null) {
final response = await _tryGetResponse(resolved, key.proxyConfig);
if (response == null || response.statusCode != HttpStatus.ok) {
return null;
}

bytes = await consolidateHttpClientResponseBytes(
response,
onBytesReceived: chunkEvents != null
? (int cumulative, int? total) {
chunkEvents.add(ImageChunkEvent(
cumulativeBytesLoaded: cumulative,
expectedTotalBytes: total,
));
}
: null,
);
} else {
d('no proxy: load image from network: $resolved');
final response = await Rhttp.getBytes(resolved.toString(),
onReceiveProgress: chunkEvents != null
? (int cumulative, int total) {
chunkEvents.add(ImageChunkEvent(
cumulativeBytesLoaded: cumulative,
expectedTotalBytes: total,
));
}
: null);
if (response.statusCode != HttpStatus.ok) {
return null;
}
bytes = response.body;
}

final bytes = await consolidateHttpClientResponseBytes(
response,
onBytesReceived: chunkEvents != null
? (int cumulative, int? total) {
chunkEvents.add(ImageChunkEvent(
cumulativeBytesLoaded: cumulative,
expectedTotalBytes: total,
));
}
: null,
);
if (bytes.lengthInBytes == 0) {
return Future<Uint8List>.error(
StateError('NetworkImage is an empty file: $resolved'));
Expand Down

0 comments on commit b89ea3d

Please sign in to comment.