Kaynağa Gözat

UploadChannel model 中使用 StoreNode 的 id 字段来引用 StoreNode 对象

reghao 2 yıl önce
ebeveyn
işleme
ecb95ce1b1

+ 2 - 2
oss-console/src/main/java/cn/reghao/oss/console/app/controller/page/StoreChannelController.java → oss-console/src/main/java/cn/reghao/oss/console/app/controller/page/UploadChannelController.java

@@ -19,10 +19,10 @@ import org.springframework.web.bind.annotation.*;
 @Api(tags = "上传通道接口")
 @RestController
 @RequestMapping("/api/store/channel")
-public class StoreChannelController {
+public class UploadChannelController {
     private final UploadChannelService uploadChannelService;
 
-    public StoreChannelController(UploadChannelService uploadChannelService) {
+    public UploadChannelController(UploadChannelService uploadChannelService) {
         this.uploadChannelService = uploadChannelService;
     }
 

+ 4 - 3
oss-console/src/main/java/cn/reghao/oss/console/app/controller/page/StoreChannelPageController.java → oss-console/src/main/java/cn/reghao/oss/console/app/controller/page/UploadChannelPageController.java

@@ -28,11 +28,11 @@ import java.util.stream.Collectors;
 @Api(tags = "上传通道页面")
 @Controller
 @RequestMapping("/store/channel")
-public class StoreChannelPageController {
+public class UploadChannelPageController {
     private final UploadChannelService uploadChannelService;
     private final UserNodeService userNodeService;
 
-    public StoreChannelPageController(UploadChannelService uploadChannelService, UserNodeService userNodeService) {
+    public UploadChannelPageController(UploadChannelService uploadChannelService, UserNodeService userNodeService) {
         this.uploadChannelService = uploadChannelService;
         this.userNodeService = userNodeService;
     }
@@ -86,8 +86,9 @@ public class StoreChannelPageController {
 
         List<KeyValue> storeNodes = userNodeService.getUserStoreNodes().stream()
                 .map(storeNode -> {
+                    int nodeId = storeNode.getId();
                     String domain = storeNode.getDomain();
-                    return new KeyValue(domain, domain);
+                    return new KeyValue(nodeId+"", domain);
                 }).collect(Collectors.toList());
 
         model.addAttribute("objectTypes", objectTypes);

+ 3 - 4
oss-console/src/main/java/cn/reghao/oss/console/app/db/repository/UploadChannelRepository.java

@@ -14,9 +14,8 @@ import java.util.List;
 public interface UploadChannelRepository extends JpaRepository<UploadChannel, Integer> {
     int countByCreateBy(int createBy);
     UploadChannel findByChannelIdAndCreateBy(int channelId, int createBy);
-    UploadChannel findByPrefixAndCreateByAndBindDomain(String prefix, int createBy, String bindDomain);
-    UploadChannel findByPrefixAndBindDomain(String prefix, String bindDomain);
-    List<UploadChannel> findByBindDomain(String bindDomain);
-    UploadChannel findByCreateByAndBindDomainAndChannelId(int createBy, String bindDomain, int channelId);
+    UploadChannel findByPrefixAndCreateByAndNodeId(String prefix, int createBy, int nodeId);
+    UploadChannel findByPrefixAndNodeId(String prefix, int nodeId);
+    List<UploadChannel> findByNodeId(int nodeId);
     Page<UploadChannel> findByCreateBy(int createBy, Pageable pageable);
 }

+ 3 - 3
oss-console/src/main/java/cn/reghao/oss/console/app/model/dto/UploadChannelDto.java

@@ -12,15 +12,15 @@ import javax.validation.constraints.NotNull;
  */
 @Data
 public class UploadChannelDto {
-    @NotBlank
-    private String domain;
+    @NotNull
+    private Integer nodeId;
     @NotBlank
     private String channelPrefix;
     @NotNull
     private Long maxSize;
     @NotNull
     private Integer fileType;
-    @Length(min = 6, max = 20, message = "名字长度在 6~20 个字符之间")
+    @Length(min = 2, max = 20, message = "名字长度在 2~20 个字符之间")
     private String name;
     @NotNull
     private Integer scope;

+ 2 - 2
oss-console/src/main/java/cn/reghao/oss/console/app/model/po/UploadChannel.java

@@ -26,7 +26,7 @@ public class UploadChannel extends BaseEntity {
     private Integer fileType;
     private Boolean processFile;
     private Integer scope;
-    private String bindDomain;
+    private Integer nodeId;
     private Integer createBy;
 
     public UploadChannel(int channelId, UploadChannelDto uploadChannelDto, int createBy) {
@@ -37,7 +37,7 @@ public class UploadChannel extends BaseEntity {
         this.fileType = uploadChannelDto.getFileType();
         this.processFile = false;
         this.scope = uploadChannelDto.getScope();
-        this.bindDomain = uploadChannelDto.getDomain();
+        this.nodeId = uploadChannelDto.getNodeId();
         this.createBy = createBy;
     }
 }

+ 2 - 2
oss-console/src/main/java/cn/reghao/oss/console/app/model/vo/UploadChannelVo.java

@@ -21,7 +21,7 @@ public class UploadChannelVo {
     private String scope;
     private String bindDomain;
 
-    public UploadChannelVo(UploadChannel uploadChannel, String maxSize) {
+    public UploadChannelVo(UploadChannel uploadChannel, String maxSize, String bindDomain) {
         this.id = uploadChannel.getId();
         this.channelId = uploadChannel.getChannelId();
         this.name = uploadChannel.getName();
@@ -30,6 +30,6 @@ public class UploadChannelVo {
         this.fileType = ObjectType.getDescByCode(uploadChannel.getFileType());
         this.processFile = uploadChannel.getProcessFile();
         this.scope = ObjectScope.getByCode(uploadChannel.getScope()).name();
-        this.bindDomain = uploadChannel.getBindDomain();
+        this.bindDomain = bindDomain;
     }
 }

+ 5 - 2
oss-console/src/main/java/cn/reghao/oss/console/app/rpc/OssService.java

@@ -1,5 +1,6 @@
 package cn.reghao.oss.console.app.rpc;
 
+import cn.reghao.oss.console.app.service.StoreNodeService;
 import cn.reghao.oss.console.app.service.UploadChannelService;
 import cn.reghao.oss.store.api.dto.*;
 import cn.reghao.oss.store.api.iface.StoreService;
@@ -15,9 +16,11 @@ import org.springframework.stereotype.Service;
 @Service
 public class OssService {
     private final UploadChannelService uploadChannelService;
+    private final StoreNodeService storeNodeService;
 
-    public OssService(UploadChannelService uploadChannelService) {
+    public OssService(UploadChannelService uploadChannelService, StoreNodeService storeNodeService) {
         this.uploadChannelService = uploadChannelService;
+        this.storeNodeService = storeNodeService;
     }
 
     public ServerInfo getServerInfo(int channelId) throws Exception {
@@ -28,7 +31,7 @@ public class OssService {
             throw new Exception(errMsg);
         }
 
-        String domain = uploadChannel.getBindDomain();
+        String domain = storeNodeService.getById(uploadChannel.getNodeId()).getDomain();
         String ossUrl = String.format("https://%s", domain);
         long maxSize = uploadChannel.getMaxSize();
 

+ 15 - 9
oss-console/src/main/java/cn/reghao/oss/console/app/service/UploadChannelService.java

@@ -18,6 +18,7 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.List;
 import java.util.stream.Collectors;
@@ -37,21 +38,22 @@ public class UploadChannelService {
         this.storeNodeService = storeNodeService;
     }
 
+    @Transactional(rollbackFor = Exception.class)
     public synchronized Result add(UploadChannelDto uploadChannelDto) {
-        String domain = uploadChannelDto.getDomain();
-        StoreNode storeNode = storeNodeService.getByDomain(domain);
+        int nodeId = uploadChannelDto.getNodeId();
+        StoreNode storeNode = storeNodeService.getById(nodeId);
         if (storeNode == null) {
-            return Result.fail(String.format("store_node with domain %s not exist", domain));
+            return Result.fail(String.format("store_node with id %s not exist", nodeId));
         }
 
         int createBy = UserContext.getUser().getId();
         String channelPrefix = uploadChannelDto.getChannelPrefix();
-        UploadChannel uploadChannel = uploadChannelRepository.findByPrefixAndCreateByAndBindDomain(channelPrefix, createBy, domain);
+        UploadChannel uploadChannel = uploadChannelRepository.findByPrefixAndCreateByAndNodeId(channelPrefix, createBy, nodeId);
         if (uploadChannel != null) {
             return Result.fail(String.format("channel_prefix %s exist", channelPrefix));
         }
 
-        uploadChannel = uploadChannelRepository.findByPrefixAndBindDomain(channelPrefix, domain);
+        uploadChannel = uploadChannelRepository.findByPrefixAndNodeId(channelPrefix, nodeId);
         if (uploadChannel != null) {
             return Result.fail(String.format("someone has already created channel_prefix %s", channelPrefix));
         }
@@ -60,6 +62,7 @@ public class UploadChannelService {
         uploadChannel = new UploadChannel(channelId, uploadChannelDto, createBy);
         uploadChannelRepository.save(uploadChannel);
 
+        String domain = storeNode.getDomain();
         String name = uploadChannel.getName();
         long maxSize = uploadChannel.getMaxSize();
         int fileType = uploadChannel.getFileType();
@@ -104,14 +107,17 @@ public class UploadChannelService {
         List<UploadChannelVo> list = page.stream().map(uploadChannel -> {
             long maxSize = uploadChannel.getMaxSize();
             String maxSizeStr = byteConverter.convert(ByteType.Bytes, maxSize);
-            return new UploadChannelVo(uploadChannel, maxSizeStr);
+            String bindDomain = storeNodeService.getById(uploadChannel.getNodeId()).getDomain();
+            return new UploadChannelVo(uploadChannel, maxSizeStr, bindDomain);
         }).collect(Collectors.toList());
 
         return new PageImpl<>(list, pageRequest, page.getTotalElements());
     }
 
     public List<UploadChannel> getByDomain(String domain) {
-        return uploadChannelRepository.findByBindDomain(domain);
+        StoreNode storeNode = storeNodeService.getByDomain(domain);
+        int nodeId = storeNode.getId();
+        return uploadChannelRepository.findByNodeId(nodeId);
     }
 
     public UploadChannel getUploadChannel(int id) {
@@ -135,7 +141,7 @@ public class UploadChannelService {
         int fileType = uploadChannel.getFileType();
         boolean processFile = uploadChannel.getProcessFile();
         int scope = uploadChannel.getScope();
-        String domain = uploadChannel.getBindDomain();
+        String domain = storeNodeService.getById(uploadChannel.getNodeId()).getDomain();
         return new ObjectChannel(channelId, name, channelPrefix, maxSize, fileType, processFile, scope, domain);
     }
 
@@ -147,7 +153,7 @@ public class UploadChannelService {
             throw new Exception(errMsg);
         }
 
-        String domain = uploadChannel.getBindDomain();
+        String domain = storeNodeService.getById(uploadChannel.getNodeId()).getDomain();
         String ossUrl = String.format("https://%s", domain);
         long maxSize = uploadChannel.getMaxSize();
 

+ 1 - 1
oss-console/src/main/resources/templates/channel/add.html

@@ -14,7 +14,7 @@
                 <td>
                     <div class="layui-form-item">
                         <div class="layui-input-inline">
-                            <select name="domain">
+                            <select name="nodeId">
                                 <option th:each="item : ${storeNodes}" th:value="${item.key}">[[${item.value}]]</option>
                             </select>
                         </div>

+ 7 - 7
oss-sdk/src/test/java/ObjectTest.java → oss-sdk/src/test/java/OssConsoleClientTest.java

@@ -12,7 +12,7 @@ import java.io.*;
  * @date 2023-06-02 16:19:04
  */
 @Slf4j
-public class ObjectTest {
+public class OssConsoleClientTest {
     static void multipartUpload() throws Exception {
         String storeEndpoint = "";
         ObjectMultipartUploadService multipartUploadService = new ObjectMultipartUploadService(storeEndpoint);
@@ -29,7 +29,7 @@ public class ObjectTest {
         String accessKeySecret = "mX7dO8zC8dokThJOC2";
         OssConsoleClient ossConsoleClient = new OssConsoleClient(consoleUrl, accessKeyId, accessKeySecret);
 
-        int channelId = 103;
+        int channelId = 108;
         ServerInfo serverInfo = ossConsoleClient.getServerInfo(channelId);
         if (serverInfo == null) {
             log.info("获取 server_info 失败");
@@ -38,20 +38,20 @@ public class ObjectTest {
         String ossUrl = serverInfo.getOssUrl();
         String token = serverInfo.getToken();
 
-        int scope = 1;
+        /*int scope = 1;
         String objectId = "dafafafa";
         int contentType = 1001;
-        ossConsoleClient.setObjectScope(scope, objectId, contentType);
+        ossConsoleClient.setObjectScope(scope, objectId, contentType);*/
 
-        /*OssStoreClient ossStoreClient = new OssStoreClient(ossUrl);
-        String filePath = "/home/reghao/data/image/12.jpg";
+        OssStoreClient ossStoreClient = new OssStoreClient(ossUrl);
+        String filePath = "/home/reghao/data/video/output.mp4";
         File file = new File(filePath);
         UploadFileRet uploadFileRet = ossStoreClient.postObjectWithJdkHttp(file, channelId, token);
         if (uploadFileRet == null) {
             log.info("文件上传失败");
         } else {
             log.info("{} -> {}", uploadFileRet.getUploadId(), uploadFileRet.getUrl());
-        }*/
+        }
 
         /*String objectName = "video/playback/28d0fd95e224499c9f2cf1d98b4551a5.flv";
         ossStoreClient.getObject(objectName);*/