uploadFile method

  1. @override
BehaviorSubject<FrappeUploadStatus> uploadFile(
  1. FrappeUploadFileParams uploadFileParams
)
override

Returns a BehaviorSubject that emit FrappeUploadStatus as the upload progresses using socket.io.

It will fallback to uploadViaHTTP in case the socket.io method fails.

On completion, the event Status.completed will be emitted and the fileUrl will be set in the backend.

Implementation

@override
BehaviorSubject<FrappeUploadStatus> uploadFile(
    FrappeUploadFileParams uploadFileParams) {
  validateUploadFileArgs(uploadFileParams);
  var obs = BehaviorSubject<FrappeUploadStatus>.seeded(FrappeUploadStatus()
    ..status = UploadingStatus.uploading
    ..hasProgress = true);
  var realtimeUploader = FrappeSocketIOUploader();
  realtimeUploader.uploadStatus.listen((uploadStatus) async {
    switch (uploadStatus.status) {
      case UploadingStatus.ready:
        break;
      case UploadingStatus.error:
        config.coreInstance.config.logger.w([
          'LTS-Renovation-Core',
          'Frappe SocketIO Upload error',
          EnumToString.convertToString(uploadStatus.error)
        ]);
        // revert to http upload
        obs.add(FrappeUploadStatus()
          ..status = UploadingStatus.uploading
          ..hasProgress = false
          ..filename = uploadFileParams.fileName);
        var response = await uploadViaHTTP(uploadFileParams);
        obs.add(FrappeUploadStatus()
          ..status = response.isSuccess
              ? UploadingStatus.completed
              : UploadingStatus.error
          ..r = response);
        await obs.close();
        break;
      case UploadingStatus.uploading:
        config.coreInstance.config.logger
            .i(['Upload Progress', uploadStatus.progress.toString()]);
        obs.add(FrappeUploadStatus()
          ..filename = uploadStatus.filename
          ..status = UploadingStatus.uploading
          ..hasProgress = true
          ..progress = uploadStatus.progress);
        break;
      case UploadingStatus.completed:
        obs.add(FrappeUploadStatus()
          ..filename = uploadStatus.filename
          ..status = UploadingStatus.completed
          ..hasProgress = true
          ..progress = 100
          ..r = uploadStatus.r);
        await obs.close();
        break;
      case UploadingStatus.detail_error:
      default:
        RequestResponse<FrappeUploadFileResponse?>? response;
        if (uploadStatus.r != null) {
          response = uploadStatus.r;
        } else {
          response = RequestResponse.fail(handleError(null, null));
        }
        obs.add(FrappeUploadStatus()
          ..status = UploadingStatus.error
          ..r = response);

        await obs.close();
    }
  });
  realtimeUploader.upload(uploadFileParams);

  // try socketio upload first
  return obs;
}