Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the method of get file extension. #475

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flutter_cache_manager/example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ subprojects {

tasks.register("clean", Delete) {
delete rootProject.buildDir
}
}
2 changes: 1 addition & 1 deletion flutter_cache_manager/example/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pluginManagement {
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
id "org.jetbrains.kotlin.android" version "1.7.10" apply false
id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}

include ":app"
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> _isImage(File file) async {
if (!await file.exists()) {
debugPrint('file is not found: ${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 isImage = await _isImage(originalFile.file);
final originalFileName = originalFile.file.path;
final fileExtension = originalFileName.split('.').last;
if (!supportedFileNames.contains(fileExtension)) {
if (!isImage) {
return originalFile;
}

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