feat: 内容代理接口 admin/sites/{siteId}/content/**,Groovy脚本httpGet对齐
This commit is contained in:
@@ -5,10 +5,14 @@ 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;
|
||||
@@ -24,6 +28,41 @@ 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
|
||||
*/
|
||||
@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());
|
||||
if (relativePath.isEmpty()) relativePath = "/";
|
||||
|
||||
// 查找最新匹配的存储内容
|
||||
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;
|
||||
AccessResult best = results.stream()
|
||||
.filter(r -> r.getUrl() != null && r.getStoragePath() != null && r.getUrl().contains(path))
|
||||
.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);
|
||||
}
|
||||
|
||||
// ==================== mediabot 提交接口(PSK 鉴权) ====================
|
||||
|
||||
|
||||
Reference in New Issue
Block a user