perf:增加版本对比器

This commit is contained in:
曾文豪
2023-03-31 10:35:21 +08:00
parent 06f295bb66
commit c5f8508a9e
6 changed files with 18 additions and 7 deletions

View File

@@ -1,5 +1,7 @@
package com.tiesheng.util.config;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.comparator.VersionComparator;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
@@ -12,6 +14,8 @@ import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @author hao
@@ -62,15 +66,22 @@ public class GlobalConfig {
* @param route
*/
public void redirect(String htmlDir, String route, HttpServletResponse response) {
if (!StrUtil.endWith(htmlDir, "/")) {
htmlDir = htmlDir + "/";
}
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
try {
Resource[] resources = patternResolver.getResources(String.format("classpath*:static/%s/*/index.html", htmlDir));
if (resources.length == 0) {
List<String> versions = new ArrayList<>();
for (Resource resource : resources) {
String path = FileUtil.normalize(resource.getURL().getPath());
versions.add(StrUtil.subBetween(path, htmlDir, "/index.html"));
}
if (CollUtil.isEmpty(versions)) {
throw new ApiException("无法重定向,请检查资源");
}
String filename = resources[resources.length - 1].getURL().getPath();
filename = FileUtil.normalize(filename);
String path = buildPath(String.format("/%s%s#%s", htmlDir, StrUtil.subAfter(filename, htmlDir, true), route));
CollUtil.sort(versions, (o1, o2) -> -VersionComparator.INSTANCE.compare(o1, o2));
String path = buildPath(String.format("/%s%s/index.html#%s", htmlDir, versions.get(0), route));
response.sendRedirect(path);
} catch (IOException e) {
LogFactory.get().info(e);