breakPointUpload method

void breakPointUpload({
  1. required String filePath,
  2. ProgressCallback? onSendProgress,
  3. Success? success,
  4. Failure? failure,
  5. Completed? completed,
  6. dynamic cancelCallback()?,
  7. int? start,
})

Implementation

void breakPointUpload({
  required String filePath,
  ProgressCallback? onSendProgress,
  Success? success,
  Failure? failure,
  Completed? completed,
  Function()? cancelCallback,
  int? start,
}) async {
  if (!(await _checkNetWork())) {
    return;
  }
  String url = _path.toString();
  if (isRestfulUrl()) {
    url = NetUtils.restfulUrl(_path.toString(), _params);
  }
  var progress = start ?? 0;
  int fileSize = 0;
  File file = File(filePath);
  if (file.existsSync()) {
    // Stream<List<int>> streamFileSize = file.openRead();
    // await for (var chunk in streamFileSize) {
    //   fileSize += chunk.length;
    // }
    fileSize = file.lengthSync();
  }
  var data = file.openRead(progress, fileSize - progress);
  try {
    _options?.method = _httpType.name;
    if (_headers.isNotEmpty) {
      _options?.headers?.addAll(_headers);
    }
    if (_enableGlobalHeader) {
      _options?.headers ??= {};
      _options?.headers?.addAll(_rxNet.getHeaders());
    }
    _options?.headers?.addAll(
        {'Content-Range': 'bytes $progress-${fileSize - 1}/$fileSize'});

    if (_toFormData) {
      _bodyData = FormData.fromMap(_params);
    }
    if (_toBodyData) {
      _bodyData = _params;
    }
    final response = await _rxNet.client!.request<ResponseBody>(
      url,
      options: _options,
      cancelToken: _cancelToken,
      data: data,
      queryParameters:
          (isRestfulUrl() || _toFormData || _toBodyData) ? {} : _params,
    );
    onResponse?.call(response);
    Stream<Uint8List> stream = response.data!.stream;
    final subscription = stream.listen((data) {
      progress = progress + data.length;
      onSendProgress?.call(progress, fileSize);
    }, onDone: () async {
      success?.call(file, SourcesType.net);
    }, onError: (e) async {
      failure?.call(e);
    }, cancelOnError: true);
    _cancelToken?.whenCancel.then((_) async {
      await subscription.cancel();
      cancelCallback?.call();
    });
  } on DioException catch (error) {
    if (error.response != null) {
      if (progress <= fileSize) {
        onSendProgress?.call(progress, fileSize);
        success?.call(file, SourcesType.net);
      }
    }
    if (CancelToken.isCancel(error)) {
      cancelCallback?.call();
    } else {
      failure?.call(error);
    }
  }

  completed?.call();
}