From 4e03901225fef873217913c3573cc85766e0ee67 Mon Sep 17 00:00:00 2001 From: afer <791143125@qq.com> Date: Wed, 4 Dec 2024 17:19:02 +0800 Subject: [PATCH] 1 1 --- .../cache_managers/image_cache_manager.dart | 28 ++++++++++++++++++- .../lib/src/compat/file_service_compat.dart | 16 ++++++----- flutter_cache_manager/pubspec.yaml | 2 +- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/flutter_cache_manager/lib/src/cache_managers/image_cache_manager.dart b/flutter_cache_manager/lib/src/cache_managers/image_cache_manager.dart index 614d7d9d..d0c1383a 100644 --- a/flutter_cache_manager/lib/src/cache_managers/image_cache_manager.dart +++ b/flutter_cache_manager/lib/src/cache_managers/image_cache_manager.dart @@ -61,6 +61,31 @@ mixin ImageCacheManager on BaseCacheManager { _runningResizes.remove(resizedKey); } + Future getExt(File file) async { + if (!await file.exists()) { + debugPrint('文件不存在: ${file.path}'); + return false; + } + final raf = await file.open(); + final header = await raf.read(8); + await raf.close(); + final hexString = + header.map((byte) => byte.toRadixString(16).padLeft(2, '0')).join(''); + if (hexString.startsWith('ffd8')) { + return true; // JPEG + } else if (hexString.startsWith('89504e47')) { + return true; // PNG + } else if (hexString.startsWith('00000200')) { + return true; // TGA + } else if (hexString.startsWith('00000100')) { + return true; // CUR + } else if (hexString.startsWith('00000000')) { + return true; // ICO + } + + return false; // 不支持的文件类型 + } + final Map> _runningResizes = {}; Future _resizeImageFile( @@ -69,9 +94,10 @@ mixin ImageCacheManager on BaseCacheManager { int? maxWidth, int? maxHeight, ) async { + bool isSupported = await getExt(originalFile.file); final originalFileName = originalFile.file.path; final fileExtension = originalFileName.split('.').last; - if (!supportedFileNames.contains(fileExtension)) { + if (isSupported) { return originalFile; } diff --git a/flutter_cache_manager/lib/src/compat/file_service_compat.dart b/flutter_cache_manager/lib/src/compat/file_service_compat.dart index 0df201e1..bbd7a6ff 100644 --- a/flutter_cache_manager/lib/src/compat/file_service_compat.dart +++ b/flutter_cache_manager/lib/src/compat/file_service_compat.dart @@ -63,13 +63,15 @@ class CompatFileServiceGetResponse implements FileServiceResponse { @override String get fileExtension { - var fileExtension = ''; - final contentTypeHeader = _header(HttpHeaders.contentTypeHeader); - if (contentTypeHeader != null) { - final contentType = ContentType.parse(contentTypeHeader); - fileExtension = contentType.fileExtension; - } - return fileExtension; + // var fileExtension = ''; + // final contentTypeHeader = _header(HttpHeaders.contentTypeHeader); + // if (contentTypeHeader != null) { + // final contentType = ContentType.parse(contentTypeHeader); + // fileExtension = contentType.fileExtension; + // } + // return fileExtension; + + return '.jpeg'; } @override diff --git a/flutter_cache_manager/pubspec.yaml b/flutter_cache_manager/pubspec.yaml index 4e49f688..ed849972 100644 --- a/flutter_cache_manager/pubspec.yaml +++ b/flutter_cache_manager/pubspec.yaml @@ -22,7 +22,7 @@ dependencies: uuid: ^4.4.2 dev_dependencies: - build_runner: ^2.4.12 + build_runner: ^2.4.9 flutter_lints: ^4.0.0 flutter_test: sdk: flutter