Ver Fonte

预览解密扩展

wanghaiwu há 3 dias atrás
pai
commit
af97740c06

+ 44 - 0
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/CustomPreviewExtension.java

@@ -0,0 +1,44 @@
+package nckd.base.helper;
+
+import kd.bos.fileservice.extension.PreviewExt;
+import kd.bos.logging.Log;
+import kd.bos.logging.LogFactory;
+import org.springframework.util.StreamUtils;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * 预览解密扩展类
+ * @author wanghaiwu_kd
+ * @date 2025/06/26
+ */
+public class CustomPreviewExtension implements PreviewExt {
+    private static final Log logger = LogFactory.getLog(CustomPreviewExtension.class);
+
+    @Override
+    public InputStream beforePreviewTempFile(String tempFileUrl, InputStream in) {
+        // 临时⽂件预览
+        // 对 原始⽂件流 in 进⾏处理,原始⽂件流使⽤后需关闭。
+        logger.info("--------------beforePreviewTempFile 解密 开始");
+
+        InputStream decodedIn = FileSECUtils.processFileWithSEC(tempFileUrl, in);
+
+        logger.info("--------------beforePreviewTempFile 解密 结束");
+
+        return decodedIn; // 返回处理后的⽂件流
+    }
+
+    @Override
+    public InputStream beforePreviewFile(String originPath, InputStream in) {
+        // 持久化⽂件预览
+        // 对原始⽂件流 in 进⾏处理,原始⽂件流使⽤后需关闭。
+        logger.info("--------------beforePreviewFile 解密 开始");
+
+        InputStream decodedIn = FileSECUtils.processFileWithSEC(originPath, in);
+
+        logger.info("--------------beforePreviewFile 解密 结束");
+
+        return decodedIn; // 返回处理后的⽂件流
+    }
+}

+ 57 - 38
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/FileSECUtils.java

@@ -24,6 +24,7 @@ import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.ssl.SSLContexts;
+import org.springframework.util.StreamUtils;
 
 import javax.net.ssl.SSLContext;
 
@@ -145,7 +146,7 @@ public class FileSECUtils {
         return null;
     }
 
-    public static InputStream decodeFileForSEC(long fileSize,InputStream inputStream){
+    public static InputStream decodeFileForSEC(int fileSize,InputStream inputStream){
         Map<String, String> mapentity = CommonHelperUtils.getCommonParams("SEC");
         if(mapentity == null){
             return null;
@@ -276,59 +277,77 @@ public class FileSECUtils {
         }
     }
 
-    public static InputStream processFileWithSEC(String path,InputStream inputStream) {
-        File tempFile = null;
-        try {
-            logger.info("--------------SEC附件解密 processFileWithSEC1 ----------------");
-            String filename = getFileNameWithoutExtension(path);
-            String filetype = getFileType(filename);
-            /**
-             * 判断是否是媒体文件,如果是媒体文件,则不进行处理
-             */
-            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 退出时删除临时文件
+    public static InputStream processFileWithSEC(String path, InputStream inputStream) {
+//        InputStream tmpInputStream = inputStream;
 
-            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);
 
+////        File tempFile = null;
+        try {
+//            logger.info("--------------SEC附件解密 processFileWithSEC1 ----------------");
+////            String filename = getFileNameWithoutExtension(path);
+////            String filetype = getFileType(filename);
+////            /**
+////             * 判断是否是媒体文件,如果是媒体文件,则不进行处理
+////             */
+////            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);
+////                }
+////            }
+////            //返回临时文件输入流,不能使用的文件流
+//
+//            if(true){
+//                return inputStream;
+//            }
+//
+//
+//            InputStream tmpInputStream = inputStream;//new FileInputStream(tempFile);
+//
+//
+//
             // 2. 调用是否加密接口方法
-            int isEncrypted = checkFileIsEncryptionRest(new FileInputStream(tempFile));
+            int isEncrypted = checkFileIsEncryptionRest(inputStream);
             if (isEncrypted == -1) {
                 logger.info("--------------SEC附件检查,文件加密状态检查失败----------------");
-                return tmpInputStream;
+                return inputStream;
             } else if (isEncrypted == 0) {
                 logger.info("--------------SEC附件检查,是明文无需解密----------------");
-                return tmpInputStream;
+                return inputStream;
             }
+            int fileSize = 0;
+
+            byte[] bytes = StreamUtils.copyToByteArray(inputStream);
+            fileSize = inputStream.read(bytes);
+
+            logger.info("--------------SEC附件解密,文件大小:" + fileSize + "----------------");
 
             // 3. 获取文件大小
-            long fileSize = tempFile.length();
+//            int fileSize = tempFile.length();
 
             // 4. 调用解密方法
-            InputStream decryptedInputStream = decodeFileForSEC(fileSize, new FileInputStream(tempFile));
+            InputStream decryptedInputStream = decodeFileForSEC(fileSize, inputStream);
             if (decryptedInputStream == null) {
                 logger.info("--------------SEC附件解密,文件解密失败----------------");
-                return tmpInputStream;
+                return inputStream;
             }
 
-            // 5. 删除临时文件
-            if (!tempFile.delete()) {
-                logger.info("--------------SEC附件解密,无法删除临时文件----------------");
-            }
+
+//            // 5. 删除临时文件
+//            if (!tempFile.delete()) {
+//                logger.info("--------------SEC附件解密,无法删除临时文件----------------");
+//            }
 
             // 返回解密后的输入流
             return decryptedInputStream;