From 130433203fceec7bad4d163b8c75c0d06cdd31be Mon Sep 17 00:00:00 2001 From: Mukhtar Akere Date: Wed, 30 Apr 2025 00:52:15 +0100 Subject: [PATCH] fix propfind cache --- pkg/debrid/debrid/refresh.go | 12 ++++++------ pkg/debrid/debrid/repair.go | 3 ++- pkg/debrid/debrid/xml.go | 22 ++++++++++------------ pkg/qbit/torrent.go | 6 +++--- pkg/webdav/handler.go | 3 +-- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/pkg/debrid/debrid/refresh.go b/pkg/debrid/debrid/refresh.go index b3eef8d..21a25c0 100644 --- a/pkg/debrid/debrid/refresh.go +++ b/pkg/debrid/debrid/refresh.go @@ -36,11 +36,11 @@ func (c *Cache) RefreshListings(refreshRclone bool) { return } // Copy the torrents to a string|time map - torrentsTime := make(map[string]time.Time, c.torrents.Size()) - torrents := make([]string, 0, c.torrents.Size()) - c.torrentsNames.Range(func(key string, value *CachedTorrent) bool { - torrentsTime[key] = value.AddedOn - torrents = append(torrents, key) + torrentsTime := make(map[string]time.Time, c.torrentsNames.Size()) + torrents := make([]string, 0, c.torrentsNames.Size()) + c.torrentsNames.Range(func(name string, value *CachedTorrent) bool { + torrentsTime[name] = value.AddedOn + torrents = append(torrents, name) return true }) @@ -59,7 +59,7 @@ func (c *Cache) RefreshListings(refreshRclone bool) { } // Atomic store of the complete ready-to-use slice c.listings.Store(files) - if err := c.RefreshParentXml(); err != nil { + if err := c.refreshParentXml(); err != nil { c.logger.Debug().Err(err).Msg("Failed to refresh XML") } diff --git a/pkg/debrid/debrid/repair.go b/pkg/debrid/debrid/repair.go index 569f11d..5aa0b71 100644 --- a/pkg/debrid/debrid/repair.go +++ b/pkg/debrid/debrid/repair.go @@ -201,9 +201,10 @@ func (c *Cache) reInsertTorrent(ct *CachedTorrent) (*CachedTorrent, error) { AddedOn: addedOn, IsComplete: len(newTorrent.Files) > 0, } - // We can safely delete the old torrent here c.setTorrent(ct) + c.RefreshListings(true) + // We can safely delete the old torrent here if oldID != "" { if err := c.DeleteTorrent(oldID); err != nil { return ct, fmt.Errorf("failed to delete old torrent: %w", err) diff --git a/pkg/debrid/debrid/xml.go b/pkg/debrid/debrid/xml.go index 88cc955..12b63d1 100644 --- a/pkg/debrid/debrid/xml.go +++ b/pkg/debrid/debrid/xml.go @@ -2,14 +2,12 @@ package debrid import ( "fmt" - "net/http" - "os" - "path" - "path/filepath" - "time" - "github.com/beevik/etree" "github.com/sirrobot01/decypharr/internal/request" + "net/http" + "os" + path "path/filepath" + "time" ) // resetPropfindResponse resets the propfind response cache for the specified parent directories. @@ -38,19 +36,19 @@ func (c *Cache) resetPropfindResponse() error { return nil } -func (c *Cache) RefreshParentXml() error { +func (c *Cache) refreshParentXml() error { parents := []string{"__all__", "torrents"} torrents := c.GetListing() clientName := c.client.GetName() for _, parent := range parents { - if err := c.refreshParentXml(torrents, clientName, parent); err != nil { + if err := c.refreshFolderXml(torrents, clientName, parent); err != nil { return fmt.Errorf("failed to refresh XML for %s: %v", parent, err) } } return nil } -func (c *Cache) refreshParentXml(torrents []os.FileInfo, clientName, parent string) error { +func (c *Cache) refreshFolderXml(torrents []os.FileInfo, clientName, parent string) error { // Define the WebDAV namespace davNS := "DAV:" @@ -65,15 +63,15 @@ func (c *Cache) refreshParentXml(torrents []os.FileInfo, clientName, parent stri currentTime := time.Now().UTC().Format(http.TimeFormat) // Add the parent directory - baseUrl := path.Clean(path.Join("webdav", clientName, parent)) - parentPath := path.Join(baseUrl) + baseUrl := path.Clean(fmt.Sprintf("/webdav/%s/%s", clientName, parent)) + parentPath := fmt.Sprintf("%s/", baseUrl) addDirectoryResponse(multistatus, parentPath, parent, currentTime) // Add torrents to the XML for _, torrent := range torrents { name := torrent.Name() // Note the path structure change - parent first, then torrent name - torrentPath := filepath.Join("webdav", + torrentPath := fmt.Sprintf("/webdav/%s/%s/%s/", clientName, parent, name, diff --git a/pkg/qbit/torrent.go b/pkg/qbit/torrent.go index 0767acf..844311a 100644 --- a/pkg/qbit/torrent.go +++ b/pkg/qbit/torrent.go @@ -115,12 +115,12 @@ func (q *QBit) ProcessFiles(torrent *Torrent, debridTorrent *debrid.Torrent, arr q.MarkAsFailed(torrent) return } - timer := time.Now() + rclonePath := filepath.Join(debridTorrent.MountPath, cache.GetTorrentFolder(debridTorrent)) // /mnt/remote/realdebrid/MyTVShow torrentFolderNoExt := utils.RemoveExtension(debridTorrent.Name) - + timer := time.Now() torrentSymlinkPath, err = q.createSymlinksWebdav(debridTorrent, rclonePath, torrentFolderNoExt) // /mnt/symlinks/{category}/MyTVShow/ - q.logger.Debug().Msgf("Torrent adding process completed in %s", time.Since(timer)) + q.logger.Debug().Msgf("Adding %s took %s", debridTorrent.Name, time.Since(timer)) } else { // User is using either zurg or debrid webdav diff --git a/pkg/webdav/handler.go b/pkg/webdav/handler.go index c1492a9..6ddd37e 100644 --- a/pkg/webdav/handler.go +++ b/pkg/webdav/handler.go @@ -267,7 +267,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { ttl := 1 * time.Minute if h.isParentPath(r.URL.Path) { // __all__ or torrents folder - // Manually build the xml ttl = 30 * time.Second } @@ -345,7 +344,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if file, ok := fRaw.(*File); ok { link, err := file.getDownloadLink() if err != nil { - h.logger.Error(). + h.logger.Trace(). Err(err). Str("path", r.URL.Path). Msg("Could not fetch download link")