saveTorrentChangesToDisk when relevant

This commit is contained in:
Ben Sarmiento
2024-01-27 16:32:50 +01:00
parent 582af81ea8
commit b44f2a4b63
6 changed files with 33 additions and 38 deletions

View File

@@ -12,9 +12,7 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool {
if len(unselectedIDs) == torrent.SelectedFiles.Count() && len(unselectedIDs) > 0 {
return true
} else if len(unselectedIDs) > 0 {
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
torrent.DownloadedIDs.Each(func(id string) bool {
info, _ := infoCache.Get(id)
t.saveTorrentChangesToDisk(torrent, func(info *Torrent) {
info.SelectedFiles.IterCb(func(_ string, file *File) {
for _, unselectedID := range unselectedIDs {
if file.ID == unselectedID {
@@ -23,8 +21,6 @@ func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool {
}
}
})
t.writeTorrentToFile(id, info)
return false
})
}
return false

View File

@@ -201,3 +201,18 @@ func (t *TorrentManager) initializeDirectories() {
t.DirectoryMap.Set(directory, cmap.New[*Torrent]())
}
}
func (t *TorrentManager) saveTorrentChangesToDisk(torrent *Torrent, cb func(*Torrent)) {
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
torrent.DownloadedIDs.Union(torrent.InProgressIDs).Each(func(id string) bool {
info, exists := infoCache.Get(id)
if !exists {
return false
}
if cb != nil {
cb(info)
}
t.writeTorrentToFile(id, info)
return false
})
}

View File

@@ -234,7 +234,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
torrent.BrokenLinks = mapset.NewSet[string]()
infoCache.Set(rdTorrent.ID, &torrent)
t.writeTorrentToFile(rdTorrent.ID, &torrent)
t.saveTorrentChangesToDisk(&torrent, nil)
return &torrent
}
@@ -290,18 +290,6 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent {
}
})
// broken links
if mainTorrent.BrokenLinks.Cardinality() > 0 {
mainTorrent.SelectedFiles.IterCb(func(_ string, file *File) {
mainTorrent.BrokenLinks.Each(func(brokenLink string) bool {
if file.Link == brokenLink {
file.Link = ""
}
return file.Link == brokenLink
})
})
}
if existing.Added < toMerge.Added {
mainTorrent.Added = toMerge.Added
} else {

View File

@@ -92,9 +92,7 @@ func (t *TorrentManager) repairAll(torrent *Torrent) {
// save the broken files to the file cache
// broken files are also added when trying to open a file
if torrent.BrokenLinks.Cardinality() > 0 {
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
torrent.DownloadedIDs.Each(func(id string) bool {
info, _ := infoCache.Get(id)
t.saveTorrentChangesToDisk(torrent, func(info *Torrent) {
hasBrokenFiles := false
info.SelectedFiles.IterCb(func(_ string, file *File) {
torrent.BrokenLinks.Each(func(brokenLink string) bool {
@@ -108,9 +106,7 @@ func (t *TorrentManager) repairAll(torrent *Torrent) {
})
if hasBrokenFiles {
info.BrokenLinks = torrent.BrokenLinks
t.writeTorrentToFile(id, info)
}
return false
})
}
@@ -183,6 +179,7 @@ func (t *TorrentManager) repair(torrent *Torrent) {
// handle torrents with incomplete links for selected files
if !t.assignUnassignedLinks(torrent) {
t.log.Debugf("Ending repair process early for torrent %s", t.GetKey(torrent))
return
}
@@ -206,6 +203,9 @@ func (t *TorrentManager) repair(torrent *Torrent) {
ix++
})
torrent.BrokenLinks = mapset.NewSet[string]()
t.saveTorrentChangesToDisk(torrent, func(info *Torrent) {
info.BrokenLinks = mapset.NewSet[string]()
})
t.log.Infof("Successfully repaired torrent %s using repair_method#1", t.GetKey(torrent))
return
}
@@ -294,17 +294,12 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
t.markAsUnfixable(torrent, "rar'ed by RD")
t.markAsUnplayable(torrent, "rar'ed by RD")
}
t.log.Debugf("Ending repair process early for torrent %s", t.GetKey(torrent))
return false
}
// empty the unassigned links as we have assigned them
if torrent.UnassignedLinks.Cardinality() > 0 {
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
torrent.DownloadedIDs.Each(func(id string) bool {
info, _ := infoCache.Get(id)
t.saveTorrentChangesToDisk(torrent, func(info *Torrent) {
info.UnassignedLinks = mapset.NewSet[string]()
t.writeTorrentToFile(id, info)
return false
})
}
@@ -469,12 +464,8 @@ func (t *TorrentManager) markAsUnplayable(torrent *Torrent, reason string) {
func (t *TorrentManager) markAsUnfixable(torrent *Torrent, reason string) {
t.log.Warnf("Marking torrent %s as unfixable - %s", t.GetKey(torrent), reason)
torrent.UnrepairableReason = reason
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
torrent.DownloadedIDs.Each(func(id string) bool {
info, _ := infoCache.Get(id)
info.UnrepairableReason = reason
t.writeTorrentToFile(id, info)
return false
t.saveTorrentChangesToDisk(torrent, func(t *Torrent) {
t.UnrepairableReason = reason
})
}
@@ -562,6 +553,9 @@ func (t *TorrentManager) handleFixers(fixer realdebrid.Torrent) *Torrent {
}
})
torrent.BrokenLinks = mapset.NewSet[string]()
t.saveTorrentChangesToDisk(torrent, func(info *Torrent) {
info.BrokenLinks = mapset.NewSet[string]()
})
t.log.Infof("Successfully repaired torrent %s using repair_method#2", t.GetKey(torrent))
} else {
t.log.Warnf("repair_method#2: Fixer is done but torrent %s is still broken; let's keep the fixer", t.GetKey(torrent))