Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
1
  • Loading branch information
myafer committed Dec 4, 2024
1 parent 54904e4 commit 4e03901
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ mixin ImageCacheManager on BaseCacheManager {
_runningResizes.remove(resizedKey);
}

Future<bool> 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<String, Stream<FileResponse>> _runningResizes = {};

Future<FileInfo> _resizeImageFile(
Expand All @@ -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;
}

Expand Down
16 changes: 9 additions & 7 deletions flutter_cache_manager/lib/src/compat/file_service_compat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion flutter_cache_manager/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4e03901

Please sign in to comment.