reghao 2 lat temu
rodzic
commit
31f03f80c4

+ 32 - 22
oss-sdk/src/main/java/cn/reghao/oss/sdk/ObjectUploadService.java

@@ -13,6 +13,7 @@ import lombok.extern.slf4j.Slf4j;
 
 import java.io.BufferedInputStream;
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.lang.reflect.Type;
 import java.net.URI;
@@ -21,6 +22,7 @@ import java.net.http.HttpRequest;
 import java.net.http.HttpResponse;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.security.NoSuchAlgorithmException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.function.Supplier;
@@ -86,8 +88,8 @@ public class ObjectUploadService {
             MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
                     .addPart("file", Paths.get(file.getAbsolutePath()))
                     .addPart("channelId", ""+channelId)
-                    .addPart("client", "client");
-                    //.addPart("sha256sum", sha256sum)
+                    .addPart("client", "client")
+                    .addPart("sha256sum", sha256sum);
 
             String api = String.format("%s/", endpoint);
             HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
@@ -108,28 +110,36 @@ public class ObjectUploadService {
     }
 
     public UploadFileRet postObject(File file, int channelId, long userId) {
-        Map<String, String> map = new HashMap<>();
-        map.put("channelId", ""+channelId);
-        map.put("client", "client");
-
-        UploadParam uploadParam = new UploadParam(file, map);
-        String api = String.format("%s/", endpoint);
-        WebResponse webResponse = webRequest.upload1(api, uploadParam, userId);
-        int statusCode = webResponse.getStatusCode();
-        String body = webResponse.getBody();
-        if (statusCode != 200) {
-            log.error("{} -> {}", statusCode, body);
-            return null;
-        }
+        try {
+            String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
+            Map<String, String> map = new HashMap<>();
+            map.put("channelId", ""+channelId);
+            map.put("client", "client");
+            map.put("userId", ""+userId);
+            map.put("sha256sum", sha256sum);
+            UploadParam uploadParam = new UploadParam(file, map);
 
-        Type type = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
-        WebResult<UploadFileRet> webResult = JsonConverter.jsonToObject(body, type);
-        if (webResult.getCode() != 0) {
-            log.error("{}", webResult.getMsg());
-            return null;
+            String api = String.format("%s/", endpoint);
+            WebResponse webResponse = webRequest.upload(api, uploadParam);
+            int statusCode = webResponse.getStatusCode();
+            String body = webResponse.getBody();
+            if (statusCode != 200) {
+                log.error("{} -> {}", statusCode, body);
+                return null;
+            }
+
+            Type type = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
+            WebResult<UploadFileRet> webResult = JsonConverter.jsonToObject(body, type);
+            if (webResult.getCode() != 0) {
+                log.error("{}", webResult.getMsg());
+                return null;
+            }
+
+            return webResult.getData();
+        } catch (Exception e) {
+            e.printStackTrace();
         }
-
-        return webResult.getData();
+        return null;
     }
 
     private UploadFileRet getResult(HttpResponse<String> httpResponse) {