turborao 2 тижнів тому
батько
коміт
22958a23d4

+ 4 - 12
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/CusFileServiceExt.java

@@ -24,18 +24,10 @@ public class CusFileServiceExt extends FilePathService {
     public InputStream encode(String originalPath, InputStream in) {
         logger.info("--------------SEC附件加密 "+originalPath+"----------------");
 
-//        InputStream inForSEC = null;
-//        int isEncryption = FileSECUtils.checkFileIsEncryptionRest(in);
-//        if(isEncryption != 1){
-//
-//            long filesize = FileSECUtils.getFileSizeByPath(originalPath);
-//            inForSEC = FileSECUtils.decodeFileForSEC(filesize,in);
-//            if(inForSEC != null){
-//                return inForSEC;
-//            }
-//        }else{
-//            logger.info("--------------SEC 附件已加密 "+originalPath+"----------------");
-//        }
+        InputStream inForSEC = FileSECUtils.processFileWithEncodeSEC(originalPath, in);
+        if (inForSEC != null) {
+            return inForSEC;
+        }
         return in;
     }
 

+ 72 - 26
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/FileSECUtils.java

@@ -37,32 +37,6 @@ import java.util.Map;
 public class FileSECUtils {
     private static final Log logger = LogFactory.getLog(FileSECUtils.class);
 
-    public static InputStream getFileTest() {
-
-        String entryEntityName = "nckd_filetest";
-
-        DynamicObject billByn = BusinessDataServiceHelper.loadSingle(entryEntityName,
-                new QFilter[]{new QFilter("billno", QCP.equals, "test0001")});
-
-        List<Map<String, Object>> sourceFileList = AttachmentServiceHelper.getAttachments(entryEntityName,billByn.getPkValue(), "attachmentpanel");
-
-        if(sourceFileList != null && sourceFileList.size()>0)
-        {
-            for(Map<String,Object> fileMap : sourceFileList){
-                AttachmentDto attachmentDto = AttachmentServiceHelper.getAttachmentInfoByAttPk(fileMap.get("attPkId"));
-                String fileUrl= attachmentDto.getResourcePath();
-                String fileName = attachmentDto.getFilename();
-
-                System.out.println("fileUrl:" + fileUrl+ ",fileName:"+fileName);
-                ByteArrayOutputStream out = new ByteArrayOutputStream();
-                FileServiceFactory.getAttachmentFileService().download(fileUrl, out, null);
-                InputStream in = new ByteArrayInputStream(out.toByteArray());
-                return in;
-            }
-        }
-
-        return null;
-    }
 
     /**
      * 检查文件是否加密的REST接口调用方法
@@ -225,6 +199,78 @@ public class FileSECUtils {
         return httpClient;
     }
 
+    public static InputStream processFileWithEncodeSEC(String path,InputStream inputStream) {
+        File tempFile = null;
+        try {
+            logger.info("--------------SEC附件加密 processFileWithEncodeSEC ----------------");
+            String filename = getFileNameWithoutExtension(path);
+            String filetype = getFileType(path);
+            /**
+             * 判断是否是媒体文件,如果是媒体文件,则不进行处理
+             */
+            boolean isMedia = isMediaFile(filetype);
+            if(isMedia){
+                return inputStream;
+            }
+            String filepath =  System.getProperty("java.io.tmpdir") + "/" + filename;
+            // 1. 将 InputStream 写入本地临时文件
+            tempFile = File.createTempFile(filepath,"."+filetype);
+            logger.info("--------------SEC附件临时路径 "+filepath,"."+filetype+" ----------------");
+            tempFile.deleteOnExit(); // 确保 JVM 退出时删除临时文件
+
+            try (FileOutputStream fos = new FileOutputStream(tempFile)) {
+                byte[] buffer = new byte[1024];
+                int bytesRead;
+                while ((bytesRead = inputStream.read(buffer)) != -1) {
+                    fos.write(buffer, 0, bytesRead);
+                }
+            }
+            //返回临时文件输入流,不能使用的文件流
+            InputStream tmpInputStream = new FileInputStream(tempFile);
+
+            // 2. 调用是否加密接口方法
+            int isEncrypted = checkFileIsEncryptionRest(new FileInputStream(tempFile));
+            if (isEncrypted == -1) {
+                logger.info("--------------SEC附件检查,文件加密状态检查失败----------------");
+                return tmpInputStream;
+            } else if (isEncrypted == 1) {
+                logger.info("--------------SEC附件检查,文件已加密----------------");
+                return tmpInputStream;
+            }
+
+            // 3. 获取文件大小
+            long fileSize = tempFile.length();
+
+            // 4. 调用加密方法
+            InputStream encodeInputStream = encodeFileForSEC(fileSize, new FileInputStream(tempFile));
+            if (encodeInputStream == null) {
+                logger.info("--------------SEC附件加密,文件解密失败----------------");
+                return null;
+            }
+
+            // 5. 删除临时文件
+            if (!tempFile.delete()) {
+                logger.info("--------------SEC附件加密,无法删除临时文件----------------");
+            }
+
+            // 返回解密后的输入流
+            return encodeInputStream;
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            logger.info("--------------SEC附件加密 处理过程中发生错误1: " + e.getMessage());
+            return null;
+        } finally {
+            try {
+                if (inputStream != null) {
+                    inputStream.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+                logger.info("--------------SEC附件加密 处理过程中发生错误2: " + e.getMessage());
+            }
+        }
+    }
 
     public static InputStream processFileWithSEC(String path,InputStream inputStream) {
         File tempFile = null;