download method

Future<int> download(
  1. String url,
  2. String savePath, {
  3. dynamic onReceiveProgress(
    1. int total,
    2. int received,
    3. int chunkSize
    )?,
  4. CustomCancelToken? cancelToken,
  5. bool deleteIfExist = false,
  6. Map<String, dynamic>? headers,
  7. int? startByte,
})

Implementation

Future<int> download(
  String url,
  String savePath, {
  Function(int total, int received, int chunkSize)? onReceiveProgress,
  CustomCancelToken? cancelToken,
  bool deleteIfExist = false,
  Map<String, dynamic>? headers,
  int? startByte,
}) async {
  if (deleteIfExist) {
    File file = File(savePath);
    if (file.existsSync()) {
      file.deleteSync();
    }
  }

  Completer<int> completer = Completer<int>();
  Uri uri = Uri.parse(url);
  HttpClient httpClient = HttpClient();
  HttpClientRequest request = await httpClient.getUrl(uri);
  headers?.forEach((key, value) {
    request.headers.add(key, value);
  });
  HttpClientResponse response = await request.close();
  int received = 0;
  int length =
      int.parse(response.headers.value(HttpHeaders.contentLengthHeader)!);
  var raf = await File(savePath).open(mode: FileMode.append);
  if (received == length) {
    raf.closeSync();
    completer.complete(received);
  }
  late StreamSubscription responseSubscription;
  responseSubscription = response.listen((chunk) {
    if (cancelToken != null) {
      if (cancelToken.isCancelled) {
        raf.closeSync();
        responseSubscription.cancel();
        completer.complete(received);
        // throw Exception('Cancelled');
        return;
      }
    }
    received += chunk.length;
    raf.writeFromSync(chunk);
    if (onReceiveProgress != null) {
      onReceiveProgress(received, length, chunk.length);
    }

    if (received == length) {
      raf.closeSync();
      completer.complete(received);
      responseSubscription.cancel();
    }
  });

  return completer.future;
}