123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package com.fdkankan.contro.mq.nsq;
- import cn.hutool.core.exceptions.ExceptionUtil;
- import cn.hutool.core.io.FileUtil;
- import cn.hutool.core.util.ZipUtil;
- import cn.hutool.crypto.digest.MD5;
- import cn.hutool.http.HttpUtil;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.fdkankan.common.constant.CommonSuccessStatus;
- import com.fdkankan.contro.common.Result;
- import com.fdkankan.contro.entity.SceneOrigBd;
- import com.fdkankan.contro.httpclient.MjHttpClient;
- import com.fdkankan.contro.service.ISceneOrigBdService;
- import com.fdkankan.model.constants.ConstantFilePath;
- import com.github.brainlag.nsq.NSQMessage;
- import com.github.brainlag.nsq.callbacks.NSQMessageCallback;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import javax.annotation.Resource;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.zip.ZipFile;
- @Component
- @Slf4j
- public class NsqMesgDeal implements NSQMessageCallback {
- private static final String BD_BASE_PATH = ConstantFilePath.BUILD_MODEL_PATH + "bd/";
- @Autowired
- private ISceneOrigBdService sceneOrigBdService;
- @Resource
- private MjHttpClient mjHttpClient;
- @Override
- public void message(NSQMessage nsqMessage) {
- String msg = new String(nsqMessage.getMessage());
- String id = new String(nsqMessage.getId());
- log.info("start deal call msg, id:{}, content:{}", id, msg);
- nsqMessage.finished();
- Result content = JSON.parseObject(msg, Result.class);
- // SceneOrigBd data = content.getData();
- JSONObject obj = (JSONObject) content.getData();
- SceneOrigBd data = JSON.parseObject(obj.toJSONString(), SceneOrigBd.class);
- try {
- String path = data.getPath();
- String fileHash = data.getFileHash();
- String localPath = BD_BASE_PATH + fileHash + File.separator + fileHash + ".zip";
- HttpUtil.downloadFile(path, new File(localPath), 1000 * 60 * 60 * 4);
- String md5 = MD5.create().digestHex(new File(localPath));
- data.setLocalPath(localPath);
- //比对md5是否一致
- if(!md5.equals(fileHash)){
- throw new RuntimeException("md5不匹配");
- }
- //data.fdage临时文件路径
- String tempFdagePath = null;
- try (
- ZipFile zipFile = new ZipFile(localPath);
- InputStream ins = ZipUtil.get(zipFile, "data.fdage")
- ){
- tempFdagePath = BD_BASE_PATH + fileHash + File.separator + fileHash + ".fdage";
- FileUtil.writeFromStream(ins, tempFdagePath);
- JSONObject dataFdage = JSON.parseObject(FileUtil.readUtf8String(tempFdagePath));
- String unicode = dataFdage.getString("creator") + "_" + dataFdage.getString("uuidtime");
- data.setUnicode(unicode);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }finally {
- FileUtil.del(tempFdagePath);
- }
- //通知平台文件已下载(删除文件)
- JSONObject param = new JSONObject();
- param.put("filename", data.getFilename());
- Result notify = mjHttpClient.notify(param);
- log.info("distributeDetail-notify:{}", notify);
- }catch (Exception e){
- data.setReason(ExceptionUtil.stacktraceToString(e, 5000));
- data.setStatus(CommonSuccessStatus.FAIL.code());
- }
- sceneOrigBdService.save(data);
- nsqMessage.finished();
- log.info("end deal call msg, id:{}, content:{}", id, msg);
- }
- public static void main(String[] args) {
- try (
- ZipFile zipFile = new ZipFile("D:\\Downloads\\916ed6689_202501101516307030\\916ed6689_202501101516307030.zip");
- InputStream ins = ZipUtil.get(zipFile, "data.fdage")
- ){
- String tempFdagePath = "D:\\Downloads\\aaa.fdage";
- FileUtil.writeFromStream(ins, tempFdagePath);
- JSONObject dataFdage = JSON.parseObject(FileUtil.readUtf8String(tempFdagePath));
- String unicode = dataFdage.getString("creator") + "_" + dataFdage.getString("uuidtime");
- System.out.println(unicode);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
|