download method
void
download(
{ - String? url,
- File? file,
- void onReceiveProgress(
- int received,
- int total,
- bool failed
)?,
- void onDone()?,
})
override
Implementation
@override
void download({
String? url,
File? file,
void Function(int received, int total, bool failed)? onReceiveProgress,
void Function()? onDone,
}) async {
if (status != UpgradeStatus.available) {
debugPrint("[UpgradeManager] The update is not available.");
return;
}
final uri = Uri.parse(url ?? fileURL);
state.updateUpgradeStatus(status: UpgradeStatus.downloading);
var received = 0;
var contentLength = 0;
try {
final StreamedResponse response = await Client().send(Request('GET', uri));
contentLength = response.contentLength ?? 1;
file ??= await defaultFileLocation(uri.pathSegments.last);
final sink = file.openWrite();
await response.stream.map((stream) {
received += stream.length;
onReceiveProgress?.call(received, contentLength, false);
return stream;
}).pipe(sink);
sink.close();
filePath = file.absolute.path;
state.updateUpgradeStatus(status: UpgradeStatus.readyToInstall);
onReceiveProgress?.call(contentLength, contentLength, false);
onDone?.call();
} catch (e) {
onReceiveProgress?.call(received, contentLength, true);
state.updateUpgradeStatus(status: UpgradeStatus.error);
debugPrint("[UpgradeManager] Downloading Error: ${e.toString()}");
}
}