Przeglądaj źródła

将 ObjectChannel 相关的操作全部放到 StoreChannelService 中

reghao 2 lat temu
rodzic
commit
655246d8d0

+ 7 - 2
oss-store/src/main/java/cn/reghao/oss/store/config/spring/AppLifecycle.java

@@ -11,6 +11,7 @@ import cn.reghao.oss.store.api.dto.StoreNodeDto;
 import cn.reghao.oss.store.api.dto.StoreProperties;
 import cn.reghao.oss.store.config.SpringProperties;
 import cn.reghao.oss.store.db.mapper.DataBlockMapper;
+import cn.reghao.oss.store.service.StoreChannelService;
 import cn.reghao.oss.store.service.StoreLocalCache;
 import cn.reghao.oss.store.task.FileTask;
 import lombok.extern.slf4j.Slf4j;
@@ -38,15 +39,18 @@ public class AppLifecycle implements ApplicationRunner, DisposableBean {
     private final MachineId machineId;
     private final OssConsoleClient ossConsoleClient;
     private final StoreLocalCache storeLocalCache;
+    private final StoreChannelService storeChannelService;
 
     public AppLifecycle(DataBlockMapper dataBlockMapper, FileTask fileTask, SpringProperties springProperties,
-                        MachineId machineId, OssConsoleClient ossConsoleClient, StoreLocalCache storeLocalCache) {
+                        MachineId machineId, OssConsoleClient ossConsoleClient, StoreLocalCache storeLocalCache,
+                        StoreChannelService storeChannelService) {
         this.dataBlockMapper = dataBlockMapper;
         this.fileTask = fileTask;
         this.springProperties = springProperties;
         this.machineId = machineId;
         this.ossConsoleClient = ossConsoleClient;
         this.storeLocalCache = storeLocalCache;
+        this.storeChannelService = storeChannelService;
     }
 
     @Override
@@ -98,7 +102,8 @@ public class AppLifecycle implements ApplicationRunner, DisposableBean {
 
         StoreProperties storeProperties = ossConsoleClient.getStoreProperties(nodeAddr);
         List<ObjectChannel> objectChannels = ossConsoleClient.getObjectChannels(nodeAddr);
-        storeLocalCache.initOssStore(storeProperties, objectChannels);
+        storeLocalCache.initOssStore(storeProperties);
+        storeChannelService.initChannels(objectChannels);
         log.info("StoreNode 初始化完成...");
     }
 

+ 6 - 2
oss-store/src/main/java/cn/reghao/oss/store/controller/ObjectGetController.java

@@ -6,6 +6,7 @@ import cn.reghao.oss.store.api.dto.ObjectChannel;
 import cn.reghao.oss.store.api.dto.ObjectMeta;
 import cn.reghao.oss.store.db.repository.ObjectRepository;
 import cn.reghao.oss.store.service.GetObjectService;
+import cn.reghao.oss.store.service.StoreChannelService;
 import cn.reghao.oss.store.service.StoreLocalCache;
 import cn.reghao.oss.store.util.JwtUtil;
 import cn.reghao.oss.store.util.ObjectUtil;
@@ -27,13 +28,16 @@ public class ObjectGetController {
     private final Cache<String, Object> cache;
     private final StoreLocalCache storeLocalCache;
     private final ObjectRepository objectRepository;
+    private StoreChannelService storeChannelService;
 
     public ObjectGetController(GetObjectService getObjectService, StoreLocalCache storeLocalCache,
-                               Cache<String, Object> cache, ObjectRepository objectRepository) {
+                               Cache<String, Object> cache, ObjectRepository objectRepository,
+                               StoreChannelService storeChannelService) {
         this.getObjectService = getObjectService;
         this.cache = cache;
         this.storeLocalCache = storeLocalCache;
         this.objectRepository = objectRepository;
+        this.storeChannelService = storeChannelService;
     }
 
     @RequestMapping(value = "/**", method = RequestMethod.HEAD)
@@ -93,7 +97,7 @@ public class ObjectGetController {
         OssPayload ossPayload = JwtUtil.getOssPayload(token, storeLocalCache.getSecretKey());
         int loginUser = ossPayload.getUserId();
         int channelId = ossPayload.getChannelId();
-        ObjectChannel objectChannel = storeLocalCache.getObjectChannel(loginUser, channelId);
+        ObjectChannel objectChannel = storeChannelService.getChannelByChannelId(loginUser, channelId);
         if (objectChannel == null) {
             String payload = String.format("channel_id %s not exist", channelId);
             getObjectService.writeResponse(HttpServletResponse.SC_FORBIDDEN, payload);

+ 5 - 2
oss-store/src/main/java/cn/reghao/oss/store/controller/ObjectUploadController.java

@@ -35,16 +35,19 @@ public class ObjectUploadController {
     private final PutObjectService putObjectService;
     private final FileProcessor fileProcessor;
     private final StoreLocalCache storeLocalCache;
+    private StoreChannelService storeChannelService;
 
     public ObjectUploadController(ChannelValidateService channelValidateService, FileStoreService fileStoreService,
                                   ObjectNameService objectNameService, PutObjectService putObjectService,
-                                  FileProcessor fileProcessor, StoreLocalCache storeLocalCache) {
+                                  FileProcessor fileProcessor, StoreLocalCache storeLocalCache,
+                                  StoreChannelService storeChannelService) {
         this.channelValidateService = channelValidateService;
         this.fileStoreService = fileStoreService;
         this.objectNameService = objectNameService;
         this.putObjectService = putObjectService;
         this.fileProcessor = fileProcessor;
         this.storeLocalCache = storeLocalCache;
+        this.storeChannelService = storeChannelService;
     }
 
     @PutMapping(value = "/**")
@@ -77,7 +80,7 @@ public class ObjectUploadController {
         }
 
         int loginUser = ossPayload.getUserId();
-        ObjectChannel objectChannel = storeLocalCache.getObjectChannel(loginUser, channelId);
+        ObjectChannel objectChannel = storeChannelService.getChannelByChannelId(loginUser, channelId);
         if (objectChannel == null) {
             String errMsg = String.format("channel validate failed, channel %s not exist", channelId);
             return ResponseEntity.status(HttpStatus.FORBIDDEN)

+ 5 - 2
oss-store/src/main/java/cn/reghao/oss/store/db/repository/ImageRepository.java

@@ -4,6 +4,7 @@ import cn.reghao.oss.store.api.dto.ObjectChannel;
 import cn.reghao.oss.store.api.dto.media.ImageUrlDto;
 import cn.reghao.oss.store.db.mapper.ImageFileMapper;
 import cn.reghao.oss.store.model.po.ImageFile;
+import cn.reghao.oss.store.service.StoreChannelService;
 import cn.reghao.oss.store.service.StoreLocalCache;
 import org.springframework.stereotype.Repository;
 import org.springframework.transaction.annotation.Transactional;
@@ -23,12 +24,14 @@ public class ImageRepository {
     private final ImageFileMapper imageFileMapper;
     private final ObjectRepository objectRepository;
     private final StoreLocalCache storeLocalCache;
+    private StoreChannelService storeChannelService;
 
     public ImageRepository(ImageFileMapper imageFileMapper, ObjectRepository objectRepository,
-                           StoreLocalCache storeLocalCache) {
+                           StoreLocalCache storeLocalCache, StoreChannelService storeChannelService) {
         this.imageFileMapper = imageFileMapper;
         this.objectRepository = objectRepository;
         this.storeLocalCache = storeLocalCache;
+        this.storeChannelService = storeChannelService;
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -83,7 +86,7 @@ public class ImageRepository {
         ImageFile original = list.get(0);
         imageUrlDto.setOriginalUrl(original.getUrl());
 
-        ObjectChannel objectChannel = storeLocalCache.getObjectChannel(original.getUrl());
+        ObjectChannel objectChannel = storeChannelService.getChannelByUrl(1, original.getUrl());
         if (objectChannel != null && objectChannel.getProcessFile()) {
             imageUrlDto.setThumbnailUrl(list.get(1).getUrl());
         } else {

+ 7 - 3
oss-store/src/main/java/cn/reghao/oss/store/rpc/StoreServiceImpl.java

@@ -3,6 +3,7 @@ package cn.reghao.oss.store.rpc;
 import cn.reghao.oss.store.api.constant.ObjectScope;
 import cn.reghao.oss.store.api.dto.*;
 import cn.reghao.oss.store.service.ObjectNameService;
+import cn.reghao.oss.store.service.StoreChannelService;
 import cn.reghao.oss.store.service.StoreLocalCache;
 import cn.reghao.oss.store.util.JwtUtil;
 import cn.reghao.oss.store.api.constant.ChannelAction;
@@ -21,10 +22,13 @@ import java.util.List;
 @Service
 public class StoreServiceImpl implements StoreService {
     private final StoreLocalCache storeLocalCache;
+    private StoreChannelService storeChannelService;
     private final ObjectNameService objectNameService;
 
-    public StoreServiceImpl(StoreLocalCache storeLocalCache, ObjectNameService objectNameService) {
+    public StoreServiceImpl(StoreLocalCache storeLocalCache, StoreChannelService storeChannelService,
+                            ObjectNameService objectNameService) {
         this.storeLocalCache = storeLocalCache;
+        this.storeChannelService = storeChannelService;
         this.objectNameService = objectNameService;
     }
 
@@ -42,7 +46,7 @@ public class StoreServiceImpl implements StoreService {
         UserContext userContext = new UserContext(userId);
         String objectName = channel.getPrefix();
         objectNameService.createParentDirs(objectName, ObjectScope.PUBLIC.getCode());
-        storeLocalCache.addObjectChannel(channel);
+        storeChannelService.addObjectChannel(channel);
     }
 
     @Override
@@ -53,7 +57,7 @@ public class StoreServiceImpl implements StoreService {
     @Override
     public StoreInfo getStoreInfo() {
         StoreProperties storeProperties = storeLocalCache.getStoreProperties();
-        List<ObjectChannel> objectChannels = storeLocalCache.getObjectChannels();
+        List<ObjectChannel> objectChannels = storeChannelService.getObjectChannels();
         List<StoreDiskDto> list = storeLocalCache.getStoreDisks();
         return new StoreInfo(storeProperties, objectChannels, list);
     }

+ 4 - 2
oss-store/src/main/java/cn/reghao/oss/store/service/SignService.java

@@ -17,14 +17,16 @@ import java.util.UUID;
 @Service
 public class SignService {
     private final StoreLocalCache storeLocalCache;
+    private StoreChannelService storeChannelService;
 
-    public SignService(StoreLocalCache storeLocalCache) {
+    public SignService(StoreLocalCache storeLocalCache, StoreChannelService storeChannelService) {
         this.storeLocalCache = storeLocalCache;
+        this.storeChannelService = storeChannelService;
     }
 
     public String getSignedUrl(int loginUser, String url, int expire) {
         long timestamp = System.currentTimeMillis() + expire*1000L;
-        int channelId = storeLocalCache.getChannelId(url);
+        int channelId = storeChannelService.getChannelIdByUrl(loginUser, url);
 
         String secretKey = storeLocalCache.getSecretKey();
         String action1 = ChannelAction.download.getName();

+ 72 - 0
oss-store/src/main/java/cn/reghao/oss/store/service/StoreChannelService.java

@@ -0,0 +1,72 @@
+package cn.reghao.oss.store.service;
+
+import cn.reghao.oss.store.api.dto.ObjectChannel;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author reghao
+ * @date 2024-03-08 09:20:27
+ */
+@Service
+public class StoreChannelService {
+    private final Map<String, ObjectChannel> prefixMap = new HashMap<>();
+    private final Map<String, ObjectChannel> channelIdMap = new HashMap<>();
+    private final StoreLocalCache storeLocalCache;
+
+    public StoreChannelService(StoreLocalCache storeLocalCache) {
+        this.storeLocalCache = storeLocalCache;
+    }
+
+    public void initChannels(List<ObjectChannel> list) {
+        for (ObjectChannel channel : list) {
+            int createBy = channel.getCreateBy();
+
+            String prefix = channel.getPrefix();
+            Integer channelId = channel.getChannelId();
+            prefixMap.put(createBy + "-" + prefix, channel);
+            channelIdMap.put(createBy + "-" + channelId, channel);
+        }
+    }
+
+    public void addObjectChannel(ObjectChannel channel) {
+        int createBy = channel.getCreateBy();
+        String prefix = channel.getPrefix();
+        Integer channelId = channel.getChannelId();
+        prefixMap.put(createBy + "-" + prefix, channel);
+        channelIdMap.put(createBy + "-" + channelId, channel);
+    }
+
+    public ObjectChannel getChannelByChannelId(int loginUser, int channelId) {
+        return channelIdMap.get(loginUser + "-" + channelId);
+    }
+
+    public ObjectChannel getChannelByUrl(int loginUser, String url) {
+        String domain = storeLocalCache.getDomain();
+        String objectName = url.replace("//" + domain + "/", "");
+        for (String prefix : prefixMap.keySet()) {
+            if (objectName.startsWith(prefix)) {
+                return prefixMap.get(loginUser + "-" + prefix);
+            }
+        }
+
+        return null;
+    }
+
+    public int getChannelIdByUrl(int loginUser, String url) {
+        ObjectChannel objectChannel = getChannelByUrl(loginUser, url);
+        if (objectChannel == null) {
+            return -1;
+        }
+
+        return objectChannel.getChannelId();
+    }
+
+    public List<ObjectChannel> getObjectChannels() {
+        return new ArrayList<>(prefixMap.values());
+    }
+}

+ 1 - 45
oss-store/src/main/java/cn/reghao/oss/store/service/StoreLocalCache.java

@@ -20,35 +20,20 @@ import java.util.Map;
 @Service
 public class StoreLocalCache {
     private StoreProperties storeProperties;
-    private final Map<String, ObjectChannel> prefixMap = new HashMap<>();
-    private final Map<Integer, ObjectChannel> channelIdMap = new HashMap<>();
     private final SpringProperties springProperties;
 
     public StoreLocalCache(SpringProperties springProperties) {
         this.springProperties = springProperties;
     }
 
-    public void initOssStore(StoreProperties storeProperties, List<ObjectChannel> list) {
+    public void initOssStore(StoreProperties storeProperties) {
         this.storeProperties = storeProperties;
-        for (ObjectChannel channel : list) {
-            String prefix = channel.getPrefix();
-            Integer channelId = channel.getChannelId();
-            prefixMap.put(prefix, channel);
-            channelIdMap.put(channelId, channel);
-        }
     }
 
     public void updateStoreProperties(StoreProperties storeProperties) {
         this.storeProperties = storeProperties;
     }
 
-    public void addObjectChannel(ObjectChannel channel) {
-        String prefix = channel.getPrefix();
-        Integer channelId = channel.getChannelId();
-        prefixMap.put(prefix, channel);
-        channelIdMap.put(channelId, channel);
-    }
-
     public String getHost() {
         return springProperties.getHost();
     }
@@ -65,35 +50,6 @@ public class StoreLocalCache {
         return storeProperties != null ? storeProperties.getSecretKey() : null;
     }
 
-    public ObjectChannel getObjectChannel(int loginUser, int channelId) {
-        return channelIdMap.get(channelId);
-    }
-
-    public int getChannelId(String url) {
-        ObjectChannel objectChannel = getObjectChannel(url);
-        if (objectChannel == null) {
-            return -1;
-        }
-
-        return objectChannel.getChannelId();
-    }
-
-    public ObjectChannel getObjectChannel(String url) {
-        String domain = getDomain();
-        String objectName = url.replace("//" + domain + "/", "");
-        for (String prefix : prefixMap.keySet()) {
-            if (objectName.startsWith(prefix)) {
-                return prefixMap.get(prefix);
-            }
-        }
-
-        return null;
-    }
-
-    public List<ObjectChannel> getObjectChannels() {
-        return new ArrayList<>(prefixMap.values());
-    }
-
     public StoreProperties getStoreProperties() {
         return storeProperties;
     }

+ 1 - 0
store-api/src/main/java/cn/reghao/oss/store/api/dto/ObjectChannel.java

@@ -22,4 +22,5 @@ public class ObjectChannel implements Serializable {
     private Boolean processFile;
     private Integer scope;
     private String domain;
+    private Integer createBy;
 }