Fix re-insertion

This commit is contained in:
Mukhtar Akere
2025-03-31 08:47:27 +01:00
parent 5792305a66
commit 8bf164451c
2 changed files with 14 additions and 7 deletions

View File

@@ -626,10 +626,7 @@ func (c *Cache) DeleteTorrent(id string) error {
c.logger.Info().Msgf("Deleting torrent %s", id)
if t, ok := c.torrents.Load(id); ok {
err := c.client.DeleteTorrent(id)
if err != nil {
return err
}
_ = c.client.DeleteTorrent(id) // SKip error handling, we don't care if it fails
c.torrents.Delete(id)
c.torrentsNames.Delete(c.GetTorrentFolder(t.Torrent))
c.removeFromDB(id)

View File

@@ -131,6 +131,8 @@ func (c *Cache) ReInsertTorrent(torrent *types.Torrent) error {
oldID := torrent.Id
defer c.repairsInProgress.Delete(oldID)
// Submit the magnet to the debrid service
torrent.Id = ""
var err error
@@ -143,8 +145,14 @@ func (c *Cache) ReInsertTorrent(torrent *types.Torrent) error {
if torrent == nil || torrent.Id == "" {
return fmt.Errorf("failed to submit magnet: empty torrent")
}
torrent.DownloadUncached = false // Set to false, avoid re-downloading
torrent, err = c.client.CheckStatus(torrent, true)
if err != nil {
if err != nil && torrent != nil {
// Torrent is likely in progress
// Delete the old and new torrent
_ = c.DeleteTorrent(oldID)
_ = c.DeleteTorrent(torrent.Id)
return fmt.Errorf("failed to check status: %w", err)
}
@@ -152,6 +160,10 @@ func (c *Cache) ReInsertTorrent(torrent *types.Torrent) error {
return fmt.Errorf("failed to delete old torrent: %w", err)
}
if torrent == nil {
return fmt.Errorf("failed to check status: empty torrent")
}
// Update the torrent in the cache
addedOn, err := time.Parse(time.RFC3339, torrent.Added)
if err != nil {
@@ -164,7 +176,5 @@ func (c *Cache) ReInsertTorrent(torrent *types.Torrent) error {
}
c.setTorrent(ct)
c.refreshListings()
c.repairsInProgress.Delete(oldID)
return nil
}