prepareDownload method Null safety

Future<bool> prepareDownload()

prepareDownload initialize the needed parameters that are needed for the download operation

Implementation

Future<bool> prepareDownload() async {
  String datetime = Utils.generateDatetime();
  final Completer<bool> preparationCompleter = Completer();

  AWSSigV4Signer signer = AWSSigV4Signer(
      region: config.credentailsConfig.region,
      accessKey: config.credentailsConfig.accessKey,
      secretKey: config.credentailsConfig.secretKey,
      hostEndpoint: config.credentailsConfig
          .host); // Create the AWS Signer to be used to sign the AWS Request

  final authorizationHeader = signer.buildAuthorizationHeader(
      'GET', '/${config.url}', {}, Utils.trimString(datetime),
      requestPayload: '');

  _header = signer.headers;
  _header['Authorization'] = authorizationHeader;

  if (config.resumeDownload) {
    File fileTobeResumed = File(config.downloadPath);
    if (!fileTobeResumed.existsSync()) {
      //Complete the function with false.
      errorCallback?.call(
          'File to be resumed does not exist. Please set resume download to false to download a new file');

      if (config.continueDownloadIfFileDoesNotExist) {
        config.resumeDownload = false;
        preparationCompleter.complete(true);
        return true;
      }

      preparationCompleter.complete(false);
    } else {
      // print('The range is ${'bytes=${fileTobeResumed.lengthSync()}-'}');
      _header['range'] = 'bytes=${fileTobeResumed.lengthSync()}-';
      preparationCompleter.complete(true);
    } //
  } else {
    preparationCompleter.complete(true);
  }

  return preparationCompleter.future;
}