Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
cache image add user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
boyan01 committed Nov 12, 2023
1 parent f3a039d commit 5328b52
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
39 changes: 22 additions & 17 deletions lib/utils/cache/cached_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -50,7 +51,26 @@ class CachedImage extends ImageProvider<CachedImage> implements CacheKey {
);
}

static final HttpClient _httpClient = HttpClient();
static final _dio = Dio();

static Future<Uint8List> _loadDataFromNetwork(
String url, {
Map<String, String>? headers,
}) async {
final response = await _dio.get<List<int>>(
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<ui.Codec> _loadAsync(
CachedImage key,
Expand All @@ -70,22 +90,7 @@ class CachedImage extends ImageProvider<CachedImage> 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);
Expand Down
2 changes: 1 addition & 1 deletion macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 0d3963a09fc94f580682bd88480486da345dc3f0

COCOAPODS: 1.12.1
COCOAPODS: 1.13.0
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ packages:
source: git
version: "0.4.3"
dio:
dependency: transitive
dependency: "direct main"
description:
name: dio
sha256: "347d56c26d63519552ef9a569f2a593dda99a81fdbdff13c584b7197cfe05059"
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 5328b52

Please sign in to comment.