diff --git a/lib/utils/cache/cached_image.dart b/lib/utils/cache/cached_image.dart index b0f25a00..9ed89b05 100644 --- a/lib/utils/cache/cached_image.dart +++ b/lib/utils/cache/cached_image.dart @@ -4,6 +4,7 @@ import 'dart:isolate'; import 'dart:ui' as ui; import 'dart:ui'; +import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/widgets.dart'; @@ -50,7 +51,26 @@ class CachedImage extends ImageProvider implements CacheKey { ); } - static final HttpClient _httpClient = HttpClient(); + static final _dio = Dio(); + + static Future _loadDataFromNetwork( + String url, { + Map? headers, + }) async { + final response = await _dio.get>( + url, + options: Options( + responseType: ResponseType.bytes, + headers: { + // FIXME: 使用真实的UA + 'User-Agent': + 'Mozilla/5.0 (Linux; Android 10; Redmi K30 Pro) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36', + if (headers != null) ...headers, + }, + ), + ); + return Uint8List.fromList(response.data!); + } Future _loadAsync( CachedImage key, @@ -70,22 +90,7 @@ class CachedImage extends ImageProvider implements CacheKey { } //request network source - final resolved = Uri.base.resolve(key.url); - final request = await _httpClient.getUrl(resolved); - headers?.forEach((String name, String value) { - request.headers.add(name, value); - }); - final response = await request.close(); - if (response.statusCode != HttpStatus.ok) { - throw Exception( - 'HTTP request failed, statusCode: ${response.statusCode}, $resolved', - ); - } - - final bytes = await consolidateHttpClientResponseBytes(response); - if (bytes.lengthInBytes == 0) { - throw Exception('NetworkImage is an empty file: $resolved'); - } + final bytes = await _loadDataFromNetwork(url, headers: headers); //save image to cache await ImageFileCache.instance.update(key, bytes); diff --git a/macos/Podfile.lock b/macos/Podfile.lock index fa724137..54d3d8ab 100644 --- a/macos/Podfile.lock +++ b/macos/Podfile.lock @@ -64,4 +64,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 0d3963a09fc94f580682bd88480486da345dc3f0 -COCOAPODS: 1.12.1 +COCOAPODS: 1.13.0 diff --git a/pubspec.lock b/pubspec.lock index 70e5f2e8..17c98182 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -219,7 +219,7 @@ packages: source: git version: "0.4.3" dio: - dependency: transitive + dependency: "direct main" description: name: dio sha256: "347d56c26d63519552ef9a569f2a593dda99a81fdbdff13c584b7197cfe05059" diff --git a/pubspec.yaml b/pubspec.yaml index 33dc8aea..5c4d9e01 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -68,6 +68,7 @@ dependencies: qr_flutter: ^4.1.0 system_clock: ^2.0.0 handy_window: ^0.3.1 + dio: any dev_dependencies: flutter_test: