This commit is contained in:
Ben Sarmiento
2023-12-02 20:37:40 +01:00
parent e4bdecd979
commit 3df201cc20
4 changed files with 42 additions and 13 deletions

View File

@@ -117,11 +117,11 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p
return t
}
func (t *TorrentManager) RefreshTorrents() {
func (t *TorrentManager) RefreshTorrents() []string {
instances, _, err := t.Api.GetTorrents(0)
if err != nil {
t.log.Warnf("Cannot get torrents: %v\n", err)
return
return nil
}
infoChan := make(chan *Torrent, len(instances))
var wg sync.WaitGroup
@@ -155,18 +155,21 @@ func (t *TorrentManager) RefreshTorrents() {
}
t.log.Infof("Compiled into %d torrents, %d were missing info", oldTorrents.Count(), noInfoCount)
var updatedPaths []string
somethingChanged := false
// removed
strset.Difference(t.accessKeySet, freshKeys).Each(func(accessKey string) bool {
somethingChanged = true
t.Delete(accessKey, false, false)
torrentPaths := t.Delete(accessKey, false, false)
updatedPaths = append(updatedPaths, torrentPaths...)
return true
})
// new
strset.Difference(freshKeys, t.accessKeySet).Each(func(accessKey string) bool {
somethingChanged = true
torrent, _ := oldTorrents.Get(accessKey)
t.UpdateTorrentResponseCache(torrent)
torrentPaths := t.UpdateTorrentResponseCache(torrent)
updatedPaths = append(updatedPaths, torrentPaths...)
t.accessKeySet.Add(accessKey)
return true
})
@@ -177,10 +180,7 @@ func (t *TorrentManager) RefreshTorrents() {
t.SetNewLatestState(t.getCurrentState())
// todo: work on hook
// _ = t.workerPool.Submit(func() {
// OnLibraryUpdateHook(updatedPaths, t.Config, t.log)
// })
return updatedPaths
}
// getMoreInfo gets original name, size and files for a torrent
@@ -311,6 +311,13 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent {
return mainTorrent
}
func (t *TorrentManager) TriggerHookOnLibraryUpdate(updatedPaths []string) {
t.log.Debugf("Triggering hook on_library_update for %d path(s)", len(updatedPaths))
_ = t.workerPool.Submit(func() {
OnLibraryUpdateHook(updatedPaths, t.Config, t.log)
})
}
// proxy
func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.Download {
t.log.Debugf("Unrestricting %s", link)
@@ -400,9 +407,13 @@ func (t *TorrentManager) startRefreshJob() {
}
t.log.Infof("Detected changes! Refreshing %d torrents", checksum.TotalCount)
t.RefreshTorrents()
updatedPaths := t.RefreshTorrents()
t.log.Info("Finished refreshing torrents")
if updatedPaths != nil {
t.TriggerHookOnLibraryUpdate(updatedPaths)
}
if t.Config.EnableRepair() {
t.RepairAll()
} else {
@@ -597,7 +608,7 @@ func (t *TorrentManager) CheckDeletedState(torrent *Torrent) bool {
return false
}
func (t *TorrentManager) Delete(accessKey string, deleteInRD bool, updateDirectoryResponses bool) {
func (t *TorrentManager) Delete(accessKey string, deleteInRD bool, updateDirectoryResponses bool) []string {
if deleteInRD {
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
@@ -612,16 +623,19 @@ func (t *TorrentManager) Delete(accessKey string, deleteInRD bool, updateDirecto
}
}
t.log.Infof("Removing torrent %s from zurg database", accessKey)
var updatedPaths []string
t.DirectoryMap.IterCb(func(directory string, torrents cmap.ConcurrentMap[string, *Torrent]) {
if ok := torrents.Has(accessKey); ok {
torrents.Remove(accessKey)
pathKey := fmt.Sprintf("%s/%s", directory, accessKey)
updatedPaths = append(updatedPaths, pathKey)
t.ResponseCache.Del(pathKey)
}
})
if updateDirectoryResponses {
t.UpdateDirectoryResponsesCache()
}
return updatedPaths
}
func (t *TorrentManager) repair(torrent *Torrent) {
@@ -846,21 +860,25 @@ func (t *TorrentManager) Repair(torrent *Torrent) {
t.repair(torrent)
t.log.Info("Finished repairing torrent %s", torrent.AccessKey)
})
t.UpdateTorrentResponseCache(torrent)
updatedPaths := t.UpdateTorrentResponseCache(torrent)
t.TriggerHookOnLibraryUpdate(updatedPaths)
}
func (t *TorrentManager) UpdateTorrentResponseCache(torrent *Torrent) {
func (t *TorrentManager) UpdateTorrentResponseCache(torrent *Torrent) []string {
updatedPaths := []string{}
dav, html := t.buildTorrentResponses(torrent)
t.AssignedDirectoryCb(torrent, func(directory string) {
torrents, _ := t.DirectoryMap.Get(directory)
torrents.Set(torrent.AccessKey, torrent)
pathKey := fmt.Sprintf("%s/%s", directory, torrent.AccessKey)
updatedPaths = append(updatedPaths, pathKey)
// torrent responses
newHtml := strings.ReplaceAll(html, "$dir", directory)
t.ResponseCache.Set(pathKey+".html", newHtml, 1)
newDav := strings.ReplaceAll(dav, "$dir", directory)
t.ResponseCache.Set(pathKey+".dav", newDav, 1)
})
return updatedPaths
}
func (t *TorrentManager) UpdateDirectoryResponsesCache() {