download method

Future<void> download(
  1. String url,
  2. String savePath,
  3. dynamic cancelToken,
  4. String thead,
  5. String tbody, {
  6. dynamic forceDownload = false,
})

Implementation

Future<void> download(String url,
 String savePath,
  cancelToken,
String thead,
String tbody,
    {forceDownload = false}) async {

  late String partialFilePath;
  late File partialFile;
  try {
    var task = getDownload(url);

    if (task == null || task.status.value == DownloadStatus.canceled) {
      return;
    }
    setStatus(task, DownloadStatus.downloading);

    if (kDebugMode) {
      print(url);
    }
    var file = File(savePath.toString());
    partialFilePath = savePath + partialExtension;
    partialFile = File(partialFilePath);

    var fileExist = await file.exists();
    var partialFileExist = await partialFile.exists();

    if (fileExist) {
      int fileIndex = 1;
      String newFilePath;
      String baseFilePath = savePath.substring(0, savePath.lastIndexOf('.'));
      String fileExtension = savePath.substring(savePath.lastIndexOf('.'));
      do {
        newFilePath = '$baseFilePath($fileIndex)$fileExtension';
        fileIndex++;
      } while (await File(newFilePath).exists());
      if (kDebugMode) {
        print("File Exists, saving as $newFilePath");
      }
      await File(savePath).copy(newFilePath);
       if(platforms.Platform.isWindows || platforms.Platform.isMacOS){
showWithSmallImage(thead,tbody,savePath);
        }else{
      NotificationUtils().createCustomNotificationWithActionButtons(
    title: thead,
    body: tbody,
    payload: "no",
  );
        }
      setStatus(task, DownloadStatus.completed);
    } else if (partialFileExist) {
      if (kDebugMode) {
        print("Partial File Exists");
      }
      var partialFileLength = await partialFile.length();

      var response = await dio.download(url, partialFilePath + tempExtension,
          onReceiveProgress: createCallback(url, partialFileLength),
          options: Options(
            headers: {HttpHeaders.rangeHeader: 'bytes=$partialFileLength-'},
          ),
          cancelToken: cancelToken,
          deleteOnError: true);

      if (response.statusCode == HttpStatus.partialContent) {
        var ioSink = partialFile.openWrite(mode: FileMode.writeOnlyAppend);
        // ignore: no_leading_underscores_for_local_identifiers
        var f = File(partialFilePath + tempExtension);
        await ioSink.addStream(f.openRead());
        await f.delete();
        await ioSink.close();
        await partialFile.rename(savePath);

        setStatus(task, DownloadStatus.completed);
      }
    } else {
      var response = await dio.download(url, partialFilePath,
          onReceiveProgress: createCallback(url, 0),
          cancelToken: cancelToken,
          deleteOnError: false);

      if (response.statusCode == HttpStatus.ok) {

        await partialFile.rename(savePath);
        if (kDebugMode) {
          print(fixPath(savePath));
        }
        if(platforms.Platform.isWindows || platforms.Platform.isMacOS){
showWithSmallImage(thead,tbody,savePath);
        }else{
       if (kDebugMode) {
         print("object")   ;
       }
        }

        setStatus(task, DownloadStatus.completed);


      }
    }
  } catch (e) {
    var task = getDownload(url)!;
    if (task.status.value != DownloadStatus.canceled &&
        task.status.value != DownloadStatus.paused) {
      setStatus(task, DownloadStatus.failed);
      runningTasks--;

      if (_queue.isNotEmpty) {
        _startExecution();
      }
      rethrow;
    } else if (task.status.value == DownloadStatus.paused) {
      final ioSink = partialFile.openWrite(mode: FileMode.writeOnlyAppend);
      final f = File(partialFilePath + tempExtension);
      if (await f.exists()) {
        await ioSink.addStream(f.openRead());
      }
      await ioSink.close();
    }
  }

  runningTasks--;

  if (_queue.isNotEmpty) {
    _startExecution();
  }
}