validateFile static method
Implementation
static Future<bool> validateFile(
File? file, Function(String error) onError) async {
if (file == null) {
onError('File is null');
return false;
}
final mimeType = lookupMimeType(file.path);
final fileSize = await file.length();
if (mimeType != null) {
if (mimeType.startsWith('audio/')) {
if (fileSize <= 16 * 1024 * 1024) {
return true; // Valid audio file
} else {
onError("Audio file exceeds the size limit (16 MB)");
}
}
if (Constant.documentFileTypes()
.any((type) => mimeType.startsWith(type))) {
if (fileSize <= 20 * 1024 * 1024) {
return true; // Valid text file
} else {
onError("Text file exceeds the size limit (20 MB)");
}
} else if (mimeType.startsWith('image/')) {
if (fileSize <= 5 * 1024 * 1024) {
return true; // Valid image file
} else {
onError("Image file exceeds the size limit (5 MB)");
}
} else if (mimeType.startsWith('video/')) {
if (fileSize <= 16 * 1024 * 1024) {
return true; // Valid video file
} else {
onError("Video file exceeds the size limit (16 MB)");
}
} else {
onError("Unsupported file type: $mimeType");
}
} else {
onError('Failed to determine MIME type for "${file.path}"');
}
return false; // File is invalid
}