From 3eaf133718a99b56cf74bfc692b56bf87c09de26 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Fri, 17 Nov 2023 21:16:51 +0800
Subject: [PATCH 001/168] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4?=
=?UTF-8?q?=E6=83=85=E5=86=B5=E4=B8=8B=E6=98=BE=E7=A4=BA=E8=AF=84=E5=88=86?=
=?UTF-8?q?=E4=B8=8D=E8=B6=B3=E6=97=B6=E6=89=80=E9=9C=80=E7=9A=84=E8=AF=84?=
=?UTF-8?q?=E5=88=86=E9=87=8F=EF=BC=8C=E5=B9=B6=E5=85=81=E8=AE=B8=E8=AF=A5?=
=?UTF-8?q?=E9=A1=B9=E9=85=8D=E7=BD=AE=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../plus/maa/backend/config/external/Copilot.java | 12 ++++++++++++
.../config/external/MaaCopilotProperties.java | 2 ++
.../plus/maa/backend/service/CopilotService.java | 7 ++++++-
3 files changed, 20 insertions(+), 1 deletion(-)
create mode 100644 src/main/java/plus/maa/backend/config/external/Copilot.java
diff --git a/src/main/java/plus/maa/backend/config/external/Copilot.java b/src/main/java/plus/maa/backend/config/external/Copilot.java
new file mode 100644
index 00000000..0d674ad0
--- /dev/null
+++ b/src/main/java/plus/maa/backend/config/external/Copilot.java
@@ -0,0 +1,12 @@
+package plus.maa.backend.config.external;
+
+import lombok.Data;
+
+@Data
+public class Copilot {
+
+ /**
+ * 作业评分总数少于指定值时显示评分不足
+ */
+ private int minValueShowNotEnoughRating = 100;
+}
diff --git a/src/main/java/plus/maa/backend/config/external/MaaCopilotProperties.java b/src/main/java/plus/maa/backend/config/external/MaaCopilotProperties.java
index c7975d63..c7cc9615 100644
--- a/src/main/java/plus/maa/backend/config/external/MaaCopilotProperties.java
+++ b/src/main/java/plus/maa/backend/config/external/MaaCopilotProperties.java
@@ -26,4 +26,6 @@ public class MaaCopilotProperties {
private Mail mail;
@NestedConfigurationProperty
private SensitiveWord sensitiveWord;
+ @NestedConfigurationProperty
+ private Copilot copilot;
}
diff --git a/src/main/java/plus/maa/backend/service/CopilotService.java b/src/main/java/plus/maa/backend/service/CopilotService.java
index 12b7f2e0..ee33d8f9 100644
--- a/src/main/java/plus/maa/backend/service/CopilotService.java
+++ b/src/main/java/plus/maa/backend/service/CopilotService.java
@@ -8,6 +8,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
@@ -66,6 +67,10 @@ public class CopilotService {
private final CopilotConverter copilotConverter;
private final AtomicLong copilotIncrementId = new AtomicLong(20000);
+ // 评分总数少于指定值时显示评分不足
+ @Value("${maa-copilot.copilot.min-value-show-not-enough-rating:100}")
+ private int minValueShowNotEnoughRating;
+
/*
首页分页查询缓存配置
格式为:需要缓存的 orderBy 类型(也就是榜单类型) -> 缓存时间
@@ -525,7 +530,7 @@ private CopilotInfo formatCopilot(Copilot copilot, @Nullable RatingType ratingTy
info.setRatingType(ratingType.getDisplay());
}
// 评分数少于一定数量
- info.setNotEnoughRating(copilot.getLikeCount() + copilot.getDislikeCount() <= 5);
+ info.setNotEnoughRating(copilot.getLikeCount() + copilot.getDislikeCount() <= minValueShowNotEnoughRating);
info.setAvailable(true);
From f075b8e85f3db516776eb1eb03ed6dbc50dcce21 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Fri, 17 Nov 2023 21:25:31 +0800
Subject: [PATCH 002/168] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?=
=?UTF-8?q?=E4=B8=AD=E7=9A=84=E9=98=88=E5=80=BC=E4=B8=BA50?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/resources/application-template.yml | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/main/resources/application-template.yml b/src/main/resources/application-template.yml
index f04a8ee3..bea8501b 100644
--- a/src/main/resources/application-template.yml
+++ b/src/main/resources/application-template.yml
@@ -60,6 +60,8 @@ maa-copilot:
ssl: false
#邮件通知
notification: true
+ copilot:
+ min-value-show-not-enough-rating: 50
From 47481a97150dab4654384160039a96e466493c4f Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Fri, 17 Nov 2023 22:33:45 +0800
Subject: [PATCH 003/168] =?UTF-8?q?=E4=BF=AE=E6=94=B9@Value=E6=B3=A8?=
=?UTF-8?q?=E5=85=A5=E7=9A=84=E6=96=B9=E5=BC=8F=E4=BB=A5=E5=8F=8AmaaCopilo?=
=?UTF-8?q?tProperties.copilot=E7=9A=84=E9=BB=98=E8=AE=A4=E5=80=BC?=
=?UTF-8?q?=EF=BC=8C=E5=94=AF=E4=B8=80=E7=9A=84=E5=A5=BD=E5=A4=84=E6=98=AF?=
=?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4=E5=80=BC=E5=8F=AA=E9=9C=80?=
=?UTF-8?q?=E8=A6=81=E6=94=B9=E4=B8=80=E4=B8=AA=E5=9C=B0=E6=96=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/plus/maa/backend/config/external/Copilot.java | 4 +++-
.../maa/backend/config/external/MaaCopilotProperties.java | 7 +++++--
src/main/java/plus/maa/backend/service/CopilotService.java | 2 +-
3 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/src/main/java/plus/maa/backend/config/external/Copilot.java b/src/main/java/plus/maa/backend/config/external/Copilot.java
index 0d674ad0..9d06b5dd 100644
--- a/src/main/java/plus/maa/backend/config/external/Copilot.java
+++ b/src/main/java/plus/maa/backend/config/external/Copilot.java
@@ -7,6 +7,8 @@ public class Copilot {
/**
* 作业评分总数少于指定值时显示评分不足
+ *
+ * 默认值:50
*/
- private int minValueShowNotEnoughRating = 100;
+ private int minValueShowNotEnoughRating = 50;
}
diff --git a/src/main/java/plus/maa/backend/config/external/MaaCopilotProperties.java b/src/main/java/plus/maa/backend/config/external/MaaCopilotProperties.java
index c7cc9615..7ecc8141 100644
--- a/src/main/java/plus/maa/backend/config/external/MaaCopilotProperties.java
+++ b/src/main/java/plus/maa/backend/config/external/MaaCopilotProperties.java
@@ -1,10 +1,13 @@
package plus.maa.backend.config.external;
+import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
+import org.springframework.stereotype.Component;
+@Data
+@Component
@ConfigurationProperties("maa-copilot")
-@lombok.Data
public class MaaCopilotProperties {
@NestedConfigurationProperty
private Jwt jwt;
@@ -27,5 +30,5 @@ public class MaaCopilotProperties {
@NestedConfigurationProperty
private SensitiveWord sensitiveWord;
@NestedConfigurationProperty
- private Copilot copilot;
+ private Copilot copilot = new Copilot();
}
diff --git a/src/main/java/plus/maa/backend/service/CopilotService.java b/src/main/java/plus/maa/backend/service/CopilotService.java
index ee33d8f9..04b8bc95 100644
--- a/src/main/java/plus/maa/backend/service/CopilotService.java
+++ b/src/main/java/plus/maa/backend/service/CopilotService.java
@@ -68,7 +68,7 @@ public class CopilotService {
private final AtomicLong copilotIncrementId = new AtomicLong(20000);
// 评分总数少于指定值时显示评分不足
- @Value("${maa-copilot.copilot.min-value-show-not-enough-rating:100}")
+ @Value("#{maaCopilotProperties.copilot.minValueShowNotEnoughRating}")
private int minValueShowNotEnoughRating;
/*
From 625e2ba45c72709530516be9ef54f0cad5348bca Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Fri, 17 Nov 2023 23:36:55 +0800
Subject: [PATCH 004/168] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=98=88=E5=80=BC?=
=?UTF-8?q?=E7=9A=84=E6=B3=A8=E5=85=A5=E6=96=B9=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/plus/maa/backend/service/CopilotService.java | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/src/main/java/plus/maa/backend/service/CopilotService.java b/src/main/java/plus/maa/backend/service/CopilotService.java
index 04b8bc95..ee088492 100644
--- a/src/main/java/plus/maa/backend/service/CopilotService.java
+++ b/src/main/java/plus/maa/backend/service/CopilotService.java
@@ -8,7 +8,6 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
-import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
@@ -20,6 +19,7 @@
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import plus.maa.backend.common.utils.converter.CopilotConverter;
+import plus.maa.backend.config.external.MaaCopilotProperties;
import plus.maa.backend.controller.request.copilot.CopilotCUDRequest;
import plus.maa.backend.controller.request.copilot.CopilotDTO;
import plus.maa.backend.controller.request.copilot.CopilotQueriesRequest;
@@ -63,14 +63,11 @@ public class CopilotService {
private final RedisCache redisCache;
private final UserRepository userRepository;
private final CommentsAreaRepository commentsAreaRepository;
+ private final MaaCopilotProperties properties;
private final CopilotConverter copilotConverter;
private final AtomicLong copilotIncrementId = new AtomicLong(20000);
- // 评分总数少于指定值时显示评分不足
- @Value("#{maaCopilotProperties.copilot.minValueShowNotEnoughRating}")
- private int minValueShowNotEnoughRating;
-
/*
首页分页查询缓存配置
格式为:需要缓存的 orderBy 类型(也就是榜单类型) -> 缓存时间
@@ -530,7 +527,7 @@ private CopilotInfo formatCopilot(Copilot copilot, @Nullable RatingType ratingTy
info.setRatingType(ratingType.getDisplay());
}
// 评分数少于一定数量
- info.setNotEnoughRating(copilot.getLikeCount() + copilot.getDislikeCount() <= minValueShowNotEnoughRating);
+ info.setNotEnoughRating(copilot.getLikeCount() + copilot.getDislikeCount() <= properties.getCopilot().getMinValueShowNotEnoughRating());
info.setAvailable(true);
From 238f6a9595504cf80d49c151f539545cdd8c1b81 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Sat, 18 Nov 2023 00:35:58 +0800
Subject: [PATCH 005/168] =?UTF-8?q?=E5=90=AF=E7=94=A8=E5=93=8D=E5=BA=94?=
=?UTF-8?q?=E4=BD=93=E8=87=AA=E5=8A=A8=E5=8E=8B=E7=BC=A9=EF=BC=8C=E8=BF=9B?=
=?UTF-8?q?=E4=B8=80=E6=AD=A5=E8=8A=82=E7=BA=A6=E6=9C=8D=E5=8A=A1=E5=99=A8?=
=?UTF-8?q?=E5=B8=A6=E5=AE=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/plus/maa/backend/filter/MaaEtagHeaderFilter.java | 7 +++++++
src/main/resources/application.yml | 2 ++
2 files changed, 9 insertions(+)
diff --git a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
index 731b1173..4676a189 100644
--- a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
+++ b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
@@ -28,6 +28,13 @@
@RequiredArgsConstructor
public class MaaEtagHeaderFilter extends ShallowEtagHeaderFilter {
+ @Override
+ protected void initFilterBean() throws ServletException {
+ super.initFilterBean();
+ // Etag 必须使用弱校验才能与自动压缩兼容
+ setWriteWeakETag(true);
+ }
+
@Override
protected void doFilterInternal(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull FilterChain filterChain) throws ServletException, IOException {
if (!HttpMethod.GET.matches(request.getMethod())) {
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index fd86378f..4ced4110 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -31,6 +31,8 @@ server:
tomcat:
#限制post表单最大为30KB
max-http-form-post-size: 30KB
+ compression:
+ enabled: true
logging:
file:
From 9091b2870ffc0ec0d0bb220128d61c12dbc54a60 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Sat, 18 Nov 2023 13:15:20 +0800
Subject: [PATCH 006/168] =?UTF-8?q?=E8=B0=83=E6=95=B4=20Etag=20=E8=BF=87?=
=?UTF-8?q?=E6=BB=A4=E5=99=A8=E7=9A=84=E5=B7=A5=E4=BD=9C=E9=80=BB=E8=BE=91?=
=?UTF-8?q?=EF=BC=8C=E4=BD=BF=E5=BE=97=E8=87=AA=E5=8A=A8=E5=8E=8B=E7=BC=A9?=
=?UTF-8?q?=E8=83=BD=E6=AD=A3=E7=A1=AE=E5=A4=84=E7=90=86=E5=85=A8=E5=B1=80?=
=?UTF-8?q?=E7=9A=84=20min-response-size?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../plus/maa/backend/MainApplication.java | 2 --
.../backend/controller/CopilotController.java | 4 +--
.../backend/filter/MaaEtagHeaderFilter.java | 31 ++++++++++---------
3 files changed, 18 insertions(+), 19 deletions(-)
diff --git a/src/main/java/plus/maa/backend/MainApplication.java b/src/main/java/plus/maa/backend/MainApplication.java
index 20683a8d..91038693 100644
--- a/src/main/java/plus/maa/backend/MainApplication.java
+++ b/src/main/java/plus/maa/backend/MainApplication.java
@@ -3,7 +3,6 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
-import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@@ -18,7 +17,6 @@
@SpringBootApplication
@ConfigurationPropertiesScan
@EnableMethodSecurity
-@ServletComponentScan
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
diff --git a/src/main/java/plus/maa/backend/controller/CopilotController.java b/src/main/java/plus/maa/backend/controller/CopilotController.java
index 04da198e..9f1c1dba 100644
--- a/src/main/java/plus/maa/backend/controller/CopilotController.java
+++ b/src/main/java/plus/maa/backend/controller/CopilotController.java
@@ -78,8 +78,8 @@ public MaaResult getCopilotById(
public MaaResult queriesCopilot(
@Parameter(description = "作业查询请求") @Valid CopilotQueriesRequest parsed
) {
- // 两秒防抖,缓解前端重复请求问题
- response.setHeader(HttpHeaders.CACHE_CONTROL, "private, max-age=2, must-revalidate");
+ // 三秒防抖,缓解前端重复请求问题
+ response.setHeader(HttpHeaders.CACHE_CONTROL, "private, max-age=3, must-revalidate");
return MaaResult.success(copilotService.queriesCopilot(helper.getUserId(), parsed));
}
diff --git a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
index 4676a189..15207adf 100644
--- a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
+++ b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
@@ -2,16 +2,15 @@
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
-import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
-import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
+import org.springframework.stereotype.Component;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import java.io.IOException;
+import java.util.Set;
/**
@@ -20,14 +19,18 @@
* @author lixuhuilll
*/
-// 配置需要使用 Etag 机制的 URL,注意和 Spring 的 UrlPattern 语法不太一样
-@WebFilter(urlPatterns = {
- "/arknights/level",
- "/copilot/query"
-})
-@RequiredArgsConstructor
+@Component
public class MaaEtagHeaderFilter extends ShallowEtagHeaderFilter {
+ private static final String CACHE_HEAD = "private, no-cache, max-age=0, must-revalidate";
+ private static final String NO_STORE_HEAD = "no-store, " + CACHE_HEAD;
+
+ // 配置需要使用 Etag 机制的 URI,注意和 Spring 的 UrlPattern 语法不太一样
+ private static final Set CACHE_URI = Set.of(
+ "/arknights/level",
+ "/copilot/query"
+ );
+
@Override
protected void initFilterBean() throws ServletException {
super.initFilterBean();
@@ -37,13 +40,11 @@ protected void initFilterBean() throws ServletException {
@Override
protected void doFilterInternal(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull FilterChain filterChain) throws ServletException, IOException {
- if (!HttpMethod.GET.matches(request.getMethod())) {
- // ETag 只处理安全的请求
- filterChain.doFilter(request, response);
- return;
+ if (CACHE_URI.contains(request.getRequestURI())) {
+ response.setHeader(HttpHeaders.CACHE_CONTROL, CACHE_HEAD);
+ } else {
+ response.setHeader(HttpHeaders.CACHE_CONTROL, NO_STORE_HEAD);
}
- // 允许使用 Etag (实际是避免默认添加的 no-store)
- response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, max-age=0, must-revalidate");
// 其他接口默认处理即可,注意默认操作相当于牺牲 CPU 来节约网络带宽,不适用于结果变更过快的接口
super.doFilterInternal(request, response, filterChain);
}
From 095cdff49e5b1383d831b1e199698f3b6c9a47d9 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Sat, 18 Nov 2023 14:17:50 +0800
Subject: [PATCH 007/168] =?UTF-8?q?=E4=BC=98=E5=8C=96=20ETag=20=E8=BF=87?=
=?UTF-8?q?=E6=BB=A4=E5=99=A8=E7=9A=84=E5=B7=A5=E4=BD=9C=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../maa/backend/filter/MaaEtagHeaderFilter.java | 14 +++++++++-----
.../java/plus/maa/backend/filter/package-info.java | 4 ++++
2 files changed, 13 insertions(+), 5 deletions(-)
create mode 100644 src/main/java/plus/maa/backend/filter/package-info.java
diff --git a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
index 15207adf..9af71e0d 100644
--- a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
+++ b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
@@ -4,12 +4,12 @@
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
-import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import java.io.IOException;
+import java.io.InputStream;
import java.util.Set;
@@ -23,7 +23,6 @@
public class MaaEtagHeaderFilter extends ShallowEtagHeaderFilter {
private static final String CACHE_HEAD = "private, no-cache, max-age=0, must-revalidate";
- private static final String NO_STORE_HEAD = "no-store, " + CACHE_HEAD;
// 配置需要使用 Etag 机制的 URI,注意和 Spring 的 UrlPattern 语法不太一样
private static final Set CACHE_URI = Set.of(
@@ -39,13 +38,18 @@ protected void initFilterBean() throws ServletException {
}
@Override
- protected void doFilterInternal(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull FilterChain filterChain) throws ServletException, IOException {
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
if (CACHE_URI.contains(request.getRequestURI())) {
response.setHeader(HttpHeaders.CACHE_CONTROL, CACHE_HEAD);
- } else {
- response.setHeader(HttpHeaders.CACHE_CONTROL, NO_STORE_HEAD);
}
// 其他接口默认处理即可,注意默认操作相当于牺牲 CPU 来节约网络带宽,不适用于结果变更过快的接口
super.doFilterInternal(request, response, filterChain);
}
+
+ @Override
+ protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
+ int responseStatusCode, InputStream inputStream) {
+ return CACHE_URI.contains(request.getRequestURI()) &&
+ super.isEligibleForEtag(request, response, responseStatusCode, inputStream);
+ }
}
diff --git a/src/main/java/plus/maa/backend/filter/package-info.java b/src/main/java/plus/maa/backend/filter/package-info.java
new file mode 100644
index 00000000..19141fe0
--- /dev/null
+++ b/src/main/java/plus/maa/backend/filter/package-info.java
@@ -0,0 +1,4 @@
+@NonNullApi
+package plus.maa.backend.filter;
+
+import org.springframework.lang.NonNullApi;
\ No newline at end of file
From c2e2f1f0d7b99476fe318a69c66e7b519ada73d2 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Sat, 18 Nov 2023 15:00:06 +0800
Subject: [PATCH 008/168] =?UTF-8?q?=E8=BF=9B=E4=B8=80=E6=AD=A5=E4=BC=98?=
=?UTF-8?q?=E5=8C=96=20ETag=20=E8=BF=87=E6=BB=A4=E5=99=A8=E7=9A=84?=
=?UTF-8?q?=E5=B7=A5=E4=BD=9C=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../backend/filter/MaaEtagHeaderFilter.java | 30 +++++++++++--------
1 file changed, 17 insertions(+), 13 deletions(-)
diff --git a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
index 9af71e0d..1e9fb9f4 100644
--- a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
+++ b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
@@ -1,14 +1,13 @@
package plus.maa.backend.filter;
-import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
-import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
@@ -37,19 +36,24 @@ protected void initFilterBean() throws ServletException {
setWriteWeakETag(true);
}
- @Override
- protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
- if (CACHE_URI.contains(request.getRequestURI())) {
- response.setHeader(HttpHeaders.CACHE_CONTROL, CACHE_HEAD);
- }
- // 其他接口默认处理即可,注意默认操作相当于牺牲 CPU 来节约网络带宽,不适用于结果变更过快的接口
- super.doFilterInternal(request, response, filterChain);
- }
-
@Override
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
int responseStatusCode, InputStream inputStream) {
- return CACHE_URI.contains(request.getRequestURI()) &&
- super.isEligibleForEtag(request, response, responseStatusCode, inputStream);
+
+ if (CACHE_URI.contains(request.getRequestURI()) &&
+ !response.isCommitted() &&
+ responseStatusCode >= 200 && responseStatusCode < 300 &&
+ HttpMethod.GET.matches(request.getMethod())) {
+
+ String cacheControl = response.getHeader(HttpHeaders.CACHE_CONTROL);
+ if (cacheControl == null) {
+ response.setHeader(HttpHeaders.CACHE_CONTROL, CACHE_HEAD);
+ return true;
+ }
+
+ return !cacheControl.contains("no-store");
+ }
+
+ return false;
}
}
From 26d5924b544a77136cc7ca69b860198f69c5a438 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Sat, 18 Nov 2023 17:01:01 +0800
Subject: [PATCH 009/168] =?UTF-8?q?=E8=BF=9B=E4=B8=80=E6=AD=A5=E4=BC=98?=
=?UTF-8?q?=E5=8C=96=20ETag=20=E8=BF=87=E6=BB=A4=E5=99=A8=E7=9A=84?=
=?UTF-8?q?=E5=B7=A5=E4=BD=9C=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BD=BF=E5=85=B6?=
=?UTF-8?q?=E6=94=AF=E6=8C=81=20URI=20Pattern=20=E8=AF=AD=E6=B3=95?=
=?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=8C=B9=E9=85=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../backend/filter/MaaEtagHeaderFilter.java | 30 +++++++++++++++----
1 file changed, 25 insertions(+), 5 deletions(-)
diff --git a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
index 1e9fb9f4..e79b27b4 100644
--- a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
+++ b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
@@ -5,10 +5,14 @@
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
+import org.springframework.http.server.PathContainer;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
+import org.springframework.web.util.pattern.PathPattern;
+import org.springframework.web.util.pattern.PathPatternParser;
import java.io.InputStream;
+import java.util.List;
import java.util.Set;
@@ -21,14 +25,22 @@
@Component
public class MaaEtagHeaderFilter extends ShallowEtagHeaderFilter {
- private static final String CACHE_HEAD = "private, no-cache, max-age=0, must-revalidate";
-
- // 配置需要使用 Etag 机制的 URI,注意和 Spring 的 UrlPattern 语法不太一样
+ /**
+ * 配置需要使用 Etag 机制的 URI,采用 PathPatter 语法
+ *
+ * @see PathPattern
+ */
private static final Set CACHE_URI = Set.of(
"/arknights/level",
"/copilot/query"
);
+ private static final String CACHE_HEAD = "private, no-cache, max-age=0, must-revalidate";
+
+ private static final List CACHE_URI_PATTERNS = CACHE_URI.stream()
+ .map(PathPatternParser.defaultInstance::parse)
+ .toList();
+
@Override
protected void initFilterBean() throws ServletException {
super.initFilterBean();
@@ -40,8 +52,16 @@ protected void initFilterBean() throws ServletException {
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
int responseStatusCode, InputStream inputStream) {
- if (CACHE_URI.contains(request.getRequestURI()) &&
- !response.isCommitted() &&
+ boolean isMatch = false;
+
+ for (PathPattern pattern : CACHE_URI_PATTERNS) {
+ if (pattern.matches(PathContainer.parsePath(request.getRequestURI()))) {
+ isMatch = true;
+ break;
+ }
+ }
+
+ if (isMatch && !response.isCommitted() &&
responseStatusCode >= 200 && responseStatusCode < 300 &&
HttpMethod.GET.matches(request.getMethod())) {
From 24c34c3ed3fc6dbefd7bda2aa296a52a1e639720 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Sat, 18 Nov 2023 17:50:35 +0800
Subject: [PATCH 010/168] =?UTF-8?q?=E4=BC=98=E5=8C=96=20URI=20Pattern=20?=
=?UTF-8?q?=E5=8C=B9=E9=85=8D=E7=9A=84=E6=80=A7=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../maa/backend/filter/MaaEtagHeaderFilter.java | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
index e79b27b4..78e11367 100644
--- a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
+++ b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
@@ -54,16 +54,21 @@ protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletRespo
boolean isMatch = false;
- for (PathPattern pattern : CACHE_URI_PATTERNS) {
- if (pattern.matches(PathContainer.parsePath(request.getRequestURI()))) {
- isMatch = true;
- break;
+ if (HttpMethod.GET.matches(request.getMethod())) {
+
+ PathContainer pathContainer = PathContainer.parsePath(request.getRequestURI());
+
+ for (PathPattern pattern : CACHE_URI_PATTERNS) {
+
+ if (pattern.matches(pathContainer)) {
+ isMatch = true;
+ break;
+ }
}
}
if (isMatch && !response.isCommitted() &&
- responseStatusCode >= 200 && responseStatusCode < 300 &&
- HttpMethod.GET.matches(request.getMethod())) {
+ responseStatusCode >= 200 && responseStatusCode < 300) {
String cacheControl = response.getHeader(HttpHeaders.CACHE_CONTROL);
if (cacheControl == null) {
From c3bccdd81bd6c13be6587851624fecb386bbedb1 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Sun, 19 Nov 2023 11:07:45 +0800
Subject: [PATCH 011/168] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=E5=8F=AF=E8=AF=BB=E6=80=A7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../backend/filter/MaaEtagHeaderFilter.java | 36 ++++++++-----------
1 file changed, 14 insertions(+), 22 deletions(-)
diff --git a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
index 78e11367..585c4253 100644
--- a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
+++ b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
@@ -1,10 +1,8 @@
package plus.maa.backend.filter;
-import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
import org.springframework.http.server.PathContainer;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
@@ -18,6 +16,10 @@
/**
* 提供基于 Etag 机制的 HTTP 缓存,有助于降低网络传输的压力
+ *
+ * 同时还解决了 GZIP 无法对 JSON 响应正常处理 min-response-size 的问题,
+ * 借助了 ETag 处理流程中的 Response 包装类包装所有响应,
+ * 从而正常获取 Content-Length
*
* @author lixuhuilll
*/
@@ -42,8 +44,7 @@ public class MaaEtagHeaderFilter extends ShallowEtagHeaderFilter {
.toList();
@Override
- protected void initFilterBean() throws ServletException {
- super.initFilterBean();
+ protected void initFilterBean() {
// Etag 必须使用弱校验才能与自动压缩兼容
setWriteWeakETag(true);
}
@@ -52,33 +53,24 @@ protected void initFilterBean() throws ServletException {
protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
int responseStatusCode, InputStream inputStream) {
- boolean isMatch = false;
-
- if (HttpMethod.GET.matches(request.getMethod())) {
+ if (super.isEligibleForEtag(request, response, responseStatusCode, inputStream)) {
+ // 如果该请求符合产生 ETag 的条件,判断是否为需要使用 ETag 机制的 URI
PathContainer pathContainer = PathContainer.parsePath(request.getRequestURI());
-
for (PathPattern pattern : CACHE_URI_PATTERNS) {
if (pattern.matches(pathContainer)) {
- isMatch = true;
- break;
+ // 如果是需要使用 ETag 机制的 URI,若其响应中不存在缓存控制头,则配置默认值
+ String cacheControl = response.getHeader(HttpHeaders.CACHE_CONTROL);
+ if (cacheControl == null) {
+ response.setHeader(HttpHeaders.CACHE_CONTROL, CACHE_HEAD);
+ }
+ // 不论是否进行默认值处理,均返回 true
+ return true;
}
}
}
- if (isMatch && !response.isCommitted() &&
- responseStatusCode >= 200 && responseStatusCode < 300) {
-
- String cacheControl = response.getHeader(HttpHeaders.CACHE_CONTROL);
- if (cacheControl == null) {
- response.setHeader(HttpHeaders.CACHE_CONTROL, CACHE_HEAD);
- return true;
- }
-
- return !cacheControl.contains("no-store");
- }
-
return false;
}
}
From 041fd20fd6bce7df6690fa8a6b84cfa04d5b1087 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Sun, 19 Nov 2023 18:35:44 +0800
Subject: [PATCH 012/168] =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=8B=86=E5=88=86?=
=?UTF-8?q?=EF=BC=8C=E5=B0=86=20ETag=20=E5=92=8C=E8=8E=B7=E5=8F=96=20Conte?=
=?UTF-8?q?nt-Length=20=E5=88=86=E4=B8=BA=E4=B8=A4=E4=B8=AA=E8=BF=87?=
=?UTF-8?q?=E6=BB=A4=E5=99=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../filter/ContentLengthRepairFilter.java | 39 ++++++++++
.../backend/filter/MaaEtagHeaderFilter.java | 76 -------------------
.../MaaEtagHeaderFilterRegistrationBean.java | 65 ++++++++++++++++
3 files changed, 104 insertions(+), 76 deletions(-)
create mode 100644 src/main/java/plus/maa/backend/filter/ContentLengthRepairFilter.java
delete mode 100644 src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
create mode 100644 src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilterRegistrationBean.java
diff --git a/src/main/java/plus/maa/backend/filter/ContentLengthRepairFilter.java b/src/main/java/plus/maa/backend/filter/ContentLengthRepairFilter.java
new file mode 100644
index 00000000..71953ddd
--- /dev/null
+++ b/src/main/java/plus/maa/backend/filter/ContentLengthRepairFilter.java
@@ -0,0 +1,39 @@
+package plus.maa.backend.filter;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.ShallowEtagHeaderFilter;
+import org.springframework.web.util.ContentCachingResponseWrapper;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * 解决了 GZIP 无法对 JSON 响应正常处理 min-response-size 的问题,
+ * 借助了 ETag 处理流程中的 Response 包装类包装所有响应,
+ * 从而正常获取 Content-Length
+ *
+ * @author lixuhuilll
+ */
+
+@Component
+public class ContentLengthRepairFilter extends ShallowEtagHeaderFilter {
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
+ if (response instanceof ContentCachingResponseWrapper) {
+ // 不对已包装过的响应体做处理
+ filterChain.doFilter(request, response);
+ } else {
+ super.doFilterInternal(request, response, filterChain);
+ }
+ }
+
+ @Override
+ protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response, int responseStatusCode, InputStream inputStream) {
+ return false;
+ }
+}
diff --git a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
deleted file mode 100644
index 585c4253..00000000
--- a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilter.java
+++ /dev/null
@@ -1,76 +0,0 @@
-package plus.maa.backend.filter;
-
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.server.PathContainer;
-import org.springframework.stereotype.Component;
-import org.springframework.web.filter.ShallowEtagHeaderFilter;
-import org.springframework.web.util.pattern.PathPattern;
-import org.springframework.web.util.pattern.PathPatternParser;
-
-import java.io.InputStream;
-import java.util.List;
-import java.util.Set;
-
-
-/**
- * 提供基于 Etag 机制的 HTTP 缓存,有助于降低网络传输的压力
- *
- * 同时还解决了 GZIP 无法对 JSON 响应正常处理 min-response-size 的问题,
- * 借助了 ETag 处理流程中的 Response 包装类包装所有响应,
- * 从而正常获取 Content-Length
- *
- * @author lixuhuilll
- */
-
-@Component
-public class MaaEtagHeaderFilter extends ShallowEtagHeaderFilter {
-
- /**
- * 配置需要使用 Etag 机制的 URI,采用 PathPatter 语法
- *
- * @see PathPattern
- */
- private static final Set CACHE_URI = Set.of(
- "/arknights/level",
- "/copilot/query"
- );
-
- private static final String CACHE_HEAD = "private, no-cache, max-age=0, must-revalidate";
-
- private static final List CACHE_URI_PATTERNS = CACHE_URI.stream()
- .map(PathPatternParser.defaultInstance::parse)
- .toList();
-
- @Override
- protected void initFilterBean() {
- // Etag 必须使用弱校验才能与自动压缩兼容
- setWriteWeakETag(true);
- }
-
- @Override
- protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
- int responseStatusCode, InputStream inputStream) {
-
- if (super.isEligibleForEtag(request, response, responseStatusCode, inputStream)) {
-
- // 如果该请求符合产生 ETag 的条件,判断是否为需要使用 ETag 机制的 URI
- PathContainer pathContainer = PathContainer.parsePath(request.getRequestURI());
- for (PathPattern pattern : CACHE_URI_PATTERNS) {
-
- if (pattern.matches(pathContainer)) {
- // 如果是需要使用 ETag 机制的 URI,若其响应中不存在缓存控制头,则配置默认值
- String cacheControl = response.getHeader(HttpHeaders.CACHE_CONTROL);
- if (cacheControl == null) {
- response.setHeader(HttpHeaders.CACHE_CONTROL, CACHE_HEAD);
- }
- // 不论是否进行默认值处理,均返回 true
- return true;
- }
- }
- }
-
- return false;
- }
-}
diff --git a/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilterRegistrationBean.java b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilterRegistrationBean.java
new file mode 100644
index 00000000..239bebfc
--- /dev/null
+++ b/src/main/java/plus/maa/backend/filter/MaaEtagHeaderFilterRegistrationBean.java
@@ -0,0 +1,65 @@
+package plus.maa.backend.filter;
+
+import jakarta.annotation.PostConstruct;
+import jakarta.servlet.Filter;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
+import org.springframework.http.HttpHeaders;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.ShallowEtagHeaderFilter;
+
+import java.io.InputStream;
+import java.util.Set;
+
+
+/**
+ * 提供基于 Etag 机制的 HTTP 缓存,有助于降低网络传输的压力
+ *
+ * @author lixuhuilll
+ */
+
+@Component
+public class MaaEtagHeaderFilterRegistrationBean extends FilterRegistrationBean {
+
+ /**
+ * 配置需要使用 Etag 机制的 URI,采用 Servlet 的 URI 匹配语法
+ */
+ private final Set ETAG_URI = Set.of(
+ "/arknights/level",
+ "/copilot/query"
+ );
+
+ @PostConstruct
+ public void init() {
+ setFilter(new MaaEtagHeaderFilter());
+ setUrlPatterns(ETAG_URI);
+ }
+
+ private static class MaaEtagHeaderFilter extends ShallowEtagHeaderFilter {
+
+ private static final String CACHE_HEAD = "private, no-cache, max-age=0, must-revalidate";
+
+ @Override
+ protected void initFilterBean() {
+ // Etag 必须使用弱校验才能与自动压缩兼容
+ setWriteWeakETag(true);
+ }
+
+ @Override
+ protected boolean isEligibleForEtag(HttpServletRequest request, HttpServletResponse response,
+ int responseStatusCode, InputStream inputStream) {
+
+ if (super.isEligibleForEtag(request, response, responseStatusCode, inputStream)) {
+ // 使用 ETag 机制的 URI,若其响应中不存在缓存控制头,则配置默认值
+ final String cacheControl = response.getHeader(HttpHeaders.CACHE_CONTROL);
+ if (cacheControl == null) {
+ response.setHeader(HttpHeaders.CACHE_CONTROL, CACHE_HEAD);
+ }
+ return true;
+ }
+
+ return false;
+ }
+ }
+}
From 65b895c60cb50bf30ef1d03a9abf84a4db80bca7 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Sun, 19 Nov 2023 23:57:40 +0800
Subject: [PATCH 013/168] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20ContentLengthRepai?=
=?UTF-8?q?rFilter=20=E7=9A=84=E6=9D=A1=E4=BB=B6=E8=A3=85=E9=85=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/plus/maa/backend/filter/ContentLengthRepairFilter.java | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/main/java/plus/maa/backend/filter/ContentLengthRepairFilter.java b/src/main/java/plus/maa/backend/filter/ContentLengthRepairFilter.java
index 71953ddd..fc2d1fc5 100644
--- a/src/main/java/plus/maa/backend/filter/ContentLengthRepairFilter.java
+++ b/src/main/java/plus/maa/backend/filter/ContentLengthRepairFilter.java
@@ -4,6 +4,7 @@
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.ShallowEtagHeaderFilter;
import org.springframework.web.util.ContentCachingResponseWrapper;
@@ -20,6 +21,7 @@
*/
@Component
+@ConditionalOnProperty(name = "server.compression.enabled", havingValue = "true")
public class ContentLengthRepairFilter extends ShallowEtagHeaderFilter {
@Override
From 80e47319d6c5efaf367bf49d09b42ae0491347a0 Mon Sep 17 00:00:00 2001
From: lixuhuilll <676824363@qq.com>
Date: Thu, 23 Nov 2023 21:22:22 +0800
Subject: [PATCH 014/168] =?UTF-8?q?=E7=83=AD=E5=BA=A6=E6=8E=92=E8=A1=8C?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=99=8D=E4=BD=8E=E6=9C=AA=E5=BC=80=E6=94=BE?=
=?UTF-8?q?=E5=85=B3=E5=8D=A1=E6=9D=83=E9=87=8D=E7=9A=84=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../response/copilot/ArkLevelInfo.java | 4 +
.../backend/repository/GithubRepository.java | 4 +
.../backend/repository/entity/ArkLevel.java | 4 +
.../entity/github/GithubContent.java | 7 ++
.../maa/backend/service/ArkLevelService.java | 83 +++++++++++++++++++
.../maa/backend/task/ArkLevelSyncTask.java | 12 ++-
.../backend/task/CopilotScoreRefreshTask.java | 13 ++-
7 files changed, 123 insertions(+), 4 deletions(-)
diff --git a/src/main/java/plus/maa/backend/controller/response/copilot/ArkLevelInfo.java b/src/main/java/plus/maa/backend/controller/response/copilot/ArkLevelInfo.java
index 7df5976d..27e19731 100644
--- a/src/main/java/plus/maa/backend/controller/response/copilot/ArkLevelInfo.java
+++ b/src/main/java/plus/maa/backend/controller/response/copilot/ArkLevelInfo.java
@@ -5,6 +5,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
+import org.jetbrains.annotations.Nullable;
import java.io.Serializable;
@@ -25,4 +26,7 @@ public class ArkLevelInfo implements Serializable {
private String name;
private int width;
private int height;
+ // 当前版本地图是否开放
+ @Nullable
+ private Boolean isOpen;
}
diff --git a/src/main/java/plus/maa/backend/repository/GithubRepository.java b/src/main/java/plus/maa/backend/repository/GithubRepository.java
index 69872aa4..7d495c20 100644
--- a/src/main/java/plus/maa/backend/repository/GithubRepository.java
+++ b/src/main/java/plus/maa/backend/repository/GithubRepository.java
@@ -4,6 +4,7 @@
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.service.annotation.GetExchange;
import plus.maa.backend.repository.entity.github.GithubCommit;
+import plus.maa.backend.repository.entity.github.GithubContent;
import plus.maa.backend.repository.entity.github.GithubTrees;
import java.util.List;
@@ -23,4 +24,7 @@ public interface GithubRepository {
@GetExchange(value = "/repos/MaaAssistantArknights/MaaAssistantArknights/commits")
List getCommits(@RequestHeader("Authorization") String token);
+ @GetExchange(value = "/repos/MaaAssistantArknights/MaaAssistantArknights/contents/{path}")
+ List getContents(@RequestHeader("Authorization") String token, @PathVariable("path") String path);
+
}
diff --git a/src/main/java/plus/maa/backend/repository/entity/ArkLevel.java b/src/main/java/plus/maa/backend/repository/entity/ArkLevel.java
index da54514c..fd5d9238 100644
--- a/src/main/java/plus/maa/backend/repository/entity/ArkLevel.java
+++ b/src/main/java/plus/maa/backend/repository/entity/ArkLevel.java
@@ -4,6 +4,7 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
+import org.jetbrains.annotations.Nullable;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
@@ -38,4 +39,7 @@ public class ArkLevel {
private String name;
private int width;
private int height;
+ // 当前版本地图是否开放
+ @Nullable
+ private Boolean isOpen;
}
diff --git a/src/main/java/plus/maa/backend/repository/entity/github/GithubContent.java b/src/main/java/plus/maa/backend/repository/entity/github/GithubContent.java
index eb69dda6..5cd6d80f 100644
--- a/src/main/java/plus/maa/backend/repository/entity/github/GithubContent.java
+++ b/src/main/java/plus/maa/backend/repository/entity/github/GithubContent.java
@@ -1,5 +1,7 @@
package plus.maa.backend.repository.entity.github;
+import com.fasterxml.jackson.databind.PropertyNamingStrategies;
+import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
@@ -10,6 +12,7 @@
* created on 2022/12/23
*/
@Data
+@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class GithubContent {
// 文件名
@@ -37,6 +40,10 @@ public boolean isDir() {
return Objects.equals(type, "dir");
}
+ public boolean isFile() {
+ return Objects.equals(type, "file");
+ }
+
public String getFileExtension() {
return name == null ?
StringUtils.EMPTY :
diff --git a/src/main/java/plus/maa/backend/service/ArkLevelService.java b/src/main/java/plus/maa/backend/service/ArkLevelService.java
index d7ad06de..26a48455 100644
--- a/src/main/java/plus/maa/backend/service/ArkLevelService.java
+++ b/src/main/java/plus/maa/backend/service/ArkLevelService.java
@@ -1,5 +1,6 @@
package plus.maa.backend.service;
+import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.RequiredArgsConstructor;
@@ -8,6 +9,8 @@
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@@ -20,6 +23,7 @@
import plus.maa.backend.repository.entity.ArkLevelSha;
import plus.maa.backend.repository.entity.gamedata.ArkTilePos;
import plus.maa.backend.repository.entity.github.GithubCommit;
+import plus.maa.backend.repository.entity.github.GithubContent;
import plus.maa.backend.repository.entity.github.GithubTree;
import plus.maa.backend.repository.entity.github.GithubTrees;
@@ -28,6 +32,8 @@
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -161,6 +167,83 @@ public void runSyncLevelDataTask() {
levelTrees.forEach(tree -> download(task, tree));
}
+ /**
+ * 更新地图开放状态
+ */
+ public void updateLevelOpenStatus() {
+ log.info("[LEVEL-OPEN-STATUS]准备更新地图开放状态");
+ GithubContent stages = githubRepo.getContents(githubToken, "resource").stream()
+ .filter(content -> content.isFile() && "stages.json".equals(content.getName()))
+ .findFirst()
+ .orElse(null);
+ if (stages == null) {
+ log.info("[LEVEL-OPEN-STATUS]地图开放状态数据不存在");
+ return;
+ }
+
+ String lastStagesSha = redisCache.getCache("level:stages:sha", String.class);
+ if (lastStagesSha != null && lastStagesSha.equals(stages.getSha())) {
+ log.info("[LEVEL-OPEN-STATUS]地图开放状态已是最新");
+ return;
+ }
+
+ log.info("[LEVEL-OPEN-STATUS]开始更新地图开放状态");
+ // 就一个文件,直接在当前线程下载数据
+ try (Response response = okHttpClient
+ .newCall(new Request.Builder().url(stages.getDownloadUrl()).build())
+ .execute()) {
+
+ if (!response.isSuccessful() || response.body() == null) {
+ log.error("[LEVEL-OPEN-STATUS]地图开放状态下载失败");
+ return;
+ }
+
+ String body = response.body().string();
+ List