From 628e3d634537f1dfe6591bfc02f5dc3c15c2b7a0 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Thu, 11 Jan 2024 06:55:31 +0100 Subject: [PATCH] do not overwrite info --- internal/torrent/delete.go | 16 ++++++++++++++-- internal/torrent/refresh.go | 9 +++++---- internal/torrent/repair.go | 25 +++++++++++++++++++++---- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/internal/torrent/delete.go b/internal/torrent/delete.go index 30aa4dc..e532c6d 100644 --- a/internal/torrent/delete.go +++ b/internal/torrent/delete.go @@ -14,8 +14,20 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool { } else if len(unselectedIDs) > 0 { infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) torrent.DownloadedIDs.Each(func(id string) bool { - infoCache.Set(id, torrent) - t.writeTorrentToFile(id, torrent, false) + info, _ := infoCache.Get(id) + info.SelectedFiles.IterCb(func(_ string, file *File) { + found := false + for _, unselectedID := range unselectedIDs { + if file.ID == unselectedID { + found = true + break + } + } + if found { + file.Link = "unselect" + } + }) + t.writeTorrentToFile(id, info, false) return false }) } diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 7dd7b5f..51b2a0c 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -208,9 +208,9 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent { // the link can have the following values // 1. https://*** - the file is available - // 2. repair - the file is available but we need to repair it - // 3. repairing - the file is being repaired - // 4. unselect - the file is deleted + // 2. repair - the link is there but we need to repair it + // 3. unselect - the file is deleted + // 4. empty - the file is not available mainTorrent.SelectedFiles = existing.SelectedFiles toMerge.SelectedFiles.IterCb(func(filepath string, fileToMerge *File) { // see if it already exists in the main torrent @@ -219,7 +219,8 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent { mainTorrent.SelectedFiles.Set(filepath, fileToMerge) } else if originalFile.Link != "unselect" { // if it exists, compare the LatestAdded property and the link - if existing.LatestAdded < toMerge.LatestAdded && strings.HasPrefix(fileToMerge.Link, "http") { + if existing.LatestAdded < toMerge.LatestAdded { + // && strings.HasPrefix(fileToMerge.Link, "http") // if torrentToMerge is more recent and its file has a link, update the main torrent's file mainTorrent.SelectedFiles.Set(filepath, fileToMerge) } diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 0f12d51..bffac4a 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -102,10 +102,28 @@ func (t *TorrentManager) repairAll() { func (t *TorrentManager) Repair(torrent *Torrent) { // save the broken files to the file cache + var brokenFileIDs []int + torrent.SelectedFiles.IterCb(func(_ string, file *File) { + if file.Link == "repair" { + brokenFileIDs = append(brokenFileIDs, file.ID) + } + }) infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) torrent.DownloadedIDs.Each(func(id string) bool { - infoCache.Set(id, torrent) - t.writeTorrentToFile(id, torrent, false) + info, _ := infoCache.Get(id) + info.SelectedFiles.IterCb(func(_ string, file *File) { + found := false + for _, brokenFileID := range brokenFileIDs { + if file.ID == brokenFileID { + found = true + break + } + } + if found { + file.Link = "unselect" + } + }) + t.writeTorrentToFile(id, info, false) return false }) _ = t.workerPool.Submit(func() { @@ -205,7 +223,6 @@ func (t *TorrentManager) repair(torrent *Torrent) { torrent.SelectedFiles.IterCb(func(_ string, file *File) { if file.Link == "repair" || file.Link == "" { brokenFiles = append(brokenFiles, *file) - file.Link = "repairing" } }) t.log.Debugf("During repair, zurg found %d broken files for torrent %s", len(brokenFiles), t.GetKey(torrent)) @@ -370,7 +387,7 @@ func (t *TorrentManager) markAsUnfixable(torrent *Torrent) { torrent.DownloadedIDs.Each(func(id string) bool { info, _ := infoCache.Get(id) info.Unfixable = true - t.writeTorrentToFile(id, torrent, false) + t.writeTorrentToFile(id, info, false) return false }) }