Kaynağa Gözat

update file-service OpenApiClient

reghao 3 hafta önce
ebeveyn
işleme
58176e7a94

+ 16 - 32
file/file-service/src/main/java/cn/reghao/tnb/file/app/service/OpenApiClient.java

@@ -5,11 +5,7 @@ import cn.reghao.jutil.jdk.io.FileSplitter;
 import cn.reghao.jutil.jdk.serializer.JsonConverter;
 import cn.reghao.jutil.jdk.web.result.WebResult;
 import cn.reghao.oss.api.dto.ServerInfo;
-import cn.reghao.oss.api.dto.UploadFilePart;
-import cn.reghao.oss.api.dto.rest.UploadFileRet;
-import cn.reghao.oss.api.dto.rest.UploadPrepare;
-import cn.reghao.oss.api.dto.rest.UploadPrepareRet;
-import cn.reghao.oss.api.dto.rest.UploadSample;
+import cn.reghao.oss.api.dto.rest.*;
 import cn.reghao.oss.api.util.OssSamplingHash;
 import cn.reghao.oss.sdk.MultiPartBodyPublisher;
 import com.google.gson.reflect.TypeToken;
@@ -70,7 +66,7 @@ public class OpenApiClient {
         String url = this.baseUrl + path;
         return HttpRequest.newBuilder()
                 .uri(URI.create(url))
-                .timeout(Duration.ofSeconds(5)) // 请求超时
+                .timeout(Duration.ofSeconds(60)) // 请求超时
                 .header("Content-Type", "application/json; charset=UTF-8")
                 .header("X-AK", this.accessKey)
                 .header("X-Timestamp", timestamp)
@@ -104,12 +100,10 @@ public class OpenApiClient {
         }
     }
 
