管道基础大数据平台系统开发-【后端】-Server
1.5
13693261870
2023-01-05 e5f61db766b3eaa28506835e3581cbd2fdc8533d
src/main/java/com/lf/server/service/data/DownloadService.java
@@ -2,11 +2,13 @@
import com.lf.server.entity.ctrl.DownloadReqEntity;
import com.lf.server.entity.data.DownloadEntity;
import com.lf.server.entity.data.MetaFileEntity;
import com.lf.server.entity.data.MetaEntity;
import com.lf.server.entity.show.PipelineEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.*;
import com.lf.server.mapper.data.DownloadMapper;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipParameters;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -116,15 +118,15 @@
    }
    /**
     * 校验密码有效性
     * 解密
     *
     * @param reqEntity 请求下载实体类
     * @return 是/否有效
     * @return 是/否解密成功
     */
    public boolean validatePwd(DownloadReqEntity reqEntity) {
    public static boolean decryptPwd(DownloadReqEntity reqEntity) {
        try {
            String pwd = RsaHelper.decrypt(reqEntity.getPwd());
            if (StringHelper.isEmpty(pwd) || !StringHelper.checkPwdValid(pwd)) {
            if (StringHelper.isEmpty(pwd)) {
                return false;
            }
@@ -140,10 +142,32 @@
    /**
     * 解密
     *
     * @param pe 管道分析实体类
     * @return 是/否解密成功
     */
    public static boolean decryptPwd(PipelineEntity pe) {
        try {
            String pwd = RsaHelper.decrypt(pe.getPwd());
            if (StringHelper.isEmpty(pwd)) {
                return false;
            }
            pe.setPwd(pwd);
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return false;
        }
    }
    /**
     * 解密
     *
     * @param pwd 加密密码
     * @return 原始密码
     */
    public String decryptPwd(String pwd) {
    public static String decryptPwd(String pwd) {
        try {
            return RsaHelper.decrypt(pwd);
        } catch (Exception ex) {
@@ -160,7 +184,7 @@
     * @param pwd  密码
     * @return 下载文件GUID
     */
    public String zipFiles(UserEntity ue, List<MetaFileEntity> list, String pwd) {
    public String zipFiles(UserEntity ue, List<MetaEntity> list, String pwd) throws Exception {
        rmRepeatMetaFiles(list);
        String downloadPath = pathHelper.getDownloadFullPath();
@@ -169,21 +193,24 @@
        ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd);
        ZipParameters params = Zip4jHelper.getZipParams();
        addMetaFiles(zip, params, list);
        return null;
        String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd));
        DownloadEntity downloadEntity = getDownloadEntity(ue, zipFile, dbPwd);
        int rows = downloadMapper.insert(downloadEntity);
        return rows > 0 ? downloadEntity.getGuid() : null;
    }
    /**
     * 移除重复的元数据文件
     */
    private void rmRepeatMetaFiles(List<MetaFileEntity> list) {
    private void rmRepeatMetaFiles(List<MetaEntity> list) {
        List<String> guidList = new ArrayList<>();
        int i = 0;
        while (i < list.size()) {
            MetaFileEntity entity = list.get(i);
            MetaEntity entity = list.get(i);
            if (guidList.contains(entity.getGuid())) {
                list.remove(i);
                continue;
@@ -197,21 +224,43 @@
    /**
     * 添加元数据文件至Zip包
     */
    private void addMetaFiles(ZipFile zip, ZipParameters params, List<MetaFileEntity> list) {
    private void addMetaFiles(ZipFile zip, ZipParameters params, List<MetaEntity> list) {
        String uploadPath = pathHelper.getConfig().getUploadPath();
        for (MetaFileEntity entity : list) {
            String filePath = uploadPath + File.separator + entity.getPath();
            File file = new File(filePath);
            File newFile = new File(filePath.replace(entity.getGuid(), entity.getName()));
        int i = 1;
        for (MetaEntity entity : list) {
            try {
                file.renameTo(newFile);
                zip.addFile(newFile, params);
                File file = new File(uploadPath + File.separator + entity.getPath());
                zip.addFile(file, params);
                FileHeader header = zip.getFileHeader(entity.getGuid());
                if (null != header) {
                    zip.renameFile(header, i++ + "_" + entity.getName());
                }
            } catch (Exception ex) {
                log.error(ex.getMessage(), ex);
            } finally {
                newFile.renameTo(file);
            }
        }
    }
    /**
     * 获取下载实体类
     */
    private DownloadEntity getDownloadEntity(UserEntity ue, String file, String pwd) throws Exception {
        DownloadEntity de = new DownloadEntity();
        de.setName(FileHelper.getFileName(file));
        // 1-Shp文件,2-专题图,3-元数据
        de.setType(3);
        de.setSizes(FileHelper.sizeToMb(new File(file).length()));
        de.setDepid(ue.getDepid());
        de.setDcount(0);
        de.setPwd(pwd);
        de.setUrl(FileHelper.getRelativePath(file));
        de.setDescr("元数据文件");
        de.setGuid(FileHelper.getFileMd5(file));
        de.setCreateUser(ue.getId());
        // de.setGeom(null)
        return de;
    }
}