downloadFile method
Download file from URL
Implementation
Future<File> downloadFile(String url, {String? filename}) async {
try {
final uri = Uri.parse(url);
final client = HttpClient();
final request = await client.getUrl(uri);
final response = await request.close();
if (response.statusCode != 200) {
throw WalletException(
'Failed to download file: HTTP ${response.statusCode}');
}
final downloadFilename = filename ?? _generateFilenameFromUrl(url);
final outputFile = await createOutputFile(downloadFilename);
final sink = outputFile.openWrite();
await response.pipe(sink);
await sink.close();
client.close();
return outputFile;
} catch (e) {
throw WalletException('Failed to download file from $url: $e');
}
}