@@ -2243,10 +2243,16 @@ func (s *Service) AnalyzeTorrentForSearchAsync(ctx context.Context, instanceID i
22432243 return nil , fmt .Errorf ("%w: instance %d not found" , ErrInvalidRequest , instanceID )
22442244 }
22452245
2246- sourceTorrent , err := s .getTorrentByHash (ctx , instanceID , hash )
2246+ torrents , err := s .syncManager .GetTorrents (ctx , instanceID , qbt.TorrentFilterOptions {
2247+ Hashes : []string {hash },
2248+ })
22472249 if err != nil {
2248- return nil , err
2250+ return nil , fmt .Errorf ("load torrents: %w" , err )
2251+ }
2252+ if len (torrents ) == 0 {
2253+ return nil , fmt .Errorf ("%w: torrent %s not found in instance %d" , ErrTorrentNotFound , hash , instanceID )
22492254 }
2255+ sourceTorrent := & torrents [0 ]
22502256 if sourceTorrent .Progress < 1.0 {
22512257 return nil , fmt .Errorf ("%w: torrent %s is not fully downloaded (progress %.2f)" , ErrTorrentNotComplete , sourceTorrent .Name , sourceTorrent .Progress )
22522258 }
@@ -2631,10 +2637,16 @@ func (s *Service) SearchTorrentMatches(ctx context.Context, instanceID int, hash
26312637 return nil , fmt .Errorf ("%w: instance %d not found" , ErrInvalidRequest , instanceID )
26322638 }
26332639
2634- sourceTorrent , err := s .getTorrentByHash (ctx , instanceID , hash )
2640+ torrents , err := s .syncManager .GetTorrents (ctx , instanceID , qbt.TorrentFilterOptions {
2641+ Hashes : []string {hash },
2642+ })
26352643 if err != nil {
2636- return nil , err
2644+ return nil , fmt . Errorf ( "load torrents: %w" , err )
26372645 }
2646+ if len (torrents ) == 0 {
2647+ return nil , fmt .Errorf ("%w: torrent %s not found in instance %d" , ErrTorrentNotFound , hash , instanceID )
2648+ }
2649+ sourceTorrent := & torrents [0 ]
26382650 if sourceTorrent .Progress < 1.0 {
26392651 return nil , fmt .Errorf ("%w: torrent %s is not fully downloaded (progress %.2f)" , ErrTorrentNotComplete , sourceTorrent .Name , sourceTorrent .Progress )
26402652 }
@@ -3159,9 +3171,15 @@ func (s *Service) ApplyTorrentSearchResults(ctx context.Context, instanceID int,
31593171 return nil , fmt .Errorf ("%w: no selections provided" , ErrInvalidRequest )
31603172 }
31613173
3162- if _ , err := s .getTorrentByHash (ctx , instanceID , hash ); err != nil {
3174+ torrents , err := s .syncManager .GetTorrents (ctx , instanceID , qbt.TorrentFilterOptions {
3175+ Hashes : []string {hash },
3176+ })
3177+ if err != nil {
31633178 return nil , err
31643179 }
3180+ if len (torrents ) == 0 {
3181+ return nil , fmt .Errorf ("%w: torrent %s not found in instance %d" , ErrTorrentNotFound , hash , instanceID )
3182+ }
31653183
31663184 cachedSelections := s .getCachedSearchResults (instanceID , hash )
31673185 if len (cachedSelections ) == 0 {
@@ -3360,32 +3378,6 @@ func asyncFilteringCacheKey(instanceID int, hash string) string {
33603378 return fmt .Sprintf ("async:%d:%s" , instanceID , cleanHash )
33613379}
33623380
3363- // getTorrentByHash retrieves a torrent by matching any known hash variant.
3364- func (s * Service ) getTorrentByHash (ctx context.Context , instanceID int , hash string ) (* qbt.Torrent , error ) {
3365- torrents , err := s .syncManager .GetTorrents (ctx , instanceID , qbt.TorrentFilterOptions {Filter : qbt .TorrentFilterAll })
3366- if err != nil {
3367- return nil , fmt .Errorf ("load torrents: %w" , err )
3368- }
3369-
3370- needle := stringutils .DefaultNormalizer .Normalize (hash )
3371- for _ , torrent := range torrents {
3372- candidates := []string {
3373- stringutils .DefaultNormalizer .Normalize (torrent .Hash ),
3374- stringutils .DefaultNormalizer .Normalize (torrent .InfohashV1 ),
3375- stringutils .DefaultNormalizer .Normalize (torrent .InfohashV2 ),
3376- }
3377-
3378- for _ , candidate := range candidates {
3379- if candidate != "" && candidate == needle {
3380- t := torrent
3381- return & t , nil
3382- }
3383- }
3384- }
3385-
3386- return nil , fmt .Errorf ("%w: torrent %s not found in instance %d" , ErrTorrentNotFound , hash , instanceID )
3387- }
3388-
33893381func (s * Service ) searchRunLoop (ctx context.Context , state * searchRunState ) {
33903382 defer func () {
33913383 canceled := ctx .Err () == context .Canceled
@@ -4374,10 +4366,16 @@ func (s *Service) filterIndexersByExistingContent(ctx context.Context, instanceI
43744366 Msg ("[CROSSSEED-FILTER] Starting indexer content filtering" )
43754367
43764368 // Get the source torrent being searched for
4377- sourceTorrent , err := s .getTorrentByHash (ctx , instanceID , hash )
4369+ torrents , err := s .syncManager .GetTorrents (ctx , instanceID , qbt.TorrentFilterOptions {
4370+ Hashes : []string {hash },
4371+ })
43784372 if err != nil {
43794373 return indexerIDs , nil , nil , err
43804374 }
4375+ if len (torrents ) == 0 {
4376+ return indexerIDs , nil , nil , fmt .Errorf ("%w: torrent %s not found in instance %d" , ErrTorrentNotFound , hash , instanceID )
4377+ }
4378+ sourceTorrent := & torrents [0 ]
43814379
43824380 log .Debug ().
43834381 Str ("sourceTorrentName" , sourceTorrent .Name ).
0 commit comments