From a131179c7108d0cc23e88e31b7347a4535bfd1b8 Mon Sep 17 00:00:00 2001 From: aamir-csol Date: Wed, 1 Apr 2026 14:33:16 +0300 Subject: [PATCH] VHR-279 --- .../mowadhafhi/mowadhafhi_hr_request.dart | 63 +++++++++++++++---- 1 file changed, 52 insertions(+), 11 deletions(-) diff --git a/lib/ui/screens/mowadhafhi/mowadhafhi_hr_request.dart b/lib/ui/screens/mowadhafhi/mowadhafhi_hr_request.dart index ec12c51..b358843 100644 --- a/lib/ui/screens/mowadhafhi/mowadhafhi_hr_request.dart +++ b/lib/ui/screens/mowadhafhi/mowadhafhi_hr_request.dart @@ -192,35 +192,76 @@ class _MowadhafhiHRRequestState extends State { title.toText16().expanded, 6.width, SimpleButton(LocaleKeys.add.tr(), () async { - FilePickerResult? result = await FilePicker.platform.pickFiles(allowMultiple: true); + FilePickerResult? result = await FilePicker.platform.pickFiles( + allowMultiple: true, + ); if (result != null) { // Maximum file size: 2 MB (in bytes) - const int maxFileSizeInBytes = 2 * 1024 * 1024; // 2 MB + const int maxFileSizeInBytes = 2 * 1024 * 1024; // 2 MB = 2097152 bytes List newFiles = []; List oversizedFiles = []; - for (String? path in result.paths) { - if (path != null) { - File file = File(path); - int fileSize = await file.length(); + for (PlatformFile platformFile in result.files) { + if (platformFile.path != null) { + File file = File(platformFile.path!); - if (fileSize <= maxFileSizeInBytes) { + // Get file size - check multiple sources for iOS compatibility + int fileSize = 0; + + // Method 1: Check platformFile.size first + if (platformFile.size > 0) { + fileSize = platformFile.size; + debugPrint('Using platformFile.size: $fileSize bytes'); + } + + // Method 2: Always verify with file.length() as well + try { + int fileLengthSize = await file.length(); + debugPrint('Using file.length(): $fileLengthSize bytes'); + // Use the maximum of both to ensure we catch the real size + if (fileLengthSize > fileSize) { + fileSize = fileLengthSize; + debugPrint('Using larger value from file.length()'); + } + } catch (e) { + debugPrint('Error getting file.length(): $e'); + // If file.length() fails, rely on platformFile.size + if (fileSize == 0) { + debugPrint('Cannot determine file size, rejecting file: ${platformFile.name}'); + oversizedFiles.add('${platformFile.name} (unknown size)'); + continue; + } + } + + double fileSizeMB = fileSize / (1024 * 1024); + debugPrint('Final file size - ${platformFile.name}: $fileSize bytes (${fileSizeMB.toStringAsFixed(2)} MB)'); + + // STRICT validation: Only accept files that are 2 MB or less + if (fileSize > 0 && fileSize <= maxFileSizeInBytes) { + debugPrint('✓ File accepted: ${platformFile.name}'); newFiles.add(file); + } else if (fileSize > maxFileSizeInBytes) { + debugPrint('✗ File REJECTED (too large): ${platformFile.name} - ${fileSizeMB.toStringAsFixed(2)} MB'); + oversizedFiles.add('${platformFile.name} (${fileSizeMB.toStringAsFixed(2)} MB)'); } else { - String fileName = path.split('/').last; - oversizedFiles.add(fileName); + debugPrint('✗ File REJECTED (invalid size): ${platformFile.name}'); + oversizedFiles.add('${platformFile.name} (invalid)'); } } } + // Only add valid files if (newFiles.isNotEmpty) { attachmentFiles = attachmentFiles + newFiles; attachmentFiles = attachmentFiles.toSet().toList(); + debugPrint('Total files attached: ${attachmentFiles.length}'); } + // Show error message for rejected files if (oversizedFiles.isNotEmpty) { - // String fileList = oversizedFiles.join(', '); - Utils.showToast('File exceeds 2 MB limit'); + String fileList = oversizedFiles.join('\n'); + Utils.showToast('Max 2MB File allowed.'); + debugPrint('Total rejected files: ${oversizedFiles.length}'); } setState(() {});