handleDownload method
- BuildContext context
Handles the download and decryption of files from the POD.
Implementation
Future<void> handleDownload(BuildContext context) async {
if (state.remoteFileName == null || state.currentPath == null) return;
try {
state = state.copyWith(downloadInProgress: true, downloadDone: false);
// Let user choose where to save the file.
String? outputFile = await FilePicker.platform.saveFile(
dialogTitle: 'Save file as:',
fileName: state.cleanFileName ??
state.remoteFileName?.replaceAll('.enc.ttl', ''),
);
if (outputFile == null) {
state = state.copyWith(downloadInProgress: false);
return;
}
final baseDir = basePath;
final relativePath = state.currentPath == baseDir
? '$baseDir/${state.remoteFileName}'
: '${state.currentPath}/${state.remoteFileName}';
// debugPrint('Attempting to download from path: $relativePath');
if (!context.mounted) return;
await getKeyFromUserIfRequired(
context,
const Text('Please enter your security key to download the file'),
);
if (!context.mounted) return;
final fileContent = await readPod(
relativePath,
context,
const Text('Downloading'),
);
if (!context.mounted) return;
if (fileContent == SolidFunctionCallStatus.fail.toString() ||
fileContent == SolidFunctionCallStatus.notLoggedIn.toString()) {
throw Exception(
'Download failed - please check your connection and permissions',
);
}
await saveDecryptedContent(fileContent, outputFile);
state = state.copyWith(downloadDone: true, downloadInProgress: false);
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('File downloaded successfully'),
backgroundColor: Theme.of(context).colorScheme.tertiary,
),
);
}
} catch (e) {
if (context.mounted) {
showAlert(context, 'Download error: ${e.toString()}');
debugPrint('Download error: $e');
}
state = state.copyWith(downloadInProgress: false);
}
}