refactor: 回退,去掉内容代理,回归简单方案
This commit is contained in:
@@ -5,14 +5,10 @@ import com.par.core.dto.ApiResponse;
|
||||
import com.par.core.dto.SiteAccessSummaryDTO;
|
||||
import com.par.core.dto.SubmitResult;
|
||||
import com.par.core.entity.AccessResult;
|
||||
import com.par.core.mapper.AccessResultMapper;
|
||||
import com.par.core.service.AccessResultService;
|
||||
import com.par.core.storage.CachedStorageService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@@ -28,56 +24,6 @@ import java.util.Map;
|
||||
public class AccessResultController {
|
||||
|
||||
private final AccessResultService accessResultService;
|
||||
private final CachedStorageService storageService;
|
||||
private final AccessResultMapper accessResultMapper;
|
||||
|
||||
// ==================== 内容代理(供 Groovy 脚本 httpGet 调用) ====================
|
||||
|
||||
/**
|
||||
* 内容代理接口:接收相对路径,返回存储的访问结果内容。
|
||||
* Groovy 脚本中 httpGet(ctx, '/index.php') 通过此接口获取已存储的页面内容。
|
||||
* ctx.siteUrl 指向:http://par:9012/api/v1/admin/sites/{siteId}/content
|
||||
*/
|
||||
/**
|
||||
* 内容代理:接收相对路径,返回存储的访问结果内容(最新一条)。
|
||||
* 脚本调用 httpGet(ctx, '/index.php') → 查找 URL 中包含该路径的存储内容。
|
||||
* 存过的返回缓存,没存过的返回 404(脚本可回退到实时 HTTP 请求)。
|
||||
*/
|
||||
@GetMapping("/api/v1/admin/sites/{siteId}/content/**")
|
||||
public ResponseEntity<String> contentProxy(
|
||||
@PathVariable("siteId") String siteId,
|
||||
HttpServletRequest request) {
|
||||
String fullPath = request.getRequestURI();
|
||||
String prefix = "/api/v1/admin/sites/" + siteId + "/content";
|
||||
String relativePath = fullPath.substring(fullPath.indexOf(prefix) + prefix.length());
|
||||
String query = request.getQueryString();
|
||||
String pathWithQuery = relativePath + (query != null ? "?" + query : "");
|
||||
|
||||
List<AccessResult> results = accessResultMapper.selectList(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<AccessResult>()
|
||||
.eq(AccessResult::getSiteId, siteId)
|
||||
.orderByDesc(AccessResult::getId)
|
||||
.last("limit 200"));
|
||||
|
||||
// URL 以 pathWithQuery 结尾 → 精确匹配,否则回退模糊匹配
|
||||
AccessResult best = results.stream()
|
||||
.filter(r -> r.getUrl() != null && r.getStoragePath() != null &&
|
||||
r.getUrl().endsWith(pathWithQuery))
|
||||
.findFirst().orElse(null);
|
||||
if (best == null && !relativePath.isEmpty()) {
|
||||
best = results.stream()
|
||||
.filter(r -> r.getUrl() != null && r.getStoragePath() != null &&
|
||||
r.getUrl().contains(relativePath))
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
if (best == null) return ResponseEntity.notFound().build();
|
||||
|
||||
String content = storageService.read(best.getStoragePath());
|
||||
if (content == null) return ResponseEntity.notFound().build();
|
||||
|
||||
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