CommonService.java 5.16 KB
package com.idss.vulsync.mvc.service;

import cloud.agileframework.common.util.file.FileUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.idss.vulsync.config.VulSftpConfiguration;
import com.idss.vulsync.entity.AssetReportVO;
import com.idss.vulsync.mvc.mapper.ConsultVulmanageSyncEntityMapper;
import com.idss.vulsync.mvc.pojo.db.ConsultVulmanageSyncEntity;
import com.idss.vulsync.utils.MultipartFileToFile;
import com.idss.vulsync.utils.SftpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: zc
 * @Date: 2024/8/20 11:10
 */
@Slf4j
@Service
public class CommonService {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    ConsultVulmanageSyncEntityMapper consultVulmanageSyncEntityMapper;
    @Autowired
    VulSftpConfiguration vulSftpConfiguration;

    public void sync(List<AssetReportVO> dataList, ConsultVulmanageSyncEntity syncEntity, Date now, String type) {
        try {
            String dateFormat = DateUtil.format(now, DatePattern.PURE_DATETIME_FORMAT);
            String zipFileName = "";
            zipFileName = syncEntity.getTransId() + "_" + type + "_" + dateFormat;

            String parentPath = vulSftpConfiguration.getTemp();

            String roundStr = syncEntity.getLatestEsId() + File.separator + dateFormat;


            File contentLogFile = FileUtil.createFile(parentPath + File.separator + roundStr, "content.log");
            OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(contentLogFile), "utf-8");

            for (AssetReportVO entity : dataList) {
                String json= objectMapper.writeValueAsString(entity);
//                String json = JSON.toJSONString(entity, new PropertyFilter() {
//                    @Override
//                    public boolean apply(Object object, String name, Object value) {
//                        return !ListUtil.toList("SortedJson", "sortedJson").contains(name);
//                    }
//                });
                ow.append(json.concat("\r\n"));
                ow.flush();
            }
            MultipartFileToFile.writeToFile(contentLogFile, parentPath, zipFileName);
            String outputDir = parentPath + File.separator + roundStr;
            List<String> zipFileUrlList = MultipartFileToFile.splitZip(parentPath, zipFileName, 20 * 1024 * 1024, outputDir);
            if(ObjectUtil.isNotEmpty(zipFileUrlList)){
                //最后一个文件后缀改成FFFF
                String lastFileUrlOrigin = zipFileUrlList.get(zipFileUrlList.size() - 1);
                String[] split = lastFileUrlOrigin.split("_");
                split[split.length-1]="FFFF.zip";
                String lastFileUrl = Arrays.stream(split).collect(Collectors.joining("_"));
                cn.hutool.core.io.FileUtil.rename(new File(lastFileUrlOrigin),lastFileUrl,false);
                zipFileUrlList.remove(zipFileUrlList.size() - 1);
                zipFileUrlList.add(lastFileUrl);
            }

            //更新db
            syncEntity.setInitFilePath(outputDir);
            syncEntity.setLatestFilePath(outputDir);
            syncEntity.setLatestUploadTime(now);
            consultVulmanageSyncEntityMapper.updateByPrimaryKeySelective(syncEntity);

            if ("1".equals(syncEntity.getCreditType())) {
                //账号密码字符串
                String pwd = cn.hutool.core.codec.Base64.decodeStr(syncEntity.getCredit());
                String userAndPort = syncEntity.getUser();
                String[] split = userAndPort.split(":");
                SftpUtil sftpUtil = new SftpUtil(split[0], pwd, vulSftpConfiguration.getIp(), Integer.parseInt(split[1]));
                try {
                    sftpUtil.login();
                    if (ObjectUtil.isNotEmpty(zipFileUrlList)) {
                        log.info("同步初始数据文件{}个", zipFileUrlList.size());
                        for (String fileUrl : zipFileUrlList) {
                            sftpUtil.upload(syncEntity.getFilePath(), fileUrl);
                        }
                    }
                }catch (Exception e){
                    log.error("ftpy异常",e);
                }finally {
                    sftpUtil.logout();
                }
            } else if ("2".equals(syncEntity.getCreditType())) {
                //账号公钥
            }
        } catch (Exception e) {
            log.error("{}同步数据异常", type, e);
        }

    }

    public static void main(String[] args) throws Exception{
        String a="idss@1234";
        byte[] encode = Base64.getEncoder().encode(a.getBytes());
        String string = new String(encode,"UTF-8");
        System.out.println(string);
    }
}