download method

void download()

Download from sync IP

Implementation

void download() async {
  // Get path for distro filesystem
  String? syncIP = prefs.getString('SyncIP');
  if (syncIP == null) {
    Notify.message('syncipnotset-text'.i18n(), loading: false);
    return;
  }
  Notify.message('${'shuttingdownwsl-text'.i18n()}...', loading: true);

  final vhdxPath = getInstancePath(distroName).file('ext4.vhdx');
  final vhdxPathTmp = getInstancePath(distroName).file('ext4.vhdx.tmp');
  final vhdxPathOld = getInstancePath(distroName).file('ext4.vhdx.old');

  // Shutdown WSL
  await WSLApi().shutdown();
  Notify.message('${'connectingtoip-text'.i18n()}: "$syncIP"...',
      loading: true);

  // Download using chunks
  var response = ChunkedDownloader(
      url: 'http://$syncIP:59132/ext4.vhdx',
      saveFilePath: vhdxPath,
      onProgress: (progress, total, speed) {
        String rec = (progress / 1024 / 1024).toStringAsFixed(2);
        String tot = (total / 1024 / 1024).toStringAsFixed(2);
        Notify.message(
            '${'downloading-text'.i18n()} $distroName, $rec MB / $tot MB',
            loading: true);
        if (progress == total) {
          Notify.message('${'downloaded-text'.i18n()} $distroName');
          File oldFile = File(vhdxPath);
          oldFile.rename(vhdxPathOld);
          File file = File(vhdxPathTmp);
          file.rename(vhdxPath);
        }
      },
      onError: (error) {
        Notify.message(
            '${'errordownloading-text'.i18n()} $distroName: $error',
            loading: false);
      });

  // Await download
  while (!response.done) {
    await Future.delayed(const Duration(milliseconds: 500));
  }
}