Przeglądaj źródła

OssConsoleClient 添加一个不需要认证的构造函数, 只给 oss-store 内部调用

reghao 2 lat temu
rodzic
commit
a0d14d040d

+ 5 - 4
oss-sdk/src/main/java/cn/reghao/oss/sdk/OssConsoleClient.java

@@ -32,6 +32,10 @@ public class OssConsoleClient {
         auth(accessKeyId, accessKeySecret);
     }
 
+    public OssConsoleClient(String endpoint) {
+        this.endpoint = endpoint;
+    }
+
     private void auth(String accessKeyId, String accessKeySecret) throws Exception {
         MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
                 .addPart("accessKeyId", accessKeyId)
@@ -63,10 +67,9 @@ public class OssConsoleClient {
     }
 
     public void registerNode(String jsonPayload) throws Exception {
-        String api = String.format("%s/api/oss/store/node", endpoint);
+        String api = String.format("%s/api/oss/store/node/register", endpoint);
         HttpClient httpClient = HttpClient.newBuilder().build();
         HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
-                .header("authorization", "Bearer " + token)
                 .header("content-type", "application/json")
                 .POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
                 .build();
@@ -114,7 +117,6 @@ public class OssConsoleClient {
     public List<ObjectChannel> getObjectChannels(String nodeAddr) throws Exception {
         String api = String.format("%s/api/oss/channels?nodeAddr=%s", endpoint, nodeAddr);
         HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
-                .header("authorization", "Bearer " + token)
                 .version(HttpClient.Version.HTTP_1_1)
                 .GET()
                 .build();
@@ -139,7 +141,6 @@ public class OssConsoleClient {
     public ObjectChannel getObjectChannel(String nodeAddr, int channelId) throws Exception {
         String api = String.format("%s/api/oss/channel?nodeAddr=%s&channelId=%s", endpoint, nodeAddr, channelId);
         HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
-                .header("authorization", "Bearer " + token)
                 .version(HttpClient.Version.HTTP_1_1)
                 .GET()
                 .build();

+ 1 - 1
oss-web/src/main/java/cn/reghao/oss/web/app/controller/page/StoreNodeController.java

@@ -28,7 +28,7 @@ public class StoreNodeController {
     }
 
     @ApiOperation(value = "添加存储节点")
-    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
+    @PostMapping(value = "/register", produces = MediaType.APPLICATION_JSON_VALUE)
     public String addStoreNode(@RequestBody @Validated StoreNode storeNode) {
         storeNodeService.add(storeNode);
         return WebResult.success();

+ 1 - 2
oss-web/src/main/java/cn/reghao/oss/web/app/model/dto/UploadChannelDto.java

@@ -18,7 +18,6 @@ public class UploadChannelDto {
     private String channelPrefix;
     @NotNull
     private Long maxSize;
-    @NotBlank(message = "")
-    @Length(max = 20, message = "新仓库分支最大长度为 40 个字符")
+    @Length(max = 20, message = "简介最大长度为 40 个字符")
     private String description;
 }

+ 2 - 1
oss-web/src/main/java/cn/reghao/oss/web/app/service/OssService.java

@@ -7,6 +7,7 @@ import cn.reghao.oss.web.app.db.repository.StoreNodeRepository;
 import cn.reghao.oss.web.app.db.repository.UploadChannelRepository;
 import cn.reghao.oss.web.app.model.po.StoreNode;
 import cn.reghao.oss.web.app.model.po.UploadChannel;
+import cn.reghao.oss.web.util.AuthKeyContext;
 import org.springframework.stereotype.Service;
 
 import java.util.Collections;
@@ -28,7 +29,7 @@ public class OssService {
     }
 
     public ServerInfo getServerInfo(int channelId) throws Exception {
-        int userId = 1;
+        int userId = AuthKeyContext.getUser();
         UploadChannel uploadChannel = uploadChannelRepository.findByChannelIdAndCreateBy(channelId, userId);
         if (uploadChannel == null) {
             String errMsg = String.format("channel_id %s not exist", channelId);

+ 4 - 4
oss-web/src/main/java/cn/reghao/oss/web/app/service/UploadChannelService.java

@@ -32,14 +32,15 @@ public class UploadChannelService {
             return Result.fail(String.format("store_node with domain %s not exist", domain));
         }
 
-        int createBy = UserContext.getUser().getId();
+        //int createBy = UserContext.getUser().getId();
+        int createBy = 410;
         String channelPrefix = uploadChannelDto.getChannelPrefix();
         UploadChannel uploadChannel = uploadChannelRepository.findByPrefixAndCreateBy(channelPrefix, createBy);
         if (uploadChannel != null) {
             return Result.fail(String.format("channel_prefix %s exist", channelPrefix));
         }
 
-        int channelId = getNextChannelId();
+        int channelId = getNextChannelId(createBy);
         uploadChannel = new UploadChannel(channelId, uploadChannelDto, createBy);
         uploadChannelRepository.save(uploadChannel);
 
@@ -56,9 +57,8 @@ public class UploadChannelService {
         return Result.success();
     }
 
-    private int getNextChannelId() {
+    private int getNextChannelId(int userId) {
         int channelId = 1001;
-        int userId = UserContext.getUser().getId();
         int total = uploadChannelRepository.countByCreateBy(userId);
         return channelId + total;
     }

+ 22 - 0
oss-web/src/test/java/AccountTest.java

@@ -1,5 +1,8 @@
+import cn.reghao.oss.api.constant.UploadChannel;
 import cn.reghao.oss.web.OssConsoleApplication;
 import cn.reghao.oss.web.account.service.AccountService;
+import cn.reghao.oss.web.app.model.dto.UploadChannelDto;
+import cn.reghao.oss.web.app.service.UploadChannelService;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -23,4 +26,23 @@ public class AccountTest {
     @Test
     public void createUserAccount() {
     }
+
+    @Autowired
+    UploadChannelService uploadChannelService;
+    @Test
+    public void initChannel() {
+        for (UploadChannel uploadChannel : UploadChannel.values()) {
+            String name = uploadChannel.name();
+            long maxSize = uploadChannel.getMaxSize();
+            String prefix = uploadChannel.getPrefix();
+
+            UploadChannelDto uploadChannelDto = new UploadChannelDto();
+            uploadChannelDto.setDomain("oss.reghao.cn");
+            uploadChannelDto.setChannelPrefix(prefix);
+            uploadChannelDto.setMaxSize(maxSize);
+            uploadChannelDto.setDescription(name);
+
+            uploadChannelService.add(uploadChannelDto);
+        }
+    }
 }