Skip to content
CisController.java 4.93 KiB
Newer Older
杨郁彬's avatar
杨郁彬 committed
package com.cesgroup.bdc.cis.controller;

import com.cesgroup.bdc.cis.service.CisService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.*;

/**
 * @author: YYB
 * @description: CIS文件加密控制类
 * @date: 2024/2/8.
 * @modified by:
 */
@Slf4j
@Controller
@RequestMapping("/cis")
public class CisController {

    @Autowired
    private CisService cisService;

    @Value("${file.upload.path}")
    private String fileBasePath;

    /**
     * 目录(文件)加密调试
     * @param dir 目录(文件)路径 如:20240304 或者 20240304/123.ofd
     * @param request
     * @return
     */
    @RequestMapping("/encrypted/{dir}/**")
    @ResponseBody
    public Map<String, String> encrypted(@PathVariable String dir, HttpServletRequest request) {
        Map<String, String> mm = new HashMap<>();
        boolean success = false;
        String path =
                request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
        path = path.replace("/cis/encrypted/","");
        path = path.replace("\\","/");
        String filePath = fileBasePath + path;
        List<String> failFiles = new ArrayList<>();
        this.recursiveEncrypted(new File(filePath),failFiles);
        if(failFiles.size()>0){
            mm.put("status", "fail");
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonString = null;
            try {
                jsonString = objectMapper.writeValueAsString(failFiles);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            mm.put("failFiles", jsonString);
        }else{
            mm.put("status", "success");
        }
        return mm;
    }

    /**
     * 递归调用加密
     * @param file 文件
     * @param failFiles 加密错误文件路径信息
     */
    private void recursiveEncrypted(File file,List<String> failFiles){
        if (file != null && file.exists()) {
            if (file.isDirectory()) {
                for (File child : file.listFiles()) {
                    recursiveEncrypted(child,failFiles); // 递归调用自身处理子目录
                }
            } else {
//                System.out.println("文件名:"+file.getName());
                boolean success = cisService.encryptedFile(file);
                if(!success){
                    failFiles.add(file.getAbsolutePath());
                }
            }
        }
    }

    /**
     * 目录(文件)解密调试
     * @param dir 目录(文件)路径 如:jmdir/20240304 或者 jmdir/20240304/123.ofd
     * @param request
     * @return
     */
    @RequestMapping("/decrypted/{dir}/**")
    @ResponseBody
    public Map<String, String> decrypted(@PathVariable String dir, HttpServletRequest request) {
        Map<String, String> mm = new HashMap<>();
        String path =
                request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
        path = path.replace("/cis/decrypted/","");
        path = path.replace("\\","/");
        String filePath = fileBasePath + path;
        List<String> failFiles = new ArrayList<>();
        this.recursiveDecrypted(new File(filePath),failFiles);
        if(failFiles.size()>0){
            mm.put("status", "fail");
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonString = null;
            try {
                jsonString = objectMapper.writeValueAsString(failFiles);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            mm.put("failFiles", jsonString);
        }else{
            mm.put("status", "success");
        }
        return mm;
    }

    /**
     * 递归调用解密方法
     * @param file 已加密文件
     * @param failFiles
     */
    private void recursiveDecrypted(File file,List<String> failFiles){
        if (file != null && file.exists()) {
            if (file.isDirectory()) {
                for (File child : file.listFiles()) {
                    recursiveDecrypted(child,failFiles); // 递归调用自身处理子目录
                }
            } else {
//                System.out.println("文件名:"+file.getName());
                boolean success = cisService.decryptedFile(file);
                if(!success){
                    failFiles.add(file.getAbsolutePath());
                }
            }
        }
    }
}