From d5dd9426ed485814bf22c3e5f6d79e6b7ae64d2e Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Mon, 20 May 2024 22:57:20 +0200 Subject: [PATCH] Adjust file reads and writes --- internal/torrent/delete.go | 10 ++++++++-- internal/torrent/manager.go | 2 +- internal/torrent/refresh.go | 19 +++++++++++++------ internal/torrent/repair.go | 24 ------------------------ pkg/realdebrid/api.go | 1 - 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/internal/torrent/delete.go b/internal/torrent/delete.go index ff34bdf..aebe70f 100644 --- a/internal/torrent/delete.go +++ b/internal/torrent/delete.go @@ -2,6 +2,7 @@ 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) { @@ -19,8 +20,10 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool { func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) { allTorrents, _ := t.DirectoryMap.Get(INT_ALL) - if deleteInRD { - if torrent, ok := allTorrents.Get(accessKey); ok { + var hash string + if torrent, ok := allTorrents.Get(accessKey); ok { + hash = torrent.Hash + if deleteInRD { for torrentID := range torrent.Components { t.log.Debugf("Deleting torrent %s (id=%s) in RD", accessKey, torrentID) t.api.DeleteTorrent(torrentID) @@ -33,4 +36,7 @@ func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) { torrents.Remove(accessKey) }) allTorrents.Remove(accessKey) + if hash != "" { + t.deleteTorrentFile(hash) + } } diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 8d63537..3f00462 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -70,7 +70,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w RemountTrigger: make(chan struct{}, 1), latestState: &LibraryState{}, - allAccessKeys: mapset.NewSet[string](), + allAccessKeys: mapset.NewSet[string](), // this is for tracking what is deleted } t.fixers = t.readFixersFromFile() diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 447940f..58df22b 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -79,8 +79,6 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string { }) } } - t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount) - // removed torrents t.allAccessKeys.Difference(freshAccessKeys).Each(func(accessKey string) bool { t.Delete(accessKey, false) @@ -89,6 +87,8 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string { }) t.allAccessKeys.Append(freshAccessKeys.ToSlice()...) + t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount) + if t.Config.EnableRepair() { if isInitialRun { t.removeExpiredFixers(instances) @@ -134,6 +134,13 @@ func (t *TorrentManager) StartRefreshJob() { // getMoreInfo gets original name, size and files for a torrent func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { + diskTor := t.readTorrentFromFile(rdTorrent.Hash) + if diskTor != nil { + if diskInfo, ok := diskTor.Components[rdTorrent.ID]; ok && diskInfo.Progress == 100 { + return diskTor + } + } + info := t.readInfoFromFile(rdTorrent.ID) if info == nil { var err error @@ -142,6 +149,9 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { t.log.Warnf("Cannot get info for id=%s: %v", rdTorrent.ID, err) return nil } + if info.Progress == 100 { + t.writeInfoToFile(info) + } } torrent := Torrent{ @@ -150,6 +160,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { Added: info.Added, Hash: info.Hash, } + // SelectedFiles is a subset of Files with only the selected ones // it also has a Link field, which can be empty // if it is empty, it means the file is no longer available @@ -192,10 +203,6 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent { } torrent.Components = map[string]*realdebrid.TorrentInfo{rdTorrent.ID: info} - if info.Progress == 100 { - t.writeInfoToFile(info) - } - return &torrent } diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index bdea00a..a1905a3 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -544,27 +544,3 @@ func (t *TorrentManager) isStillBroken(info *realdebrid.TorrentInfo, brokenFiles } return false } - -func getSelectedFiles(info *realdebrid.TorrentInfo) []*File { - var selectedFiles []*File - // if some Links are empty, we need to repair it - for _, file := range info.Files { - if file.Selected == 0 { - continue - } - selectedFiles = append(selectedFiles, &File{ - File: file, - Ended: info.Ended, - Link: "", // no link yet - IsBroken: true, - }) - } - if len(selectedFiles) == len(info.Links) { - // all links are still intact! good! - for i, file := range selectedFiles { - file.Link = info.Links[i] - file.IsBroken = false - } - } - return selectedFiles -} diff --git a/pkg/realdebrid/api.go b/pkg/realdebrid/api.go index 9b61701..8b5ce18 100644 --- a/pkg/realdebrid/api.go +++ b/pkg/realdebrid/api.go @@ -166,7 +166,6 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) { totalCount := result.totalCount if onlyOne { - rd.log.Debugf("Returning early the %d torrents", len(allTorrents)) return allTorrents, totalCount, nil }