refactor: 代理接口纯路径透传,存过的返回/没存过404,脚本可回退实时HTTP
This commit is contained in:
@@ -39,14 +39,9 @@ public class AccessResultController {
|
||||
* ctx.siteUrl 指向:http://par:9012/api/v1/admin/sites/{siteId}/content
|
||||
*/
|
||||
/**
|
||||
* 内容代理:支持两种方式调用 ——
|
||||
* 1. 真实路径:GET /admin/sites/{siteId}/content/index.php?search=xxx
|
||||
* 2. 页面类型:GET /admin/sites/{siteId}/content/sync
|
||||
* 页面类型映射(按站点类型自动匹配):
|
||||
* sync → index.php (或 api/member/profile)
|
||||
* search → torrents.php (或 api/torrent/search)
|
||||
* sign → attendance.php
|
||||
* detail → details.php
|
||||
* 内容代理:接收相对路径,返回存储的访问结果内容(最新一条)。
|
||||
* 脚本调用 httpGet(ctx, '/index.php') → 查找 URL 中包含该路径的存储内容。
|
||||
* 存过的返回缓存,没存过的返回 404(脚本可回退到实时 HTTP 请求)。
|
||||
*/
|
||||
@GetMapping("/api/v1/admin/sites/{siteId}/content/**")
|
||||
public ResponseEntity<String> contentProxy(
|
||||
@@ -54,13 +49,9 @@ public class AccessResultController {
|
||||
HttpServletRequest request) {
|
||||
String fullPath = request.getRequestURI();
|
||||
String prefix = "/api/v1/admin/sites/" + siteId + "/content";
|
||||
String rawPath = fullPath.substring(fullPath.indexOf(prefix) + prefix.length());
|
||||
String relativePath = fullPath.substring(fullPath.indexOf(prefix) + prefix.length());
|
||||
String query = request.getQueryString();
|
||||
|
||||
// 页面类型 → URL 模式映射
|
||||
String relativePath = resolvePageType(rawPath, siteId);
|
||||
String pathWithQuery = relativePath + (query != null ? "?" + query : "");
|
||||
if (pathWithQuery.isEmpty()) pathWithQuery = "/";
|
||||
|
||||
List<AccessResult> results = accessResultMapper.selectList(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<AccessResult>()
|
||||
@@ -68,14 +59,15 @@ public class AccessResultController {
|
||||
.orderByDesc(AccessResult::getId)
|
||||
.last("limit 200"));
|
||||
|
||||
// 优先精确匹配,否则模糊匹配
|
||||
// URL 以 pathWithQuery 结尾 → 精确匹配,否则回退模糊匹配
|
||||
AccessResult best = results.stream()
|
||||
.filter(r -> r.getUrl() != null && r.getStoragePath() != null &&
|
||||
(r.getUrl().endsWith(pathWithQuery) || r.getUrl().contains(pathWithQuery)))
|
||||
r.getUrl().endsWith(pathWithQuery))
|
||||
.findFirst().orElse(null);
|
||||
if (best == null) {
|
||||
if (best == null && !relativePath.isEmpty()) {
|
||||
best = results.stream()
|
||||
.filter(r -> r.getUrl() != null && r.getStoragePath() != null && r.getUrl().contains(relativePath))
|
||||
.filter(r -> r.getUrl() != null && r.getStoragePath() != null &&
|
||||
r.getUrl().contains(relativePath))
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
if (best == null) return ResponseEntity.notFound().build();
|
||||
@@ -87,18 +79,6 @@ public class AccessResultController {
|
||||
return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType + ";charset=UTF-8")).body(content);
|
||||
}
|
||||
|
||||
/** 页面类型 → URL 模式 */
|
||||
private String resolvePageType(String type, String siteId) {
|
||||
return switch (type) {
|
||||
case "/sync", "/sync/" -> ".php?file=index"; // 通用匹配 index.php
|
||||
case "/search", "/search/" -> "torrents.php";
|
||||
case "/sign", "/sign/" -> "attendance.php";
|
||||
case "/detail", "/detail/" -> "details.php";
|
||||
case "/profile", "/profile/" -> "api/member/profile";
|
||||
default -> type; // 原样传递(真实路径)
|
||||
};
|
||||
}
|
||||
|
||||
// ==================== mediabot 提交接口(PSK 鉴权) ====================
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user