Преглед изворни кода

fileStore私有域客户端文件访问时长

wangliang пре 2 година
родитељ
комит
763e594855

+ 37 - 3
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/FileStoreUtil.java

@@ -1,6 +1,7 @@
 package com.qmth.teachcloud.common.util;
 
 import com.qmth.boot.core.fss.service.FileService;
+import com.qmth.boot.core.fss.store.FileStore;
 import com.qmth.teachcloud.common.config.DictionaryConfig;
 import com.qmth.teachcloud.common.contant.SystemConstant;
 import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
@@ -15,6 +16,8 @@ import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
 import java.io.*;
+import java.time.Duration;
+import java.util.Objects;
 
 /**
  * @Description: 文件存储工具类
@@ -198,7 +201,6 @@ public class FileStoreUtil {
         return fileService.getFileStore(type).read(objectName);
     }
 
-
     /**
      * 获取文件访问url
      *
@@ -209,9 +211,41 @@ public class FileStoreUtil {
     public String getPrivateUrl(String objectPath, String type) {
         String server = null;
         if ("public".equals(type)) {
-            return dictionaryConfig.fssPublicDomain().getServer() + SystemConstant.ORG_SPLIT + objectPath;
+            server = dictionaryConfig.fssPublicDomain().getServer();
+            return server + "/" + objectPath;
+        } else if ("private".equals(type)) {
+            Boolean oss = dictionaryConfig.sysDomain().isOss();
+            if (Objects.nonNull(oss) && oss) {
+                FileStore fileStore = fileService.getFileStore(type);
+                return fileStore.getPresignedUrl(objectPath, Duration.ofMinutes(5L));
+            } else {
+                return dictionaryConfig.fssPrivateDomain().getServer() + "/" + objectPath;
+            }
+        } else {
+            throw ExceptionResultEnum.ERROR.exception("文件存储store类型不存在");
+        }
+    }
+
+    /**
+     * 获取文件访问url
+     *
+     * @param objectPath 文件路径
+     * @param type       文件上传的类型
+     * @return
+     */
+    public String getPrivateUrlByClient(String objectPath, String type) {
+        String server = null;
+        if ("public".equals(type)) {
+            server = dictionaryConfig.fssPublicDomain().getServer();
+            return server + SystemConstant.ORG_SPLIT + objectPath;
         } else if ("private".equals(type)) {
-            return dictionaryConfig.fssPrivateDomain().getServer() + SystemConstant.ORG_SPLIT + objectPath;
+            Boolean oss = dictionaryConfig.sysDomain().isOss();
+            if (Objects.nonNull(oss) && oss) {
+                FileStore fileStore = fileService.getFileStore(type);
+                return fileStore.getPresignedUrl(objectPath, Duration.ofMinutes(120L));
+            } else {
+                return dictionaryConfig.fssPrivateDomain().getServer() + SystemConstant.ORG_SPLIT + objectPath;
+            }
         } else {
             throw ExceptionResultEnum.ERROR.exception("文件存储store类型不存在");
         }