|
|
|
@ -192,35 +192,76 @@ class _MowadhafhiHRRequestState extends State<MowadhafhiHRRequest> {
|
|
|
|
title.toText16().expanded,
|
|
|
|
title.toText16().expanded,
|
|
|
|
6.width,
|
|
|
|
6.width,
|
|
|
|
SimpleButton(LocaleKeys.add.tr(), () async {
|
|
|
|
SimpleButton(LocaleKeys.add.tr(), () async {
|
|
|
|
FilePickerResult? result = await FilePicker.platform.pickFiles(allowMultiple: true);
|
|
|
|
FilePickerResult? result = await FilePicker.platform.pickFiles(
|
|
|
|
|
|
|
|
allowMultiple: true,
|
|
|
|
|
|
|
|
);
|
|
|
|
if (result != null) {
|
|
|
|
if (result != null) {
|
|
|
|
// Maximum file size: 2 MB (in bytes)
|
|
|
|
// 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<File> newFiles = [];
|
|
|
|
List<File> newFiles = [];
|
|
|
|
List<String> oversizedFiles = [];
|
|
|
|
List<String> oversizedFiles = [];
|
|
|
|
|
|
|
|
|
|
|
|
for (String? path in result.paths) {
|
|
|
|
for (PlatformFile platformFile in result.files) {
|
|
|
|
if (path != null) {
|
|
|
|
if (platformFile.path != null) {
|
|
|
|
File file = File(path);
|
|
|
|
File file = File(platformFile.path!);
|
|
|
|
int fileSize = await file.length();
|
|
|
|
|
|
|
|
|
|
|
|
// 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)');
|
|
|
|
|
|
|
|
|
|
|
|
if (fileSize <= maxFileSizeInBytes) {
|
|
|
|
// STRICT validation: Only accept files that are 2 MB or less
|
|
|
|
|
|
|
|
if (fileSize > 0 && fileSize <= maxFileSizeInBytes) {
|
|
|
|
|
|
|
|
debugPrint('✓ File accepted: ${platformFile.name}');
|
|
|
|
newFiles.add(file);
|
|
|
|
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 {
|
|
|
|
} else {
|
|
|
|
String fileName = path.split('/').last;
|
|
|
|
debugPrint('✗ File REJECTED (invalid size): ${platformFile.name}');
|
|
|
|
oversizedFiles.add(fileName);
|
|
|
|
oversizedFiles.add('${platformFile.name} (invalid)');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Only add valid files
|
|
|
|
if (newFiles.isNotEmpty) {
|
|
|
|
if (newFiles.isNotEmpty) {
|
|
|
|
attachmentFiles = attachmentFiles + newFiles;
|
|
|
|
attachmentFiles = attachmentFiles + newFiles;
|
|
|
|
attachmentFiles = attachmentFiles.toSet().toList();
|
|
|
|
attachmentFiles = attachmentFiles.toSet().toList();
|
|
|
|
|
|
|
|
debugPrint('Total files attached: ${attachmentFiles.length}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Show error message for rejected files
|
|
|
|
if (oversizedFiles.isNotEmpty) {
|
|
|
|
if (oversizedFiles.isNotEmpty) {
|
|
|
|
// String fileList = oversizedFiles.join(', ');
|
|
|
|
String fileList = oversizedFiles.join('\n');
|
|
|
|
Utils.showToast('File exceeds 2 MB limit');
|
|
|
|
Utils.showToast('Max 2MB File allowed.');
|
|
|
|
|
|
|
|
debugPrint('Total rejected files: ${oversizedFiles.length}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setState(() {});
|
|
|
|
setState(() {});
|
|
|
|
|