sendTPMultipleFilesMessage static method
Implementation
static Future<TPMessage?> sendTPMultipleFilesMessage(
TPMessageMultipleFilesParams params, {
Function(int? errorCode, String? errorMessage)? errorCallback,
Function(int bytesSent, int totalBytesSent, int totalBytesExpectedToSend)? progressCallback,
}) async {
// Fetches the allowed MIME type list (cached up to 1 hour), validates each file,
// uploads all files to S3, then sends a message containing the uploaded file URLs.
if (!_isInitialized(errorCallback: errorCallback)) { return null; }
if (!_checkAuthInfo(errorCallback: errorCallback)) { return null; }
if (params.files.isEmpty) {
errorCallback?.call(-1, "no files to upload.");
return null;
}
if (params.files.length > 10) {
errorCallback?.call(6003, "FileCountLimitExceeded");
return null;
}
final channelId = params.channel.getChannelId();
final Set<String>? allowedMimeTypes;
try {
final mimeTypeList = await _getMimeTypesWithCache();
Logger.log("Allowed MIME types: ${mimeTypeList.map((e) => e.contentType).join(', ')}");
allowedMimeTypes = mimeTypeList.map((e) => e.contentType).toSet();
} on TPException catch (e) {
Logger.log("TPException fetching supported MIME types: $e");
errorCallback?.call(e.getCode(), e.getMessage());
return null;
} catch (e) {
Logger.log("Error fetching supported MIME types: $e");
errorCallback?.call(-1, e.toString());
return null;
}
for (final file in params.files) {
if (!allowedMimeTypes.contains(file.mimeType)) {
final e = TPException()..setCode(6002)..setMessage("Not Supported Content Type: ${file.mimeType}");
errorCallback?.call(e.getCode(), e.getMessage());
return null;
}
}
final List<TPUploadResult> uploadResults;
try {
final totalBytesExpectedToSend = await TPFileTransfer.prepareUpload(params.files, allowedMimeTypes: allowedMimeTypes);
uploadResults = await TPFileTransfer.uploadAllFiles(
channelId, params.files, totalBytesExpectedToSend, progressCallback);
} on TPException catch (e) {
Logger.log("TPException uploading files: $e");
errorCallback?.call(e.getCode(), e.getMessage());
return null;
} catch (e) {
Logger.log("Error uploading files: $e");
errorCallback?.call(-1, e.toString());
return null;
}
final message = await _sendMultipleFilesMessageRequest(
channelId: channelId,
params: params,
uploadedUrls: uploadResults.map((r) => r.url).toList(),
errorCallback: errorCallback,
);
if (message != null) {
// On success, the files are moved out of the tmp bucket — invalidate the cached presigned URLs.
try {
await TPFileTransfer.invalidateUploadCache(uploadResults.map((r) => r.dbKey));
} catch (e) {
Logger.log("Failed to invalidate upload cache (non-fatal): $e");
}
}
return message;
}