Handle adds and deletes

This commit is contained in:
Ben Sarmiento
2023-12-06 02:02:01 +01:00
parent 4b8fd82acd
commit 0e471ba42d
5 changed files with 41 additions and 28 deletions

View File

@@ -6,20 +6,20 @@ import (
"github.com/debridmediamanager/zurg/internal/torrent" "github.com/debridmediamanager/zurg/internal/torrent"
) )
func HandleDeleteTorrent(directory, torrentName string, t *torrent.TorrentManager) error { func HandleDeleteTorrent(directory, torrentName string, torMgr *torrent.TorrentManager) error {
torrents, ok := t.DirectoryMap.Get(directory) torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok { if !ok {
return fmt.Errorf("cannot find directory %s", directory) return fmt.Errorf("cannot find directory %s", directory)
} }
if !torrents.Has(torrentName) { if !torrents.Has(torrentName) {
return fmt.Errorf("cannot find torrent %s", torrentName) return fmt.Errorf("cannot find torrent %s", torrentName)
} }
t.Delete(torrentName, true) torMgr.Delete(torrentName, true)
return nil return nil
} }
func HandleDeleteFile(directory, torrentName, fileName string, t *torrent.TorrentManager) error { func HandleDeleteFile(directory, torrentName, fileName string, torMgr *torrent.TorrentManager) error {
torrents, ok := t.DirectoryMap.Get(directory) torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok { if !ok {
return fmt.Errorf("cannot find directory %s", directory) return fmt.Errorf("cannot find directory %s", directory)
} }
@@ -32,8 +32,8 @@ func HandleDeleteFile(directory, torrentName, fileName string, t *torrent.Torren
return fmt.Errorf("cannot find file %s", fileName) return fmt.Errorf("cannot find file %s", fileName)
} }
file.Link = "unselect" file.Link = "unselect"
if t.CheckDeletedState(torrent) { if torMgr.CheckDeletedState(torrent) {
t.Delete(torrentName, true) torMgr.Delete(torrentName, true)
} }
// todo: triggeer an update ??? // todo: triggeer an update ???
// t.TriggerHookOnLibraryUpdate(updatedPaths) // t.TriggerHookOnLibraryUpdate(updatedPaths)

View File

@@ -11,10 +11,10 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
func HandleListDirectories(t *torrent.TorrentManager) (*string, error) { func HandleListDirectories(torMgr *torrent.TorrentManager) (*string, error) {
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">"
davDoc += dav.BaseDirectory("", "") davDoc += dav.BaseDirectory("", "")
directories := t.DirectoryMap.Keys() directories := torMgr.DirectoryMap.Keys()
sort.Strings(directories) sort.Strings(directories)
for _, directory := range directories { for _, directory := range directories {
if strings.HasPrefix(directory, "int__") { if strings.HasPrefix(directory, "int__") {
@@ -26,25 +26,33 @@ func HandleListDirectories(t *torrent.TorrentManager) (*string, error) {
return &davDoc, nil return &davDoc, nil
} }
func HandleListTorrents(directory string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) { func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
_, ok := t.DirectoryMap.Get(directory) torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok { if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory) return nil, fmt.Errorf("cannot find directory %s", directory)
} }
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">"
davDoc += dav.Directory("", "") davDoc += dav.Directory("", "")
directories := t.DirectoryMap.Keys() var allTorrents []*torrent.Torrent
sort.Strings(directories) torrents.IterCb(func(_ string, tor *torrent.Torrent) {
for _, directory := range directories { if tor.AllInProgress() {
davDoc += dav.Directory(directory, "") return
}
allTorrents = append(allTorrents, tor)
})
sort.Slice(allTorrents, func(i, j int) bool {
return allTorrents[i].AccessKey < allTorrents[j].AccessKey
})
for _, tor := range allTorrents {
davDoc += dav.Directory(tor.AccessKey, tor.LatestAdded)
} }
davDoc += "</d:multistatus>" davDoc += "</d:multistatus>"
return &davDoc, nil return &davDoc, nil
} }
func HandleListFiles(directory, torrentName string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) { func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
torrents, ok := t.DirectoryMap.Get(directory) torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok { if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory) return nil, fmt.Errorf("cannot find directory %s", directory)
} }

View File

@@ -26,8 +26,8 @@ func HandleListDirectories(torMgr *torrent.TorrentManager) (*string, error) {
return &htmlDoc, nil return &htmlDoc, nil
} }
func HandleListTorrents(directory string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) { func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
torrents, ok := t.DirectoryMap.Get(directory) torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok { if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory) return nil, fmt.Errorf("cannot find directory %s", directory)
} }
@@ -49,8 +49,8 @@ func HandleListTorrents(directory string, t *torrent.TorrentManager, log *zap.Su
return &htmlDoc, nil return &htmlDoc, nil
} }
func HandleListFiles(directory, torrentName string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) { func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
torrents, ok := t.DirectoryMap.Get(directory) torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok { if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory) return nil, fmt.Errorf("cannot find directory %s", directory)
} }

