Skip to content

Commit

Permalink
fix(*): Fix failed to load br compressed image from web request
Browse files Browse the repository at this point in the history
Some contents (images) in web response is compressed in br format and
dio has no support of that format so the response data is considered
as invalid image data.

This commit added a package to decompress response data when is in br
format.
  • Loading branch information
realth000 committed Aug 29, 2024
1 parent 5a7d785 commit a9da05a
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- app:修复先前升级flutter版本导致的默认配色问题,现在使用默认的蓝色作为色调。
- app:修正各类边距值不是Material 3取值的问题。
- app:修复一些情况下,网络连接报错但UI没有更新的问题。
- app:修复部分图片无法加载的问题。
- 翻译:修复部分错误翻译。
- 帖子:修复部分签名档存在样式溢出的用户的所在楼层无法回复和评分的问题。
- 帖子:修复帖子评分中头像无法加载时不显示且不断重试的问题。
Expand Down
7 changes: 3 additions & 4 deletions lib/features/cache/repository/image_cache_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ final class ImageCacheRepository with LoggerMixin {
handle(err);
throw err;
}
final imageData = respEither.unwrap().data as List<int>;
await _imageCacheProvider.updateCache(url, imageData);
_controller
.add(ImageCacheSuccessResponse(url, Uint8List.fromList(imageData)));
final imageData = respEither.unwrap().data as Uint8List;
await _imageCacheProvider.updateCache(url, Uint8List.fromList(imageData));
_controller.add(ImageCacheSuccessResponse(url, imageData));
} catch (e) {
_controller.add(ImageCacheFailedResponse(url));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ final class ImageCacheProvider with LoggerMixin {
/// Update image cached file.
///
/// Update cache file and info in database.
Future<void> updateCache(String imageUrl, List<int> imageData) async {
Future<void> updateCache(String imageUrl, Uint8List imageData) async {
final fileName = imageUrl.fileNameV5();

// Update image cache info to database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io' if (dart.libaray.js) 'package:web/web.dart';
import 'package:cookie_jar/cookie_jar.dart';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:dio_brotli_transformer/dio_brotli_transformer.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
import 'package:tsdm_client/exceptions/exceptions.dart';
import 'package:tsdm_client/features/settings/repositories/settings_repository.dart';
Expand Down Expand Up @@ -95,6 +96,8 @@ final class NetClientProvider with LoggerMixin {
},
);
d.interceptors.add(_ErrorHandler());
// decode br content-type.
d.transformer = DioBrotliTransformer();
}

return NetClientProvider._(d);
Expand All @@ -104,6 +107,8 @@ final class NetClientProvider with LoggerMixin {
factory NetClientProvider.buildNoCookie({Dio? dio}) {
final d = dio ?? getIt.get<SettingsRepository>().buildDefaultDio();
d.interceptors.add(_ErrorHandler());
// decode br content-type.
d.transformer = DioBrotliTransformer();
return NetClientProvider._(d);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/cached_image/cached_image_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ final class CachedImageProvider extends ImageProvider<CachedImageProvider>
error('failed to get image from $imageUrl, code=${resp.statusCode}');
return Uint8List(0);
}
final imageData = resp.data as List<int>;
final imageData = resp.data as Uint8List;

// Make cache.
await getIt.get<ImageCacheProvider>().updateCache(imageUrl, imageData);
Expand Down
16 changes: 16 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.1"
brotli:
dependency: transitive
description:
name: brotli
sha256: "7f891558ed779aab2bed874f0a36b8123f9ff3f19cf6efbee89e18ed294945ae"
url: "https://pub.dev"
source: hosted
version: "0.6.0"
build:
dependency: transitive
description:
Expand Down Expand Up @@ -327,6 +335,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.6.0"
dio_brotli_transformer:
dependency: "direct main"
description:
name: dio_brotli_transformer
sha256: "2ceeaa997a6c2faeda7e1ad1765e87316c9be853fb9892fd0c80c71ca6788e92"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
dio_cookie_manager:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies:
dart_mappable: 4.2.2
device_info_plus: ^10.1.2
dio: ^5.6.0
dio_brotli_transformer: ^2.0.0
dio_cookie_manager: ^3.1.1
drift: ^2.19.1+1
# For drift
Expand Down

0 comments on commit a9da05a

Please sign in to comment.