download method

void download({
  1. required String savePath,
  2. ProgressCallback? onReceiveProgress,
  3. Success? success,
  4. Failure? failure,
  5. Completed? completed,
})

下载文件

Implementation

void download({
  required String savePath,
  adapter.ProgressCallback? onReceiveProgress,
  Success? success,
  Failure? failure,
  Completed? completed,
}) async {
  if (!(await _checkNetWork())) {
    return;
  }

  final url = _buildFinalUrl();
  final payload = _resolveRequestPayload();
  final file = File(savePath);

  try {
    if (!file.parent.existsSync()) {
      file.parent.createSync(recursive: true);
    }

    // 构建 AdapterRequest
    final adapterRequest = _buildAdapterRequest(
      url: url,
      queryParams: payload.queryParams,
      data: payload.body,
      contentType: payload.contentType,
    );

    // 使用适配器下载文件
    final adapter = _requireAdapter();
    final response = await adapter.download(
      adapterRequest,
      savePath,
      onProgress: (received, total) {
        onReceiveProgress?.call(received, total > 0 ? total : received);
      },
    );

    onResponse?.call(response);
    if (response.isSuccess) {
      success?.call(savePath, SourcesType.net);
    } else {
      failure?.call(response.data);
    }
  } on AdapterException catch (e) {
    failure?.call(e);
  } catch (e) {
    failure?.call(e);
  } finally {
    completed?.call();
  }
}