uploadMergedMessageFile static method
合并并上传消息,返回上传后的url和md5
Implementation
static Future<NIMResult<MessageUploadInfo>> uploadMergedMessageFile(
List<NIMMessage> messages) async {
final mergeMsg = await createForwardMessageListFileDetail(messages);
if (mergeMsg.isSuccess && mergeMsg.data != null) {
final List<int> fileBytes = utf8.encode(mergeMsg.data!);
String fileName =
"$mergeFileName${DateTime.now().millisecondsSinceEpoch}";
var directory;
if (Platform.isIOS) {
directory = await getApplicationDocumentsDirectory();
} else {
directory = await getExternalStorageDirectory();
}
if (directory == null) {
return NIMResult.failure(message: 'directory is null');
}
File file;
try {
file = File('${directory.path}/$fileName');
file.writeAsBytesSync(fileBytes);
} catch (e) {
return NIMResult.failure(message: 'create file error ${e.toString()}');
}
final uploadRes =
await NimCore.instance.nosService.upload(filePath: file.path);
if (uploadRes.isSuccess && uploadRes.data != null) {
try {
final md5 = await file.md5;
file.delete();
return NIMResult.success(
data: MessageUploadInfo(uploadRes.data!, md5));
} catch (exception) {
return NIMResult.failure(
message: 'get md5 error ${exception.toString()}');
}
} else {
return NIMResult.failure(message: uploadRes.errorDetails);
}
} else {
return NIMResult.failure(message: mergeMsg.errorDetails);
}
}