From ce0a7bd6efb6b4ec95a6e264b11e532d3289bb16 Mon Sep 17 00:00:00 2001 From: zhaojun <873019219@qq.com> Date: Sat, 27 May 2023 16:26:22 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=8A=BD=E5=8F=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=88=B0=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../module/config/utils/SpringMvcUtils.java | 22 +++++++++++++++++++ .../proxy/ProxyDownloadController.java | 10 +++------ .../proxy/ProxyUploadController.java | 16 ++++---------- 3 files changed, 29 insertions(+), 19 deletions(-) create mode 100644 src/main/java/im/zhaojun/zfile/module/config/utils/SpringMvcUtils.java diff --git a/src/main/java/im/zhaojun/zfile/module/config/utils/SpringMvcUtils.java b/src/main/java/im/zhaojun/zfile/module/config/utils/SpringMvcUtils.java new file mode 100644 index 0000000..00934b2 --- /dev/null +++ b/src/main/java/im/zhaojun/zfile/module/config/utils/SpringMvcUtils.java @@ -0,0 +1,22 @@ +package im.zhaojun.zfile.module.config.utils; + +import im.zhaojun.zfile.core.util.RequestHolder; +import org.springframework.util.AntPathMatcher; +import org.springframework.web.servlet.HandlerMapping; + +import javax.servlet.http.HttpServletRequest; + +/** + * @author zhaojun + */ +public class SpringMvcUtils { + + public static String getExtractPathWithinPattern() { + HttpServletRequest httpServletRequest = RequestHolder.getRequest(); + String path = (String) httpServletRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); + String bestMatchPattern = (String) httpServletRequest.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); + AntPathMatcher apm = new AntPathMatcher(); + return apm.extractPathWithinPattern(bestMatchPattern, path); + } + +} diff --git a/src/main/java/im/zhaojun/zfile/module/storage/controller/proxy/ProxyDownloadController.java b/src/main/java/im/zhaojun/zfile/module/storage/controller/proxy/ProxyDownloadController.java index 92ce858..ab59b79 100644 --- a/src/main/java/im/zhaojun/zfile/module/storage/controller/proxy/ProxyDownloadController.java +++ b/src/main/java/im/zhaojun/zfile/module/storage/controller/proxy/ProxyDownloadController.java @@ -2,9 +2,10 @@ package im.zhaojun.zfile.module.storage.controller.proxy; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import com.github.xiaoymin.knife4j.annotations.ApiSort; -import im.zhaojun.zfile.module.storage.context.StorageSourceContext; import im.zhaojun.zfile.core.exception.StorageSourceNotSupportProxyUploadException; import im.zhaojun.zfile.core.util.ProxyDownloadUrlUtils; +import im.zhaojun.zfile.module.config.utils.SpringMvcUtils; +import im.zhaojun.zfile.module.storage.context.StorageSourceContext; import im.zhaojun.zfile.module.storage.service.base.AbstractBaseFileService; import im.zhaojun.zfile.module.storage.service.base.AbstractProxyTransferService; import io.swagger.annotations.Api; @@ -13,11 +14,9 @@ import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.util.AntPathMatcher; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.servlet.HandlerMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @@ -54,10 +53,7 @@ public class ProxyDownloadController { @ResponseBody public ResponseEntity downAttachment(@PathVariable("storageKey") String storageKey, String signature) throws IOException { // 获取下载文件路径 - String path = (String) httpServletRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); - String bestMatchPattern = (String) httpServletRequest.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); - AntPathMatcher apm = new AntPathMatcher(); - String filePath = apm.extractPathWithinPattern(bestMatchPattern, path); + String filePath = SpringMvcUtils.getExtractPathWithinPattern(); AbstractBaseFileService storageServiceByKey = storageSourceContext.getByStorageKey(storageKey); diff --git a/src/main/java/im/zhaojun/zfile/module/storage/controller/proxy/ProxyUploadController.java b/src/main/java/im/zhaojun/zfile/module/storage/controller/proxy/ProxyUploadController.java index de876a5..f516317 100644 --- a/src/main/java/im/zhaojun/zfile/module/storage/controller/proxy/ProxyUploadController.java +++ b/src/main/java/im/zhaojun/zfile/module/storage/controller/proxy/ProxyUploadController.java @@ -1,18 +1,13 @@ package im.zhaojun.zfile.module.storage.controller.proxy; -import im.zhaojun.zfile.module.storage.context.StorageSourceContext; import im.zhaojun.zfile.core.util.AjaxJson; +import im.zhaojun.zfile.module.config.utils.SpringMvcUtils; +import im.zhaojun.zfile.module.storage.context.StorageSourceContext; import im.zhaojun.zfile.module.storage.service.base.AbstractBaseFileService; import im.zhaojun.zfile.module.storage.service.base.AbstractProxyTransferService; import io.swagger.annotations.Api; -import org.springframework.util.AntPathMatcher; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.servlet.HandlerMapping; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; @@ -43,10 +38,7 @@ public class ProxyUploadController { } // 获取上传路径 - String path = (String) httpServletRequest.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); - String bestMatchPattern = (String) httpServletRequest.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); - AntPathMatcher apm = new AntPathMatcher(); - String filePath = apm.extractPathWithinPattern(bestMatchPattern, path); + String filePath = SpringMvcUtils.getExtractPathWithinPattern(); AbstractBaseFileService storageServiceByKey = storageSourceContext.getByStorageKey(storageKey);