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