Files
zurg/internal/torrent/delete.go
2024-05-27 06:25:55 +02:00

47 lines
1.3 KiB
Go

package torrent
import (
cmap "github.com/orcaman/concurrent-map/v2"
)
// CheckDeletedStatus checks if all files in a torrent are marked as deleted, if so, it returns true
func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool {
var deletedIDs []int
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if file.State.Is("deleted_file") {
deletedIDs = append(deletedIDs, file.ID)
}
})
if len(deletedIDs) == torrent.SelectedFiles.Count() && len(deletedIDs) > 0 {
return true
} else if len(deletedIDs) > 0 {
t.writeTorrentToFile(torrent)
}
return false
}
func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) {
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
var hash string
if torrent, ok := allTorrents.Get(accessKey); ok {
hash = torrent.Hash
if deleteInRD {
torrent.DownloadedIDs.Each(func(torrentID string) bool {
t.log.Debugf("Deleting torrent %s (id=%s) in RD", accessKey, torrentID)
t.api.DeleteTorrent(torrentID)
t.deleteInfoFile(torrentID)
return false
})
}
}
// t.log.Infof("Removing torrent %s from zurg database (not real-debrid)", accessKey)
t.DirectoryMap.IterCb(func(directory string, torrents cmap.ConcurrentMap[string, *Torrent]) {
torrents.Remove(accessKey)
})
allTorrents.Remove(accessKey)
if hash != "" {
t.deleteTorrentFile(hash)
}
}