downloadMedia method

Future<void> downloadMedia()

Implementation

Future<void> downloadMedia() async {
  if (_convo != null) {
    state = state.copyWith(isDownloading: true);
    try {
      //Download media if media path is not available
      final tempDir = await getTemporaryDirectory();
      final result = await _convo!.downloadMedia(
        messageInfo.messageId,
        null,
        tempDir.path,
      );
      final mediaPath = result.text();
      if (mediaPath != null) {
        state = state.copyWith(
          mediaFile: File(mediaPath),
          videoThumbnailFile: null,
          isDownloading: false,
        );
        final videoThumbnailFile = await getThumbnailData(mediaPath);
        if (videoThumbnailFile != null) {
          if (state.mediaFile?.path == mediaPath) {
            state = state.copyWith(videoThumbnailFile: videoThumbnailFile);
          }
        }
      }
    } catch (e, s) {
      _log.severe('Error downloading media:', e, s);
      state = state.copyWith(
        isDownloading: false,
        mediaChatLoadingState: MediaChatLoadingState.error(
          'Some error occurred ${e.toString()}',
        ),
      );
    }
  }
}