sendFile method
This method allows to send a file
to a channel
with an optional fileMessage
.
Ensure that your Keyset has a
publishKey
defined.
If you provide a cipherKey
, the file will be encrypted with it.
If its missing, then Keyset.cipherKey will be used.
If no cipherKey
is provided, then the file won't be encrypted.
If the upload was successful, but publishing the file event to the channel
wasn't,
this method will retry publishing up to a value configured in fileMessagePublishRetryLimit
.
In case that is unsuccessful, you can retry publishing the file event manually by passing FileInfo
to the publishFileMessage method.
Additional file event options
-
You can set a per-message time to live in storage using
fileMessageTtl
option. If set to0
, message won't expire. If unset, expiration will fall back to default. -
You can override the default account configuration on message saving using
storeFileMessage
flag -true
to save andfalse
to discard. Leave this option unset if you want to use the default. -
Provide
fileMessageMeta
for additional information.
Implementation
Future<PublishFileMessageResult> sendFile(
String channel, String fileName, List<int> file,
{CipherKey? cipherKey,
dynamic fileMessage,
bool? storeFileMessage,
int? fileMessageTtl,
String? customMessageType,
dynamic fileMessageMeta,
Keyset? keyset,
String? using}) async {
keyset ??= _core.keysets[using];
var requestPayload = await _core.parser.encode({'name': fileName});
var uploadDetails = await defaultFlow<GenerateFileUploadUrlParams,
GenerateFileUploadUrlResult>(
keyset: keyset,
core: _core,
params: GenerateFileUploadUrlParams(keyset, channel, requestPayload),
serialize: (object, [_]) =>
GenerateFileUploadUrlResult.fromJson(object));
if (keyset.cipherKey != null ||
cipherKey != null ||
_core.crypto is CryptoModule) {
file = (cipherKey != null ||
!(keyset.cipherKey == _core.keysets.defaultKeyset.cipherKey))
? _core.crypto.encryptFileData(cipherKey ?? keyset.cipherKey!, file)
: _core.crypto.encrypt(file);
}
var fileInfo = FileInfo(
uploadDetails.fileId,
uploadDetails.fileName,
getFileUrl(channel, uploadDetails.fileId, uploadDetails.fileName,
keyset: keyset)
.toString(),
);
var publishMessage = FileMessage(fileInfo, message: fileMessage);
var retryCount = keyset.fileMessagePublishRetryLimit;
var params = FileUploadParams(
uploadDetails.uploadUri, {...uploadDetails.formFields, 'file': file});
await defaultFlow<FileUploadParams, FileUploadResult>(
keyset: keyset,
core: _core,
params: params,
deserialize: false,
serialize: (object, [_]) => FileUploadResult.fromJson(object));
var publishFileResult = PublishFileMessageResult();
do {
try {
publishFileResult = await publishFileMessage(channel, publishMessage,
ttl: fileMessageTtl,
storeMessage: storeFileMessage,
meta: fileMessageMeta,
cipherKey: cipherKey,
customMessageType: customMessageType,
keyset: keyset,
using: using);
} catch (e) {
publishFileResult.description =
'File message publish failed due to $e please refer fileInfo for file details';
publishFileResult.isError = true;
}
if (!publishFileResult.isError!) {
publishFileResult.fileInfo = fileInfo;
return publishFileResult;
}
--retryCount;
} while (retryCount > 0);
return publishFileResult..fileInfo = fileInfo;
}