67 lines
1.9 KiB
Dart
67 lines
1.9 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart' show instantiateImageCodec;
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
/// Utility to validate image files
|
|
class ImageValidator {
|
|
/// Validate if a file is a valid image by trying to decode it
|
|
static Future<bool> validateImageFile(String filePath) async {
|
|
try {
|
|
final file = File(filePath);
|
|
if (!await file.exists()) {
|
|
print('File does not exist: $filePath');
|
|
return false;
|
|
}
|
|
|
|
final bytes = await file.readAsBytes();
|
|
print('File size: ${bytes.length} bytes');
|
|
|
|
if (bytes.isEmpty) {
|
|
print('File is empty: $filePath');
|
|
return false;
|
|
}
|
|
|
|
// Try to decode as image
|
|
final codec = await instantiateImageCodec(bytes);
|
|
final frameInfo = await codec.getNextFrame();
|
|
final image = frameInfo.image;
|
|
|
|
print('Image dimensions: ${image.width}x${image.height}');
|
|
await image.dispose();
|
|
await codec.dispose();
|
|
|
|
return true;
|
|
} catch (e) {
|
|
print('Failed to validate image file $filePath: $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// Validate images in the documents directory
|
|
static Future<void> validateStoredImages() async {
|
|
try {
|
|
final dir = await getApplicationDocumentsDirectory();
|
|
final logoDir = Directory('${dir.path}/logos');
|
|
|
|
if (!await logoDir.exists()) {
|
|
print('Logo directory does not exist');
|
|
return;
|
|
}
|
|
|
|
final files = logoDir.listSync();
|
|
print('Found ${files.length} files in logo directory');
|
|
|
|
for (final file in files) {
|
|
if (file is File) {
|
|
print('Validating ${file.path}...');
|
|
final isValid = await validateImageFile(file.path);
|
|
print('Validation result: $isValid');
|
|
}
|
|
}
|
|
} catch (e) {
|
|
print('Error validating stored images: $e');
|
|
}
|
|
}
|
|
} |