🎨 #2858 【企业微信】优化会话存档获取媒体数据的接口
This commit is contained in:
@@ -5,6 +5,7 @@ import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.bean.msgaudit.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* 会话内容存档接口.
|
||||
@@ -72,6 +73,22 @@ public interface WxCpMsgAuditService {
|
||||
void getMediaFile(@NonNull long sdk, @NonNull String sdkfileid, String proxy, String passwd, @NonNull long timeout,
|
||||
@NonNull String targetFilePath) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 获取媒体文件 传入一个lambda,each所有的数据分片byte[],更加灵活
|
||||
* 针对图片、文件等媒体数据,提供sdk接口拉取数据内容。
|
||||
* 详情可以看官方文档,亦可阅读此接口源码。
|
||||
*
|
||||
* @param sdk getChatDatas()获取到的sdk,注意,每次获取的sdk会不一样
|
||||
* @param sdkfileid 消息体内容中的sdkfileid信息
|
||||
* @param proxy 使用代理的请求,需要传入代理的链接。如:socks5://10.0.0.1:8081 或者 http://10.0.0.1:8081,如果没有传null
|
||||
* @param passwd 代理账号密码,需要传入代理的账号密码。如 user_name:passwd_123,如果没有传null
|
||||
* @param timeout 超时时间,分片数据需累加到文件存储。单次最大返回512K字节,如果文件比较大,自行设置长一点,比如timeout=10000
|
||||
* @param action 传入一个lambda,each所有的数据分片
|
||||
* @throws WxErrorException the wx error exception
|
||||
*/
|
||||
void getMediaFile(@NonNull long sdk, @NonNull String sdkfileid, String proxy, String passwd, @NonNull long timeout,
|
||||
@NonNull Consumer<byte[]> action) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* 获取会话内容存档开启成员列表
|
||||
* 企业可通过此接口,获取企业开启会话内容存档的成员列表
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.MsgAudit.*;
|
||||
|
||||
@@ -80,10 +81,10 @@ public class WxCpMsgAuditServiceImpl implements WxCpMsgAuditService {
|
||||
long sdk = Finance.NewSdk();
|
||||
//因为会话存档单独有个secret,优先使用会话存档的secret
|
||||
String msgAuditSecret = cpService.getWxCpConfigStorage().getMsgAuditSecret();
|
||||
if(StringUtils.isEmpty(msgAuditSecret)) {
|
||||
if (StringUtils.isEmpty(msgAuditSecret)) {
|
||||
msgAuditSecret = cpService.getWxCpConfigStorage().getCorpSecret();
|
||||
}
|
||||
long ret = Finance.Init(sdk, cpService.getWxCpConfigStorage().getCorpId(),msgAuditSecret);
|
||||
long ret = Finance.Init(sdk, cpService.getWxCpConfigStorage().getCorpId(), msgAuditSecret);
|
||||
if (ret != 0) {
|
||||
Finance.DestroySdk(sdk);
|
||||
throw new WxErrorException("init sdk err ret " + ret);
|
||||
@@ -181,7 +182,26 @@ public class WxCpMsgAuditServiceImpl implements WxCpMsgAuditService {
|
||||
if (!targetFile.getParentFile().exists()) {
|
||||
targetFile.getParentFile().mkdirs();
|
||||
}
|
||||
this.getMediaFile(sdk, sdkfileid, proxy, passwd, timeout, i -> {
|
||||
try {
|
||||
// 大于512k的文件会分片拉取,此处需要使用追加写,避免后面的分片覆盖之前的数据。
|
||||
FileOutputStream outputStream = new FileOutputStream(targetFile, true);
|
||||
outputStream.write(i);
|
||||
outputStream.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getMediaFile(@NonNull long sdk, @NonNull String sdkfileid, String proxy, String passwd, @NonNull long timeout, @NonNull Consumer<byte[]> action) throws WxErrorException {
|
||||
/**
|
||||
* 1、媒体文件每次拉取的最大size为512k,因此超过512k的文件需要分片拉取。
|
||||
* 2、若该文件未拉取完整,sdk的IsMediaDataFinish接口会返回0,同时通过GetOutIndexBuf接口返回下次拉取需要传入GetMediaData的indexbuf。
|
||||
* 3、indexbuf一般格式如右侧所示,”Range:bytes=524288-1048575“:表示这次拉取的是从524288到1048575的分片。单个文件首次拉取填写的indexbuf
|
||||
* 为空字符串,拉取后续分片时直接填入上次返回的indexbuf即可。
|
||||
*/
|
||||
String indexbuf = "";
|
||||
int ret, data_len = 0;
|
||||
log.debug("正在分片拉取媒体文件 sdkFileId为{}", sdkfileid);
|
||||
@@ -200,9 +220,7 @@ public class WxCpMsgAuditServiceImpl implements WxCpMsgAuditService {
|
||||
|
||||
try {
|
||||
// 大于512k的文件会分片拉取,此处需要使用追加写,避免后面的分片覆盖之前的数据。
|
||||
FileOutputStream outputStream = new FileOutputStream(new File(targetFilePath), true);
|
||||
outputStream.write(Finance.GetData(mediaData));
|
||||
outputStream.close();
|
||||
action.accept(Finance.GetData(mediaData));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user