sendFileMessage method

FileMessage sendFileMessage(
  1. FileMessageCreateParams params,
  2. {FileMessageHandler? handler,
  3. ProgressHandler? progressHandler}
)

Sends a file with given file information.

Implementation

FileMessage sendFileMessage(
  FileMessageCreateParams params, {
  FileMessageHandler? handler,
  ProgressHandler? progressHandler,
}) {
  sbLog.i(StackTrace.current,
      'params.uploadFile.name: ${params.fileInfo.fileName}');
  checkUnsupportedAction();

  if (params.fileInfo.hasSource == false) {
    throw InvalidParameterException();
  }

  final pendingFileMessage =
      FileMessage.fromParams(params: params, channel: this)..set(chat);
  pendingFileMessage.sendingStatus = SendingStatus.pending;
  pendingFileMessage.sender =
      Sender.fromUser(chat.chatContext.currentUser, this);

  final queue = chat.getMessageQueue(channelUrl);
  final task = AsyncSimpleTask(
    () async {
      UploadResponse? uploadResponse;

      if (params.fileInfo.hasBinary) {
        try {
          uploadResponse = await chat.apiClient
              .send<UploadResponse>(ChannelFileUploadRequest(chat,
                  channelUrl: channelUrl,
                  requestId: pendingFileMessage.requestId!,
                  params: params,
                  progressHandler: progressHandler))
              .timeout(
            Duration(seconds: chat.chatContext.options.fileTransferTimeout),
            onTimeout: () {
              if (handler != null) {
                handler(
                  pendingFileMessage..sendingStatus = SendingStatus.failed,
                  SendbirdException(
                    message: 'Upload timeout',
                    code: SendbirdError.fileUploadTimeout,
                  ),
                );
              }
              throw SendbirdException(code: SendbirdError.fileUploadTimeout);
            },
          );
        } catch (e) {
          sbLog.e(StackTrace.current, 'e: $e');
          rethrow;
        }
      }

      String? fileUrl = uploadResponse?.url;
      int? fileSize = uploadResponse?.fileSize;
      if (fileUrl != null) params.fileInfo.fileUrl = fileUrl;
      if (fileSize != null) params.fileInfo.fileSize = fileSize;

      final cmd = Command.buildFileMessage(
        channelUrl: channelUrl,
        params: params,
        requestId: pendingFileMessage.requestId,
        requireAuth: uploadResponse?.requireAuth,
        thumbnails: uploadResponse?.thumbnails,
      );

      final message = BaseMessage.getMessageFromJsonWithChat<FileMessage>(
        chat,
        cmd.payload,
        channelType: channelType,
        commandType: cmd.cmd,
      );

      if (chat.chatContext.currentUser == null) {
        final error = ConnectionRequiredException();
        message
          ..errorCode = error.code
          ..sendingStatus = SendingStatus.failed;
        if (handler != null) handler(message, error);
        return message;
      }

      if (chat.connectionManager.isConnected()) {
        chat.commandManager.sendCommand(cmd).then((result) {
          if (result == null) return;
          final message = BaseMessage.getMessageFromJsonWithChat<FileMessage>(
            chat,
            result.payload,
            commandType: result.cmd,
          );

          chat.collectionManager.onMessageSentByMe(message);
          if (handler != null) handler(message, null);
        }).catchError((e) {
          sbLog.e(StackTrace.current, 'e: $e');
          pendingFileMessage
            ..errorCode = e?.code ?? SendbirdError.unknownError
            ..sendingStatus = SendingStatus.failed;
          if (handler != null) handler(pendingFileMessage, e);
        });
      } else {
        final request = ChannelFileMessageSendRequest(
          chat,
          channelType: channelType,
          channelUrl: channelUrl,
          params: params,
          thumbnails: uploadResponse?.thumbnails,
          requireAuth: uploadResponse?.requireAuth,
        );
        final message = await chat.apiClient.send<FileMessage>(request);

        chat.collectionManager.onMessageSentByMe(message);
        if (handler != null) handler(message, null);
      }
    },
    onCancel: () {
      if (handler != null) {
        handler(pendingFileMessage, OperationCanceledException());
      }
    },
  );

  queue.enqueue(task);

  chat.setUploadTask(pendingFileMessage.requestId!, task);
  chat.setMessageQueue(channelUrl, queue);

  return pendingFileMessage;
}