downloadMessage static method

Future<V2TimCallback> downloadMessage({
  1. required String msgID,
  2. required int messageType,
  3. required int imageType,
  4. required bool isSnapshot,
})

Implementation

static Future<V2TimCallback> downloadMessage({
  required String msgID,
  required int messageType,
  required int imageType, // 图片类型,仅messageType为图片消息是有效
  required bool isSnapshot, // 是否是视频封面,仅messageType为视频消息是有效
}) async {
  Map<String, dynamic> message = Map.from({});
  String userDatafind = Tools.generateUserData(methodName: "findMessage");
  Pointer<Uint8> user_data_find = Tools.string2PointerInt8(userDatafind);
  Pointer<Uint8> json_message_id_array = Tools.string2PointerInt8(json.encode(List<String>.from([msgID])));
  int res = desktopSDK.D_TIMMsgFindMessages(json_message_id_array, user_data_find);
  if (res == TIMResult.TIM_SUCC) {
    Map<String, dynamic> data = await getAsyncData(apiKey: userDatafind);
    if (data["code"] == 0) {
      List<Map<String, dynamic>> messageList = List<Map<String, dynamic>>.from(json.decode(data["json_param"].isEmpty ? json.encode([]) : data["json_param"]));
      if (messageList.isNotEmpty) {
        message = messageList.first;
      }
    } else {
      return V2TimCallback.fromJson({
        "code": data["code"],
        "desc": data["desc"],
        "data": "",
      });
    }
  } else {
    return V2TimCallback.fromJson({
      "code": -1,
      "desc": "message not found",
      "data": "",
    });
  }

  var elemArray = message["message_elem_array"] ?? [];
  if (List.from(elemArray).isNotEmpty) {
    Map<String, dynamic> elem = List.from(elemArray).first;

    String name = Tools.getMulitiMessageFileID(
      elem,
      imageType,
      isSnapshot,
    );
    var data = Tools.getMulitiMessageFileIDV2(
      elem,
      imageType,
      isSnapshot,
    );
    Map<String, dynamic> downloaParam = Map.from({
      "msg_download_elem_param_id": name,
      "msg_download_elem_param_url": Tools.getMulitiMessageFileUrl(
        elem,
        imageType,
        isSnapshot,
      ),
    });

    var donwloadpath = await Tools.getDownloadPathV2(data['uuid'] ?? "", data['name'] ?? "");

    File f = File(donwloadpath);
    if (f.existsSync()) {
      return V2TimCallback.fromJson({
        "code": -4,
        "desc": "file exist.",
      });
    }
    print("download file to $donwloadpath ...");
    // String userData = "downloadMessageGlobal-${DateTime.now().microsecondsSinceEpoch}__${msgID}__${imageType}__${isSnapshot ? 1 : 0}__$donwloadpath";
    String userData = Tools.generateUserData();
    downloadCache[userData] = json.encode({
      "msgID": msgID,
      "imageType": imageType,
      "isSnapshot": isSnapshot,
      "donwloadpath": donwloadpath,
    });
    Pointer<Uint8> user_data_ptr = Tools.string2PointerInt8(userData);
    int res1 = desktopSDK.D_TIMMsgDownloadElemToPath(Tools.string2PointerInt8(json.encode(downloaParam)), Tools.string2PointerInt8(donwloadpath), user_data_ptr);
    if (res1 != TIMResult.TIM_SUCC) {
      return V2TimCallback.fromJson({
        "code": res1,
        "desc": "",
      });
    } else {
      Map<String, dynamic> data = await getAsyncData(apiKey: userData);
      return V2TimCallback.fromJson({
        "code": data["code"],
        "desc": data["desc"],
      });
    }
  } else {
    return V2TimCallback.fromJson({
      "code": -1,
      "desc": "elem not found",
      "data": "",
    });
  }
}