浏览代码

修改DiskStore中校验写入md5的方法,改为重复计算一次临时文件md5值

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 3 年之前
父节点
当前提交
cb9cf5e492

+ 21 - 0
core-fss/src/main/java/com/qmth/boot/core/fss/store/FileStore.java

@@ -18,6 +18,27 @@ public interface FileStore {
     default void close() {
     }
 
+    /**
+     * 将md5内容转换为hexString格式
+     *
+     * @param md5
+     * @return
+     */
+    default String toHexString(String md5) {
+        if (StringUtils.isBlank(md5)) {
+            throw new IllegalArgumentException("Invalid md5 parameter");
+        }
+        //hex格式
+        if (md5.length() == 32) {
+            return md5;
+        }
+        //base64格式
+        else if (md5.length() == 24) {
+            return ByteArray.fromBase64(md5).toHexString();
+        }
+        throw new IllegalArgumentException("Invalid md5 length");
+    }
+
     /**
      * 将md5内容转换为base64格式
      *

+ 11 - 7
core-fss/src/main/java/com/qmth/boot/core/fss/store/impl/DiskStore.java

@@ -1,15 +1,14 @@
 package com.qmth.boot.core.fss.store.impl;
 
 import com.qmth.boot.core.fss.store.FileStore;
-import com.qmth.boot.tools.codec.CodecUtils;
 import com.qmth.boot.tools.io.IOUtils;
+import com.qmth.boot.tools.models.ByteArray;
 import com.qmth.boot.tools.uuid.FastUUID;
 
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.InputStream;
-import java.security.MessageDigest;
 
 /**
  * 本地磁盘文件管理工具
@@ -53,15 +52,20 @@ public class DiskStore implements FileStore {
         //临时目录创建临时文件
         File tempFile = new File(tempDir, FastUUID.get());
         try {
-            final MessageDigest digest = CodecUtils.getMd5();
+            //final MessageDigest digest = CodecUtils.getMd5();
             FileOutputStream ous = new FileOutputStream(tempFile);
-            IOUtils.copy(ins, ous, (buffer, length) -> digest.update(buffer, 0, length));
+            IOUtils.copy(ins, ous);
+            //IOUtils.copy(ins, ous, (buffer, length) -> digest.update(buffer, 0, length));
             //校验写入内容的摘要信息
-            if (!CodecUtils.toBase64(digest.digest()).equalsIgnoreCase(toBase64(md5))) {
-                throw new RuntimeException("Write md5 validate faile");
-            }
+            //if (!CodecUtils.toBase64(digest.digest()).equalsIgnoreCase(toBase64(md5))) {
+            //throw new RuntimeException("Write md5 validate faile");
+            //}
             IOUtils.closeQuietly(ins);
             IOUtils.closeQuietly(ous);
+            //校验写入内容的摘要信息
+            if (!ByteArray.md5(tempFile).toBase64().equalsIgnoreCase(toBase64(md5))) {
+                throw new RuntimeException("Write md5 validate faile");
+            }
             //检查正式文件及目录
             File targetFile = new File(rootDir, path);
             if (targetFile.isDirectory()) {