From 185c84dd79dec1317df0fc0d54d4a10d318f1bf5 Mon Sep 17 00:00:00 2001 From: zhaojun1998 Date: Sun, 9 Feb 2020 18:05:56 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=9B=B4=E9=93=BE=E4=B8=8B=E8=BD=BD=E5=8A=9F=E8=83=BD?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/controller/PageController.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/im/zhaojun/common/controller/PageController.java diff --git a/src/main/java/im/zhaojun/common/controller/PageController.java b/src/main/java/im/zhaojun/common/controller/PageController.java new file mode 100644 index 0000000..c7c8326 --- /dev/null +++ b/src/main/java/im/zhaojun/common/controller/PageController.java @@ -0,0 +1,63 @@ +package im.zhaojun.common.controller; + +import cn.hutool.core.util.URLUtil; +import im.zhaojun.common.model.dto.FileItemDTO; +import im.zhaojun.common.model.enums.FileTypeEnum; +import im.zhaojun.common.service.AbstractFileService; +import im.zhaojun.common.service.SystemConfigService; +import org.springframework.stereotype.Controller; +import org.springframework.util.AntPathMatcher; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.servlet.HandlerMapping; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import java.net.MalformedURLException; +import java.util.Objects; + +/** + * @author Zhao Jun + * 2020/2/9 11:17 + */ +@Controller +public class PageController { + + @Resource + private SystemConfigService systemConfigService; + + @GetMapping("/directlink/**") + public String directlink(final HttpServletRequest request) throws MalformedURLException { + String path = (String) request.getAttribute( + HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); + String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); + AntPathMatcher apm = new AntPathMatcher(); + String filePath = apm.extractPathWithinPattern(bestMatchPattern, path); + + if (filePath.length() > 0 && filePath.charAt(0) != '/') { + filePath = "/" + filePath; + } + + AbstractFileService fileService = systemConfigService.getCurrentFileService(); + FileItemDTO fileItem = fileService.getFileItem(filePath); + + String url = fileItem.getUrl(); + + int queryIndex = url.indexOf('?'); + + if (queryIndex != -1) { + String origin = url.substring(0, queryIndex); + String queryString = url.substring(queryIndex + 1); + + url = URLUtil.encode(origin) + "?" + URLUtil.encode(queryString); + } else { + url = URLUtil.encode(url); + } + + + if (Objects.equals(fileItem.getType(), FileTypeEnum.FOLDER)) { + return "redirect:" + fileItem.getUrl(); + } else { + return "redirect:" + url; + } + } +}