View File

@@ -131,7 +131,7 @@ func (t *TorrentManager) RefreshTorrents() {
t.log.Infof("Fetched info for %d torrents", len(instances)) t.log.Infof("Fetched info for %d torrents", len(instances))
freshKeys := set.NewStringSet() freshKeys := set.NewStringSet()
oldTorrents, _ := t.DirectoryMap.Get(INT_ALL) allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
noInfoCount := 0 noInfoCount := 0
for info := range infoChan { for info := range infoChan {
if info == nil { if info == nil {
@@ -139,14 +139,14 @@ func (t *TorrentManager) RefreshTorrents() {
continue continue
} }
freshKeys.Add(info.AccessKey) freshKeys.Add(info.AccessKey)
if torrent, exists := oldTorrents.Get(info.AccessKey); !exists { if torrent, exists := allTorrents.Get(info.AccessKey); !exists {
oldTorrents.Set(info.AccessKey, info) allTorrents.Set(info.AccessKey, info)
} else if !strset.Difference(info.DownloadedIDs, torrent.DownloadedIDs).IsEmpty() { } else if !strset.Difference(info.DownloadedIDs, torrent.DownloadedIDs).IsEmpty() {
mainTorrent := t.mergeToMain(torrent, info) mainTorrent := t.mergeToMain(torrent, info)
oldTorrents.Set(info.AccessKey, &mainTorrent) allTorrents.Set(info.AccessKey, &mainTorrent)
} }
} }
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", allTorrents.Count(), noInfoCount)
// removed // removed
strset.Difference(t.accessKeySet, freshKeys).Each(func(accessKey string) bool { strset.Difference(t.accessKeySet, freshKeys).Each(func(accessKey string) bool {
@@ -156,6 +156,11 @@ func (t *TorrentManager) RefreshTorrents() {
// new // new
strset.Difference(freshKeys, t.accessKeySet).Each(func(accessKey string) bool { strset.Difference(freshKeys, t.accessKeySet).Each(func(accessKey string) bool {
t.accessKeySet.Add(accessKey) t.accessKeySet.Add(accessKey)
tor, _ := allTorrents.Get(accessKey)
t.AssignedDirectoryCb(tor, func(directory string) {
torrents, _ := t.DirectoryMap.Get(directory)
torrents.Set(accessKey, tor)
})
return true return true
}) })

View File

@@ -10,9 +10,9 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
func HandleHeadRequest(directory, torrentName, fileName string, w http.ResponseWriter, req *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) { func HandleHeadRequest(directory, torrentName, fileName string, w http.ResponseWriter, req *http.Request, torMgr *torrent.TorrentManager, log *zap.SugaredLogger) {
torrents, ok := t.DirectoryMap.Get(directory) torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok { if !ok {
log.Warnf("Cannot find directory %s", directory) log.Warnf("Cannot find directory %s", directory)
http.Error(w, "File not found", http.StatusNotFound) http.Error(w, "File not found", http.StatusNotFound)