Jelajahi Sumber

修改core-fss和starter-api,修复FileService初始化配置问题,以及fss访问端点的路径解析问题

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 11 bulan lalu
induk
melakukan
2e6e5843a0

+ 8 - 9
core-fss/src/main/java/com/qmth/boot/core/fss/config/FssAutoConfiguration.java

@@ -12,6 +12,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.lang.Nullable;
 
 import javax.validation.constraints.NotNull;
 
@@ -31,12 +32,6 @@ public class FssAutoConfiguration {
         return new FileStorePropertyMap();
     }
 
-    @Bean(destroyMethod = "close")
-    @FssCondition(single = false)
-    public FileService fileService(FileStorePropertyMap fileStorePropertyMap, FssSecretProvider fssSecretProvider) {
-        return new DefaultFileService(fileStorePropertyMap.getFss(), fssSecretProvider);
-    }
-
     /**
      * 只配置了单个FileStore的情况
      *
@@ -57,8 +52,12 @@ public class FssAutoConfiguration {
     }
 
     @Bean(destroyMethod = "close")
-    @FssCondition(single = true)
-    public FileService fileService(@NotNull FileStore fileStore) {
-        return new SingletonFileService(fileStore);
+    public FileService fileService(@Nullable FileStore fileStore, @Nullable FileStorePropertyMap fileStorePropertyMap,
+            @NotNull FssSecretProvider fssSecretProvider) {
+        if (fileStore != null) {
+            return new SingletonFileService(fileStore);
+        } else {
+            return new DefaultFileService(fileStorePropertyMap.getFss(), fssSecretProvider);
+        }
     }
 }

+ 18 - 30
starter-api/src/main/java/com/qmth/boot/api/controller/FssController.java

@@ -1,27 +1,24 @@
 package com.qmth.boot.api.controller;
 
 import com.qmth.boot.api.annotation.Aac;
-import com.qmth.boot.core.exception.ForbiddenException;
-import com.qmth.boot.core.exception.NotFoundException;
 import com.qmth.boot.core.exception.ParameterException;
-import com.qmth.boot.core.exception.UnauthorizedException;
 import com.qmth.boot.core.fss.service.FileService;
 import com.qmth.boot.core.fss.store.FileStore;
 import com.qmth.boot.core.fss.utils.FssUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.http.HttpStatus;
+import org.springframework.core.io.InputStreamResource;
 import org.springframework.http.MediaType;
+import org.springframework.http.MediaTypeFactory;
 import org.springframework.http.ResponseEntity;
-import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import javax.servlet.http.HttpServletRequest;
 import javax.validation.constraints.Min;
 import javax.validation.constraints.NotBlank;
-import java.io.InputStreamReader;
 
 @CrossOrigin
-@Controller
+@RestController
 @RequestMapping(FssUtils.INNER_ENDPOINT_PREFIX)
 @Aac(auth = false, strict = false)
 public class FssController {
@@ -29,32 +26,23 @@ public class FssController {
     @Resource
     private FileService fileService;
 
-    @GetMapping("/{path}")
-    public ResponseEntity get(@PathVariable String path,
+    @GetMapping("/**")
+    public ResponseEntity<InputStreamResource> get(HttpServletRequest request,
             @RequestParam(name = FssUtils.INNER_ENDPOINT_PARAM_BUCKET) @NotBlank String bucket,
             @RequestParam(name = FssUtils.INNER_ENDPOINT_PARAM_EXPIRE_TIME) @Min(0) Long expireTime,
-            @RequestParam(name = FssUtils.INNER_ENDPOINT_PARAM_SIGNATURE) @NotBlank String signature) {
-        try {
-            FileStore fileStore = fileService.getFileStore(bucket);
-            if (fileStore == null) {
-                return new ResponseEntity("bucket not exists: " + bucket, HttpStatus.BAD_REQUEST);
-            }
-            if (expireTime <= 0) {
-                return new ResponseEntity("expireTime is invalid", HttpStatus.BAD_REQUEST);
-            }
-            return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM).body(new InputStreamReader(
-                    fileStore.readServerUrl(bucket, path, expireTime, StringUtils.trimToEmpty(signature))));
-        } catch (ForbiddenException e) {
-            return new ResponseEntity(e.getMessage(), HttpStatus.FORBIDDEN);
-        } catch (NotFoundException e1) {
-            return new ResponseEntity(e1.getMessage(), HttpStatus.NOT_FOUND);
-        } catch (ParameterException e2) {
-            return new ResponseEntity(e2.getMessage(), HttpStatus.BAD_REQUEST);
-        } catch (UnauthorizedException e3) {
-            return new ResponseEntity(e3.getMessage(), HttpStatus.UNAUTHORIZED);
-        } catch (Exception e4) {
-            return new ResponseEntity(e4.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+            @RequestParam(name = FssUtils.INNER_ENDPOINT_PARAM_SIGNATURE) @NotBlank String signature) throws Exception {
+        String path = request.getServletPath().substring(FssUtils.INNER_ENDPOINT_PREFIX.length() + 1);
+        FileStore fileStore = fileService.getFileStore(bucket);
+        if (fileStore == null) {
+            throw new ParameterException("bucket not exists: " + bucket);
         }
+        if (expireTime <= 0) {
+            throw new ParameterException("expireTime is invalid");
+        }
+        return ResponseEntity.ok()
+                .contentType(MediaTypeFactory.getMediaType(path).orElse(MediaType.APPLICATION_OCTET_STREAM))
+                .body(new InputStreamResource(
+                        fileStore.readServerUrl(bucket, path, expireTime, StringUtils.trimToEmpty(signature))));
     }
 
 }