download static method

Future<String> download(
  1. Object url,
  2. String fileName, {
  3. Map<String, String>? headers,
  4. void onProgress(
    1. int bytesReceived,
    2. int totalBytes
    )?,
})

Downloads a file from the specified url to a temporary location. The url can be either a String or a Uri object.

Returns the path to the downloaded file.

Implementation

static Future<String> download(
  Object url,
  String fileName, {
  Map<String, String>? headers,
  void Function(int bytesReceived, int totalBytes)? onProgress,
}) async {
  try {
    // Create a temporary file path
    final tempDir = Directory.systemTemp;
    final filePath = '${tempDir.path}/$fileName';

    final uri = _toUri(url);
    await _client.download(uri, fileName, headers: headers, onProgress: onProgress);

    // Track the downloaded file
    _downloadedFiles[fileName] = filePath;

    return filePath;
  } catch (e) {
    if (e is HTTPSException) {
      rethrow;
    } else {
      Error.throwWithStackTrace(
        HTTPSException(
          message: 'Failed to download file from $url: ${e.toString()}',
          originalError: e,
        ),
        StackTrace.current,
      );
    }
  }
}