fix: 内容代理支持查询参数+精确匹配+回退+JSON content-type
This commit is contained in:
@@ -38,6 +38,12 @@ public class AccessResultController {
|
||||
* Groovy 脚本中 httpGet(ctx, '/index.php') 通过此接口获取已存储的页面内容。
|
||||
* ctx.siteUrl 指向:http://par:9012/api/v1/admin/sites/{siteId}/content
|
||||
*/
|
||||
/**
|
||||
* 内容代理:根据路径和可选查询参数返回存储内容。
|
||||
* 匹配规则:URL 以请求路径结尾(含查询参数),返回最新匹配项。
|
||||
* GET /admin/sites/{siteId}/content/index.php
|
||||
* GET /admin/sites/{siteId}/content/torrents.php?search=keyword
|
||||
*/
|
||||
@GetMapping("/api/v1/admin/sites/{siteId}/content/**")
|
||||
public ResponseEntity<String> contentProxy(
|
||||
@PathVariable("siteId") String siteId,
|
||||
@@ -45,23 +51,35 @@ public class AccessResultController {
|
||||
String fullPath = request.getRequestURI();
|
||||
String prefix = "/api/v1/admin/sites/" + siteId + "/content";
|
||||
String relativePath = fullPath.substring(fullPath.indexOf(prefix) + prefix.length());
|
||||
if (relativePath.isEmpty()) relativePath = "/";
|
||||
String query = request.getQueryString();
|
||||
String pathWithQuery = relativePath + (query != null ? "?" + query : "");
|
||||
if (pathWithQuery.isEmpty()) pathWithQuery = "/";
|
||||
|
||||
// 查找最新匹配的存储内容
|
||||
List<AccessResult> results = accessResultMapper.selectList(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<AccessResult>()
|
||||
.eq(AccessResult::getSiteId, siteId)
|
||||
.orderByDesc(AccessResult::getId)
|
||||
.last("limit 100"));
|
||||
String path = relativePath;
|
||||
.last("limit 200"));
|
||||
|
||||
// 优先精确匹配(URL 以 pathWithQuery 结尾),否则模糊匹配
|
||||
AccessResult best = results.stream()
|
||||
.filter(r -> r.getUrl() != null && r.getStoragePath() != null && r.getUrl().contains(path))
|
||||
.filter(r -> r.getUrl() != null && r.getStoragePath() != null &&
|
||||
(r.getUrl().endsWith(pathWithQuery) || r.getUrl().contains(pathWithQuery)))
|
||||
.findFirst().orElse(null);
|
||||
if (best == null) {
|
||||
// 回退:URL 包含相对路径(不含查询参数)
|
||||
String pathOnly = relativePath;
|
||||
best = results.stream()
|
||||
.filter(r -> r.getUrl() != null && r.getStoragePath() != null && r.getUrl().contains(pathOnly))
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
if (best == null) return ResponseEntity.notFound().build();
|
||||
|
||||
String content = storageService.read(best.getStoragePath());
|
||||
if (content == null) return ResponseEntity.notFound().build();
|
||||
return ResponseEntity.ok().contentType(MediaType.TEXT_HTML).body(content);
|
||||
|
||||
String contentType = "json".equalsIgnoreCase(best.getContentType()) ? "application/json" : "text/html";
|
||||
return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType + ";charset=UTF-8")).body(content);
|
||||
}
|
||||
|
||||
// ==================== mediabot 提交接口(PSK 鉴权) ====================
|
||||
|
||||
Reference in New Issue
Block a user