feat(repair): 新增全库修复+重刮入口,剧集类路径正确归类

- 服务层 RepairAndRescrapeAllLibraries:先从路径占位符回填缺失/错误外部ID,再逐库重刮(含 no_match 重试)
- 新增 handler 与路由 POST /api/admin/media/repair-rescrape,异步执行经 WS 推送进度
- 前端媒体库页新增「全库修复+重刮」按钮调用该接口
- groupSeries:路径落在剧集类目录(电视剧/动漫/特别篇等)时按剧集卡处理,跳转分类视图
This commit is contained in:
ShukeBta
2026-06-19 11:41:34 +08:00
parent ce38fd257f
commit 87bb94d4df
6 changed files with 161 additions and 6 deletions
+38
View File
@@ -0,0 +1,38 @@
// Package handler — batch repair+rescrape endpoint.
package handler
import (
"context"
"net/http"
"github.com/gin-gonic/gin"
"github.com/ShukeBta/MediaStationGo/internal/service"
)
// repairAndRescrapeAllHandler 触发"全库修复+重刮"流程:先从媒体路径中的
// {tmdb-N}/{bangumi-N} 等占位符回填缺失的外部 ID, 再对所有媒体库重刮一遍。
//
// 路由: POST /api/admin/media/repair-rescrape (需 admin)
// 异步执行, 立即返回 202;通过 WS hub "scrape" topic 推送进度。
func repairAndRescrapeAllHandler(svc *service.Container) gin.HandlerFunc {
return func(c *gin.Context) {
task := startScrapeHTTPTask(svc, "全库修复并重刮", "", "")
go func() {
result, err := svc.RepairAndRescrapeAllLibraries(context.Background())
metrics := map[string]int64{
"repaired": int64(result.Repaired),
"libraries": int64(result.Libraries),
"matched": int64(result.Matched),
}
stage := "completed"
message := "全库修复并重刮完成"
if err != nil {
stage = "scrape"
message = "全库修复并重刮失败"
}
finishHTTPTask(task, err, stage, message, metrics, nil)
}()
c.JSON(http.StatusAccepted, gin.H{"status": "started"})
}
}
+3
View File
@@ -87,6 +87,9 @@ func registerAdminRoutes(api *gin.RouterGroup, cfg *config.Config, svc *service.
admin.GET("/organize/sources", organizeSourcesHandler(svc))
admin.POST("/organize/source", organizeDirectoryHandler(svc))
// 全库修复+重刮:从路径占位符回填缺失外部 ID,然后批量重刮整库。
admin.POST("/media/repair-rescrape", repairAndRescrapeAllHandler(svc))
// API key management (encrypted at rest).
admin.GET("/api-configs", listAPIConfigsHandler(svc))
admin.GET("/api-configs/:provider", getAPIConfigHandler(svc))
+60
View File
@@ -106,3 +106,63 @@ func cloudPathRepairShouldReplaceTitle(current, hinted string) bool {
}
return len([]rune(current)) > len([]rune(hinted))*2
}
// RepairAndRescrapeResult 汇总一次「全库修复+重刮」的结果。
type RepairAndRescrapeResult struct {
Repaired int `json:"repaired"` // 从路径占位符回填外部 ID 的媒体数
Libraries int `json:"libraries"` // 参与重刮的媒体库数
Matched int `json:"matched"` // 重刮后成功匹配的媒体数
}
// RepairAndRescrapeAllLibraries 修复并重刮所有媒体库:先从媒体路径中的
// {tmdb-123}/{bangumi-456} 等占位符回填缺失或错误的外部 ID(回填后会把相关
// 行的 scrape_status 重置为 pending),随后逐个媒体库重刮(含 no_match 重试),
// 让此前因空 ID / 脏 ID 无法刮削的媒体重新匹配到正确数据。
func (c *Container) RepairAndRescrapeAllLibraries(ctx context.Context) (RepairAndRescrapeResult, error) {
var result RepairAndRescrapeResult
if c == nil || c.Repo == nil || c.Repo.DB == nil {
return result, nil
}
repaired, err := c.RepairCloudPathMetadata(ctx)
if err != nil {
return result, err
}
result.Repaired = repaired
if c.Scraper == nil || c.Repo.Library == nil {
return result, nil
}
libraries, err := c.Repo.Library.List(ctx)
if err != nil {
return result, err
}
for i := range libraries {
select {
case <-ctx.Done():
return result, ctx.Err()
default:
}
lib := libraries[i]
if !lib.Enabled {
continue
}
result.Libraries++
// retryNoMatch=true:连之前匹配失败的也再试一次,因为这次可能已回填到正确 ID。
matched, err := c.Scraper.EnrichLibrary(ctx, lib.ID, true)
if err != nil {
if c.Log != nil {
c.Log.Warn("repair rescrape library failed", zap.String("library", lib.ID), zap.Error(err))
}
continue
}
result.Matched += matched
}
if c.Log != nil {
c.Log.Info("repair and rescrape all libraries done",
zap.Int("repaired", result.Repaired),
zap.Int("libraries", result.Libraries),
zap.Int("matched", result.Matched))
}
return result, nil
}