This commit is contained in:
杨建炊 2025-09-29 11:27:24 +08:00
parent ae77622325
commit 5cf48298f2
2 changed files with 35 additions and 21 deletions

View File

@ -56,7 +56,7 @@ public class CropController {
@GetMapping("/crop/chatData") @GetMapping("/crop/chatData")
public List<CropMessage> chatData(Long startSeq,String corpId) throws IOException { public List<CropMessage> chatData(Long startSeq, String corpId) throws IOException {
long limit = 1000; long limit = 1000;
long slice = Finance.NewSlice(); long slice = Finance.NewSlice();
Long sdk = sdkManager.initSdk(corpId, "mEizahrSF6axdfWtSK_f73a3j6-sV02hhyGG7ogmTpM"); Long sdk = sdkManager.initSdk(corpId, "mEizahrSF6axdfWtSK_f73a3j6-sV02hhyGG7ogmTpM");
@ -69,7 +69,7 @@ public class CropController {
} }
String contentResult = Finance.GetContentFromSlice(slice); String contentResult = Finance.GetContentFromSlice(slice);
log.info("内容字符串" + contentResult); log.info("内容字符串" + contentResult);
return decodeChatData(contentResult,startSeq,sdk,corpId); return decodeChatData(contentResult, startSeq, sdk, corpId);
} }
/** /**
@ -77,7 +77,7 @@ public class CropController {
* *
* @param contentResult 拉取到的JSON原文 * @param contentResult 拉取到的JSON原文
*/ */
public List<CropMessage> decodeChatData(String contentResult,Long startSeq,Long sdk,String corpId) throws IOException { public List<CropMessage> decodeChatData(String contentResult, Long startSeq, Long sdk, String corpId) throws IOException {
// 1.基础信息解析 // 1.基础信息解析
List<CropMessage> messageList = new ArrayList<>(); List<CropMessage> messageList = new ArrayList<>();
MessagePullResponse messagePullResponse = JSON.parseObject(contentResult, MessagePullResponse.class); MessagePullResponse messagePullResponse = JSON.parseObject(contentResult, MessagePullResponse.class);
@ -92,7 +92,7 @@ public class CropController {
} }
String privateKey = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8); String privateKey = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
// 3. 提取所有 seq // 3. 提取所有 seq
log.info("chatdataList={} 加密数据",chatdataList); log.info("chatdataList={} 加密数据", chatdataList);
// 4. 查询数据库中已存在的 seq // 4. 查询数据库中已存在的 seq
// 直接在业务代码中或封装到 service // 直接在业务代码中或封装到 service
List<Long> existingSeqs = cropMessageService.lambdaQuery() List<Long> existingSeqs = cropMessageService.lambdaQuery()
@ -103,9 +103,9 @@ public class CropController {
.map(CropMessage::getSeq) .map(CropMessage::getSeq)
.collect(Collectors.toList()); .collect(Collectors.toList());
Set<Long> existingSeqSet = new HashSet<>(existingSeqs); Set<Long> existingSeqSet = new HashSet<>(existingSeqs);
log.info("existingSeqSet={} 加密数据",existingSeqSet); log.info("existingSeqSet={} 加密数据", existingSeqSet);
CropConfig config=cropConfigMapper.selectById(1); CropConfig config = cropConfigMapper.selectById(1);
Long maxSeq = config.getLastSeq(); Long maxSeq = config.getLastSeq();
// 改为 // 改为
List<CropFile> mediaFileInfos = new ArrayList<>(); List<CropFile> mediaFileInfos = new ArrayList<>();
@ -117,7 +117,7 @@ public class CropController {
} }
try { try {
// 解密ChatData数据 // 解密ChatData数据
String plainTextJson = decrypt(chatData, privateKey,sdk); String plainTextJson = decrypt(chatData, privateKey, sdk);
BaseMessageDto messageDto = JSON.parseObject(plainTextJson, BaseMessageDto.class); BaseMessageDto messageDto = JSON.parseObject(plainTextJson, BaseMessageDto.class);
// 转换dto数据到entity // 转换dto数据到entity
CropMessage message = messageDto.convertToMessage(); CropMessage message = messageDto.convertToMessage();
@ -140,8 +140,9 @@ public class CropController {
} }
return saveData(messageList,maxSeq,config,mediaFileInfos,sdk,corpId); return saveData(messageList, maxSeq, config, mediaFileInfos, sdk, corpId);
} }
/** /**
* 异步下载待处理的媒体文件 * 异步下载待处理的媒体文件
* *
@ -149,7 +150,7 @@ public class CropController {
*/ */
@Transactional @Transactional
public List<CropMessage> saveData(List<CropMessage> messageList, Long maxSeq, CropConfig config public List<CropMessage> saveData(List<CropMessage> messageList, Long maxSeq, CropConfig config
, List<CropFile>mediaFileInfos,Long sdk,String corpId) { , List<CropFile> mediaFileInfos, Long sdk, String corpId) {
// 4.保存消息和失败任务 // 4.保存消息和失败任务
cropMessageService.saveBatch(messageList); cropMessageService.saveBatch(messageList);
@ -164,6 +165,7 @@ public class CropController {
mf.setSeq(info.getSeq()); mf.setSeq(info.getSeq());
mf.setCropId(corpId); mf.setCropId(corpId);
mf.setStatus(0); mf.setStatus(0);
mf.setExt(info.getExt());
return mf; return mf;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
@ -172,18 +174,20 @@ public class CropController {
// 异步触发下载任务可以轮询数据库 // 异步触发下载任务可以轮询数据库
CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
downloadPendingFiles(mediaFiles,sdk); // 也可以不传参改为查数据库 downloadPendingFiles(mediaFiles, sdk); // 也可以不传参改为查数据库
}); });
} }
return messageList; return messageList;
} }
/** /**
* 异步下载待处理的媒体文件 * 异步下载待处理的媒体文件
*/ */
private void downloadPendingFiles(List<CropFile> mediaFiles,Long sdk) { private void downloadPendingFiles(List<CropFile> mediaFiles, Long sdk) {
for (CropFile mf : mediaFiles) { for (CropFile mf : mediaFiles) {
try { try {
String url = downFile(mf.getSdkFileId(), mf.getMsgType(),sdk); String msgType = getSuffixByMsgType(mf);
String url = downFile(mf.getSdkFileId(), msgType, sdk);
if (url != null && !url.isEmpty()) { if (url != null && !url.isEmpty()) {
mf.setUrl(url); mf.setUrl(url);
mf.setStatus(1); mf.setStatus(1);
@ -201,6 +205,7 @@ public class CropController {
} }
} }
} }
/** /**
* 提取所有需要下载的媒体文件信息 * 提取所有需要下载的媒体文件信息
*/ */
@ -230,10 +235,17 @@ public class CropController {
if (msg.containsKey(msgType)) { if (msg.containsKey(msgType)) {
String sdkFileId = msg.getJSONObject(msgType).getString("sdkfileid"); String sdkFileId = msg.getJSONObject(msgType).getString("sdkfileid");
if (sdkFileId != null && !sdkFileId.trim().isEmpty()) { if (sdkFileId != null && !sdkFileId.trim().isEmpty()) {
CropFile cropFile = new CropFile(); // 注意你写成了 CropConfig应为 CropFile CropFile cropFile = new CropFile();
cropFile.setMsgType(msgType); cropFile.setMsgType(msgType);
cropFile.setSeq(seq); cropFile.setSeq(seq);
cropFile.setSdkFileId(sdkFileId); cropFile.setSdkFileId(sdkFileId);
cropFile.setSeq(seq);
cropFile.setExt(msgType);
if (msgType.equals("file")) {
String fileext = msg.getJSONObject(msgType).getString("fileext");
cropFile.setExt(fileext);
}
result.add(cropFile); result.add(cropFile);
} }
} }
@ -246,9 +258,11 @@ public class CropController {
} }
@GetMapping("/crop/downFile") @GetMapping("/crop/downFile")
public String downFile(String sdkField,String msgType,Long sdk) throws IOException { public String downFile(String sdkField, String msgType, Long sdk) throws IOException {
File tempFile = File.createTempFile(GlobalConstants.DOWNLOAD_TEMP_FILE_NAME_PREFIX , File tempFile = File.createTempFile(GlobalConstants.DOWNLOAD_TEMP_FILE_NAME_PREFIX,
getSuffixByMsgType(msgType), mediaContext.getTempMergeFileDir()); msgType, mediaContext.getTempMergeFileDir());
sdk = sdkManager.initSdk("ww64f47cd0afc456a5", "mEizahrSF6axdfWtSK_f73a3j6-sV02hhyGG7ogmTpM");
String indexbuf = null; // 初始化 indexbuf String indexbuf = null; // 初始化 indexbuf
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile)) { try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile)) {
while (true) { while (true) {
@ -273,8 +287,8 @@ public class CropController {
return ""; return "";
} }
public String getSuffixByMsgType(String msgType) { public String getSuffixByMsgType(CropFile cropFile) {
switch (msgType) { switch (cropFile.getMsgType()) {
case "image": case "image":
return ".jpg"; return ".jpg";
case "voice": case "voice":
@ -282,7 +296,7 @@ public class CropController {
case "video": case "video":
return ".mp4"; return ".mp4";
case "file": case "file":
return ".bin"; return "." + cropFile.getExt(); // 取真实后缀
default: default:
return ".bin"; return ".bin";
} }
@ -296,7 +310,7 @@ public class CropController {
* @param privateKeyStr 私钥字符串 * @param privateKeyStr 私钥字符串
* @return 解密后的原文 * @return 解密后的原文
*/ */
private String decrypt(ChatData chatData, String privateKeyStr,Long sdk) throws Exception { private String decrypt(ChatData chatData, String privateKeyStr, Long sdk) throws Exception {
// 1.解密EncryptRandomKey // 1.解密EncryptRandomKey
String randomKey = EncodeUtils.decryptRandomKey(chatData.getEncryptRandomKey(), privateKeyStr); String randomKey = EncodeUtils.decryptRandomKey(chatData.getEncryptRandomKey(), privateKeyStr);
// 2.调用SDK方法解密密文数据 // 2.调用SDK方法解密密文数据

View File

@ -23,7 +23,7 @@ public class CropFile {
private String url; // 下载后上传 OSS 的地址 private String url; // 下载后上传 OSS 的地址
private String fileName; // 可选原始文件名后续可补充 private String ext; // 可选原始文件名后续可补充
private Integer status = 0; // 0 pending, 1 success, 2 failed private Integer status = 0; // 0 pending, 1 success, 2 failed