downloadFile function
Downloads a file, saves it with a unique name, and caches it.
Implementation
Future<File> downloadFile(
bool showLog,
String url,
String extFile,
String folder,
int ttl, {
Dio? dio,
}) async {
dio ??= Dio();
if (showLog) {
Logger.log('📥 Downloading: $url');
}
try {
final response = await dio.get<List<int>>(
url,
options: Options(
responseType: ResponseType.bytes,
followRedirects: true,
),
);
// Save file to cache
File cachedFile = await saveToCache(
showLog,
url,
response.data!,
extFile,
folder,
);
if (showLog) {
Logger.log('✅ File saved: ${cachedFile.path}');
}
return cachedFile;
} catch (e) {
if (showLog) {
Logger.error('❌ Failed to download: $e');
}
throw Exception("Failed to download file: $e");
}
}