This commit is contained in:
toom1996
2026-09-13 00:52:35 +08:00
parent 10d8a96e8c
commit dbee704c95
44 changed files with 755 additions and 1148 deletions

View File

@ -3,7 +3,6 @@ package handler
import (
"errors"
"net/http"
"sort"
"strconv"
"fashionapi/internal/dto"
@ -64,39 +63,11 @@ func (h *IngestHandler) CrawlBrands(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"brands": brands})
}
// CrawlExists 图集预检接口(POST /admin/internal/crawl/exists,复用 IngestAuth HMAC 验签)。
//
// 爬虫在抓详情页之前批量问「这些 source_url 是否已爬取过」,跳过已存在的图集,
// 省掉「抓详情页 → 提取图片 URL → 上报 → worker 判重后丢弃」这一整套无效动作。
// 判定条件与 worker 判重一致,因此跳过是安全的:不会漏抓,也不会重复抓。
func (h *IngestHandler) CrawlExists(c *gin.Context) {
var req dto.CrawlExistsRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid json: " + err.Error()})
return
}
exists, err := h.ingest.ExistsSourceURLs(c.Request.Context(), req.SourceURLs)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "query failed"})
return
}
existing := make([]string, 0, len(exists))
for u := range exists {
existing = append(existing, u)
}
// 排序保证输出稳定,便于日志比对与测试断言。
sort.Strings(existing)
c.JSON(http.StatusOK, dto.CrawlExistsResponse{
Existing: existing,
ExistingCount: len(existing),
})
}
func isClientErr(err error) bool {
// service 层 NewError 带 http 状态码;这里简单按消息前缀判断,避免暴露内部类型。
if err == nil {
return false
}
msg := err.Error()
return len(msg) > 0 && (msg == "source_url required" || msg == "brand_uid required")
return len(msg) > 0 && msg == "brand_uid required"
}