uploadImageAndVideo method

Future<void> uploadImageAndVideo(
  1. File image,
  2. File? videoFile,
  3. String setupFailedError,
  4. String imageDataTooLargeError,
  5. String serverCouldNotProcessImageError,
  6. String mediaFormatNotSupportedError,
)

Implementation

Future<void> uploadImageAndVideo(File image, File? videoFile, String setupFailedError, String imageDataTooLargeError, String serverCouldNotProcessImageError, String mediaFormatNotSupportedError) async {
  try {
    final deviceId = await _getDeviceFingerprint();

    await _advanceProgressTo(0.35);

    final imageBytes = await image.readAsBytes();
    final imageBase64 = base64Encode(imageBytes);

    await _advanceProgressTo(0.5);

    final imageRequest = UploadMediaRequest(
      mediaBase64Strict: imageBase64,
      visitorId: deviceId,
      deviceId: deviceId,
      appScreen: state.screen,
    );

    try {
      final apiClient = _ref.read(apiClientProvider);
      await apiClient.perform(
        imageRequest,
        progressHandler: (progress) {
          if (!_isDisposed) {
            _safeSetState(state.copyWith(uploadProgress: 0.5 + (progress * 0.2)));
          }
        },
      );

      if (videoFile != null && await videoFile.exists()) {
        await _advanceProgressTo(0.7);

        try {
          final compressedFile = await _compressVideo(videoFile);

          if (compressedFile != null) {
            final videoBytes = await compressedFile.readAsBytes();
            final videoBase64 = base64Encode(videoBytes);

            final videoRequest = UploadMediaRequest(
              mediaBase64Strict: videoBase64,
              visitorId: deviceId,
              deviceId: deviceId,
              appScreen: state.screen,
            );

            try {
              await apiClient.perform(
                videoRequest,
                progressHandler: (progress) {
                  if (!_isDisposed) {
                    _safeSetState(state.copyWith(uploadProgress: 0.7 + (progress * 0.25)));
                  }
                },
              );

              await compressedFile.delete();
            } catch (e) {
              // Handle error silently
            }
          }
        } catch (e) {
          // Handle error silently
        }
      }

      await _advanceProgressTo(1.0);
      state = state.copyWith(isProcessing: false);
    } catch (e) {
      await _handleUploadError(e, setupFailedError, imageDataTooLargeError, serverCouldNotProcessImageError, mediaFormatNotSupportedError);
    }
  } catch (e) {
    await _handleUploadError(e, setupFailedError, imageDataTooLargeError, serverCouldNotProcessImageError, mediaFormatNotSupportedError);
  }

  if (videoFile != null && await videoFile.exists()) {
    try {
      await videoFile.delete();
    } catch (e) {
      // Handle error silently
    }
  }
}