-    private UploadPrepareRet prepareUpload(int channelCode, Path filePath) throws Exception {
-        // 1. 计算 SHA-256 (用于后端秒传校验)
-        String sha256 = OssSamplingHash.calculateFullHash(filePath);
+    private UploadPrepareRet prepareUpload(String clientSha256sum, int channelCode, Path filePath) throws Exception {
         String filename0 = filePath.getFileName().toString();
         long size = filePath.toFile().length();
-        UploadPrepare uploadPrepare = new UploadPrepare(channelCode, filename0, size, sha256);
+        UploadPrepare uploadPrepare = new UploadPrepare(channelCode, filename0, size, clientSha256sum);
         String jsonBody = JsonConverter.objectToJson(uploadPrepare);
         String api = "/api/open/file/prepare_upload";
 
@@ -158,7 +152,9 @@ public class OpenApiClient {
     }
 
     private UploadFileRet uploadFilePart(int channelCode, String uploadUrl, String token, Path filePath) throws Exception {
-        UploadPrepareRet uploadPrepareRet = prepareUpload(channelCode, filePath);
+        // 1. 计算 SHA-256 (用于后端秒传校验)
+        String clientSha256sum = OssSamplingHash.calculateFullHash(filePath);
+        UploadPrepareRet uploadPrepareRet = prepareUpload(clientSha256sum, channelCode, filePath);
         if (uploadPrepareRet.isExist()) {
             UploadFileRet uploadFileRet = checkSample(filePath, uploadPrepareRet);
             if (uploadFileRet.isFastUpload()) {
@@ -190,7 +186,7 @@ public class OpenApiClient {
             map.put(chunkNumber, start);
 
             int currentChunkSize = part.length;
-            UploadFilePart uploadFilePart = new UploadFilePart(channelCode, identifier, filename, relativePath,
+            UploadFilePart uploadFilePart = new UploadFilePart(clientSha256sum, uploadId, identifier, filename, relativePath,
                     totalSize, chunkSize, totalChunks, chunkNumber, currentChunkSize);
 
             UploadFileRet uploadFileRet = postObject(uploadUrl, token, part, uploadFilePart);
@@ -209,7 +205,8 @@ public class OpenApiClient {
     private UploadFileRet postObject(String uploadUrl, String uploadToken, byte[] bytes, UploadFilePart uploadFilePart) throws Exception {
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher();
         publisher
-                .addPart("channelCode", uploadFilePart.getChannelCode()+"")
+                .addPart("clientSha256sum", uploadFilePart.getClientSha256sum())
+                .addPart("uploadId", uploadFilePart.getUploadId())
                 .addPart("identifier", uploadFilePart.getIdentifier())
                 .addPart("filename", uploadFilePart.getFilename())
                 .addPart("relativePath", uploadFilePart.getRelativePath())
@@ -243,12 +240,11 @@ public class OpenApiClient {
         return null;
     }
 
-    public UploadFileRet uploadSingleFile(String uploadUrl, String uploadToken, Path filePath) throws Exception {
+    public UploadFileRet putFile(String uploadUrl, String uploadToken, Path filePath) throws Exception {
         // 1. 计算 SHA-256 (用于后端秒传校验)
         String sha256 = OssSamplingHash.calculateFullHash(filePath);
 
         String contentType = "application/octet-stream";
-        int channelCode = 101;
         String filename = filePath.getFileName().toString();
         // 2. 构造请求
         // 我们把元数据放在 Header 里,Body 放文件流
@@ -258,11 +254,8 @@ public class OpenApiClient {
                 .header("Content-Type", contentType)
                 .header("X-File-SHA256", sha256)
                 .header("X-File-Name", filename)
-                .header("X-Channel-Code", channelCode+"")
-                // JDK 17 的 BodyPublishers.ofFile 是流式的,不会爆内存
-                .POST(HttpRequest.BodyPublishers.ofFile(filePath))
+                .PUT(HttpRequest.BodyPublishers.ofFile(filePath))
                 .build();
-
         // 3. 发送请求
         HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
         if (response.statusCode() != 200) {
@@ -297,7 +290,7 @@ public class OpenApiClient {
 
     public void updateAvatar(File avatarFile) throws Exception {
         ServerInfo serverInfo1 = getServerInfo("image");
-        UploadFileRet uploadFileRet1 = uploadSingleFile(serverInfo1.getOssUrl(), serverInfo1.getToken(), Path.of(avatarFile.getPath()));
+        UploadFileRet uploadFileRet1 = putFile(serverInfo1.getOssUrl(), serverInfo1.getToken(), Path.of(avatarFile.getPath()));
         String  avatarFileId = uploadFileRet1.getUploadId();
 
         String path = "/api/open/file/update_avatar";
@@ -313,12 +306,8 @@ public class OpenApiClient {
     }
 
     public void publishVideoPost(File coverFile, File videoFile) throws Exception {
-        if (coverFile == null) {
-            coverFile = new File(getCoverPath(videoFile));
-        }
-
         ServerInfo serverInfo1 = getServerInfo("image");
-        UploadFileRet uploadFileRet1 = uploadSingleFile(serverInfo1.getOssUrl(), serverInfo1.getToken(), Path.of(coverFile.getPath()));
+        UploadFileRet uploadFileRet1 = putFile(serverInfo1.getOssUrl(), serverInfo1.getToken(), Path.of(coverFile.getPath()));
         String coverFileId = uploadFileRet1.getUploadId();
 
         ServerInfo serverInfo2 = getServerInfo("video");
@@ -327,7 +316,6 @@ public class OpenApiClient {
         String videoFileId = uploadFileRet2.getUploadId();
         Map<String, Object> map0 = new HashMap<>();
         map0.put("videoFileId", videoFileId);
-        map0.put("channelCode", 101);
         map0.put("filename", videoFile.getName());
 
         String path0 = "/api/open/content/vod/publish/file";
@@ -355,17 +343,13 @@ public class OpenApiClient {
         Type type1 = new TypeToken<WebResult<String>>(){}.getType();
         WebResult<String> webResult1 = JsonConverter.jsonToObject(body1, type1);
         if (webResult1.getCode() != 0) {
-            throw new RuntimeException("post failed: " + webResult.getMsg());
+            throw new RuntimeException("post failed: " + webResult1.getMsg());
         }
     }
 
-    private String getCoverPath(File videoFile) {
-        return "";
-    }
-
     public void updateVideoCover(String videoId, File coverFile) throws Exception {
         ServerInfo serverInfo1 = getServerInfo("image");
-        UploadFileRet uploadFileRet1 = uploadSingleFile(serverInfo1.getOssUrl(), serverInfo1.getToken(), Path.of(coverFile.getPath()));
+        UploadFileRet uploadFileRet1 = putFile(serverInfo1.getOssUrl(), serverInfo1.getToken(), Path.of(coverFile.getPath()));
         String  coverFileId = uploadFileRet1.getUploadId();
 
         String path = "/api/open/content/vod/update/cover";

+ 0 - 1
file/file-service/src/main/java/cn/reghao/tnb/file/app/zdisk/service/CamRecordService.java

@@ -12,7 +12,6 @@ import cn.reghao.tnb.file.app.zdisk.model.constant.AlbumType;
 import cn.reghao.tnb.file.app.zdisk.model.query.CamDeviceQuery;
 import cn.reghao.tnb.file.app.zdisk.model.vo.CamRecordDetail;
 import cn.reghao.tnb.file.app.zdisk.model.vo.CamRecordInfo;
-import cn.reghao.oss.api.dto.media.VideoInfo;
 import cn.reghao.tnb.common.auth.UserContext;
 import cn.reghao.tnb.file.app.zdisk.model.dto.CamRecordDto;
 import cn.reghao.tnb.file.app.zdisk.model.po.CamDevice;