mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-05 04:56:37 +08:00
fix full library display and cloud search visibility
This commit is contained in:
@@ -171,6 +171,22 @@ func getMediaHandler(svc *service.Container) gin.HandlerFunc {
|
||||
func searchMediaHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
q := c.Query("q")
|
||||
if c.Query("page") != "" || c.Query("page_size") != "" {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
size, _ := strconv.Atoi(c.DefaultQuery("page_size", "50"))
|
||||
items, total, err := svc.Media.SearchMediaVisiblePage(c.Request.Context(), q, page, size, mediaVisibilityForRequest(c, svc))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"items": items,
|
||||
"total": total,
|
||||
"page": page,
|
||||
"page_size": size,
|
||||
})
|
||||
return
|
||||
}
|
||||
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "50"))
|
||||
items, err := svc.Media.SearchMediaVisible(c.Request.Context(), q, limit, mediaVisibilityForRequest(c, svc))
|
||||
if err != nil {
|
||||
|
||||
@@ -461,46 +461,58 @@ func (r *MediaRepository) Search(ctx context.Context, query string, limit int) (
|
||||
}
|
||||
|
||||
func (r *MediaRepository) SearchFiltered(ctx context.Context, query string, limit int, filter MediaQueryFilter) ([]model.Media, error) {
|
||||
items, _, err := r.SearchFilteredPage(ctx, query, 0, limit, filter)
|
||||
return items, err
|
||||
}
|
||||
|
||||
func (r *MediaRepository) SearchFilteredPage(ctx context.Context, query string, offset, limit int, filter MediaQueryFilter) ([]model.Media, int64, error) {
|
||||
query = strings.TrimSpace(query)
|
||||
if limit <= 0 {
|
||||
limit = 50
|
||||
}
|
||||
if query != "" {
|
||||
if items, ok := r.searchFilteredFTS(ctx, query, limit, filter); ok {
|
||||
if len(items) > 0 {
|
||||
return items, nil
|
||||
if items, total, ok := r.searchFilteredFTS(ctx, query, offset, limit, filter); ok {
|
||||
if total > 0 {
|
||||
return items, total, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return r.searchFilteredLIKE(ctx, query, limit, filter)
|
||||
return r.searchFilteredLIKE(ctx, query, offset, limit, filter)
|
||||
}
|
||||
|
||||
func (r *MediaRepository) searchFilteredFTS(ctx context.Context, query string, limit int, filter MediaQueryFilter) ([]model.Media, bool) {
|
||||
func (r *MediaRepository) searchFilteredFTS(ctx context.Context, query string, offset, limit int, filter MediaQueryFilter) ([]model.Media, int64, bool) {
|
||||
if !r.searchIndexEnabled(ctx) {
|
||||
return nil, false
|
||||
return nil, 0, false
|
||||
}
|
||||
ftsQuery := mediaFTSQuery(query)
|
||||
if ftsQuery == "" {
|
||||
return nil, false
|
||||
return nil, 0, false
|
||||
}
|
||||
var total int64
|
||||
var items []model.Media
|
||||
q := r.db.WithContext(ctx).
|
||||
Table("media").
|
||||
Select("media.*").
|
||||
Joins("JOIN media_search_fts ON media_search_fts.media_id = media.id").
|
||||
Where("media.deleted_at IS NULL").
|
||||
Where("media_search_fts MATCH ?", ftsQuery)
|
||||
q = applyQualifiedMediaQueryFilter(q, filter)
|
||||
err := q.Order("bm25(media_search_fts), media.created_at DESC").Limit(limit).Find(&items).Error
|
||||
if err != nil {
|
||||
return nil, false
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
return nil, 0, false
|
||||
}
|
||||
return items, true
|
||||
if total == 0 {
|
||||
return items, 0, true
|
||||
}
|
||||
err := q.Select("media.*").Order("bm25(media_search_fts), media.created_at DESC").Offset(offset).Limit(limit).Find(&items).Error
|
||||
if err != nil {
|
||||
return nil, 0, false
|
||||
}
|
||||
return items, total, true
|
||||
}
|
||||
|
||||
func (r *MediaRepository) searchFilteredLIKE(ctx context.Context, query string, limit int, filter MediaQueryFilter) ([]model.Media, error) {
|
||||
func (r *MediaRepository) searchFilteredLIKE(ctx context.Context, query string, offset, limit int, filter MediaQueryFilter) ([]model.Media, int64, error) {
|
||||
var items []model.Media
|
||||
q := r.db.WithContext(ctx).Model(&model.Media{}).Limit(limit)
|
||||
var total int64
|
||||
q := r.db.WithContext(ctx).Model(&model.Media{})
|
||||
q = applyMediaQueryFilter(q, filter)
|
||||
terms := mediaSearchTerms(query)
|
||||
for _, term := range terms {
|
||||
@@ -510,6 +522,9 @@ func (r *MediaRepository) searchFilteredLIKE(ctx context.Context, query string,
|
||||
like, like, like, like,
|
||||
)
|
||||
}
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if query != "" {
|
||||
prefix := escapeLike(query) + "%"
|
||||
exact := query
|
||||
@@ -520,8 +535,8 @@ func (r *MediaRepository) searchFilteredLIKE(ctx context.Context, query string,
|
||||
} else {
|
||||
q = q.Order("created_at desc")
|
||||
}
|
||||
err := q.Find(&items).Error
|
||||
return items, err
|
||||
err := q.Offset(offset).Limit(limit).Find(&items).Error
|
||||
return items, total, err
|
||||
}
|
||||
|
||||
func applyQualifiedMediaQueryFilter(q *gorm.DB, filter MediaQueryFilter) *gorm.DB {
|
||||
|
||||
@@ -145,6 +145,17 @@ func TestListMediaVisibleIncludesMergedCloudLibraryItems(t *testing.T) {
|
||||
if total != 2 || !slices.Equal(mediaTitles(items), []string{"云盘剧", "本地剧"}) {
|
||||
t.Fatalf("profile-limited merged list total=%d items=%#v", total, mediaTitles(items))
|
||||
}
|
||||
|
||||
searchItems, err := svc.SearchMediaVisible(t.Context(), "剧", 20, MediaVisibility{
|
||||
IncludeNSFW: true,
|
||||
AllowedLibraryIDs: []string{local.ID},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := mediaTitles(searchItems); !slices.Equal(got, []string{"云盘剧", "本地剧"}) {
|
||||
t.Fatalf("profile-limited merged search items=%#v, want local+hidden cloud", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStartAllCloudLibraryScansIncludesMergedCloudMounts(t *testing.T) {
|
||||
|
||||
@@ -29,6 +29,9 @@ type MediaVisibility struct {
|
||||
HiddenLibraryIDs []string
|
||||
}
|
||||
|
||||
const maxMediaSearchLimit = 50000
|
||||
const maxMediaSearchPageSize = 2000
|
||||
|
||||
func (v MediaVisibility) Allows(media *model.Media) bool {
|
||||
if media == nil {
|
||||
return false
|
||||
@@ -281,8 +284,8 @@ func (s *MediaService) SearchMedia(ctx context.Context, query string, limit int)
|
||||
func (s *MediaService) SearchMediaVisible(ctx context.Context, query string, limit int, visibility MediaVisibility) ([]model.Media, error) {
|
||||
if limit <= 0 {
|
||||
limit = 50
|
||||
} else if limit > 2000 {
|
||||
limit = 2000
|
||||
} else if limit > maxMediaSearchLimit {
|
||||
limit = maxMediaSearchLimit
|
||||
}
|
||||
visibility = ExpandMediaVisibilityForMergedCloudLibraries(ctx, s.repo, visibility)
|
||||
return s.repo.Media.SearchFiltered(ctx, query, limit, repository.MediaQueryFilter{
|
||||
@@ -292,6 +295,24 @@ func (s *MediaService) SearchMediaVisible(ctx context.Context, query string, lim
|
||||
})
|
||||
}
|
||||
|
||||
func (s *MediaService) SearchMediaVisiblePage(ctx context.Context, query string, page, pageSize int, visibility MediaVisibility) ([]model.Media, int64, error) {
|
||||
if pageSize <= 0 {
|
||||
pageSize = 50
|
||||
}
|
||||
if pageSize > maxMediaSearchPageSize {
|
||||
pageSize = maxMediaSearchPageSize
|
||||
}
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
visibility = ExpandMediaVisibilityForMergedCloudLibraries(ctx, s.repo, visibility)
|
||||
return s.repo.Media.SearchFilteredPage(ctx, query, (page-1)*pageSize, pageSize, repository.MediaQueryFilter{
|
||||
IncludeNSFW: visibility.IncludeNSFW,
|
||||
AllowedLibraryIDs: visibility.AllowedLibraryIDs,
|
||||
HiddenLibraryIDs: visibility.HiddenLibraryIDs,
|
||||
})
|
||||
}
|
||||
|
||||
// GetMedia returns a single media row.
|
||||
func (s *MediaService) GetMedia(ctx context.Context, id string) (*model.Media, error) {
|
||||
return s.repo.Media.FindByID(ctx, id)
|
||||
|
||||
@@ -176,6 +176,61 @@ func TestSearchMediaVisibleHonorsLargePosterWallLimit(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchMediaVisibleCanReturnHugeLibraryResultsWhenRequested(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AutoMigrate(&model.Library{}, &model.Media{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repos := repository.New(db)
|
||||
lib := model.Library{Name: "海量剧集", Path: "/media/huge", Type: "tv", Enabled: true}
|
||||
if err := repos.Library.Create(t.Context(), &lib); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const total = 2505
|
||||
rows := make([]model.Media, total)
|
||||
for i := range rows {
|
||||
rows[i] = model.Media{
|
||||
LibraryID: lib.ID,
|
||||
Title: fmt.Sprintf("海量剧集 %04d", i),
|
||||
Path: fmt.Sprintf("/media/huge/show-%04d.mkv", i),
|
||||
SeasonNum: 1,
|
||||
EpisodeNum: i + 1,
|
||||
}
|
||||
}
|
||||
if err := db.CreateInBatches(&rows, 500).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
items, err := NewMediaService(&config.Config{}, zap.NewNop(), repos).
|
||||
SearchMediaVisible(t.Context(), "海量剧集", total, MediaVisibility{IncludeNSFW: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(items) != total {
|
||||
t.Fatalf("huge search returned %d rows, want %d", len(items), total)
|
||||
}
|
||||
|
||||
firstPage, totalRows, err := NewMediaService(&config.Config{}, zap.NewNop(), repos).
|
||||
SearchMediaVisiblePage(t.Context(), "海量剧集", 1, 2000, MediaVisibility{IncludeNSFW: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if totalRows != total || len(firstPage) != 2000 {
|
||||
t.Fatalf("huge search page 1 len=%d total=%d, want len=2000 total=%d", len(firstPage), totalRows, total)
|
||||
}
|
||||
secondPage, totalRows, err := NewMediaService(&config.Config{}, zap.NewNop(), repos).
|
||||
SearchMediaVisiblePage(t.Context(), "海量剧集", 2, 2000, MediaVisibility{IncludeNSFW: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if totalRows != total || len(secondPage) != total-2000 {
|
||||
t.Fatalf("huge search page 2 len=%d total=%d, want len=%d total=%d", len(secondPage), totalRows, total-2000, total)
|
||||
}
|
||||
}
|
||||
|
||||
func sortedMediaTitles(rows []model.Media) []string {
|
||||
out := make([]string, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
|
||||
Reference in New Issue
Block a user