uploadAttachment static method
dynamic
uploadAttachment(})
Implementation
static uploadAttachment(File file, type, String fileStore,
{required Function(String id, String message, String type,
{String? tempUniqueId})
cb,
required Function onMessageSend,
required BuildContext context,
required String interactionId,
required User user,
required String appId,
String threadId = ""}) async {
var uuid = const Uuid();
Logs.debug(fileStore);
String uniqueId = uuid.v4().toString();
Message socketMessage = Message(
attachments: [
message_model.Attachment(
type: type, data: message_model.Data(fileUrl: file))
],
interactionId: interactionId,
message: "",
messageType: "multimedia",
author: Author(id: user.participantId, name: user.name),
createdAt: DateTime.now(),
isDelivered: true,
channelMessageId: uniqueId,
);
onMessageSend(socketMessage);
var request = http.MultipartRequest(
'POST',
Uri.tryParse(
"$fileStore/chat_widget_$appId/${interactionId.isNotEmpty ? interactionId : threadId}")!);
request.files.add(http.MultipartFile.fromBytes(
'file', File(file.path).readAsBytesSync(),
filename: file.path));
var res = await request.send();
Logs.debug("////////////////////RESPONSE");
res.stream.transform(utf8.decoder).listen((value) {
Logs.debug(value);
Map<String, dynamic> data = json.decode(value);
Logs.debug("///////////////////// ATTACHMENT ////////////////////////");
Logs.debug(data);
Uri uri = Uri.parse(fileStore);
Logs.debug(uri.origin);
Logs.debug(fileStore);
Logs.debug(Uri.tryParse(
"$fileStore/chat_widget_$appId/${interactionId.isNotEmpty ? interactionId : threadId}")!);
if (data["success"]) {
cb(interactionId,
data["fileUrl"] ?? "${uri.origin}/store${data["url"]}", type,
tempUniqueId: uniqueId);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(data["message"] ?? "Error while uploading media"),
duration: const Duration(seconds: 2), // Toast duration
),
);
}
});
}