From c520b5572f09661fe087e66e5603ee16535485f9 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sat, 18 Nov 2023 17:16:09 +0100 Subject: [PATCH] Fix filenames on webdav issue --- internal/dav/listing.go | 27 +++++++++------------------ internal/http/listing.go | 18 +++++++----------- internal/torrent/manager.go | 8 +------- 3 files changed, 17 insertions(+), 36 deletions(-) diff --git a/internal/dav/listing.go b/internal/dav/listing.go index 0c8ffb4..76601e9 100644 --- a/internal/dav/listing.go +++ b/internal/dav/listing.go @@ -4,7 +4,6 @@ import ( "encoding/xml" "fmt" "net/http" - "net/url" "path" "path/filepath" "sort" @@ -79,12 +78,9 @@ func handleListOfTorrents(requestPath string, t *torrent.TorrentManager) ([]byte } var responses []dav.Response // initial response is the directory itself - responses = append(responses, dav.Directory(basePath)) + responses = append(responses, dav.Directory(basePath+"/")) - var accessKeys []string - torrents.IterCb(func(accessKey string, _ *torrent.Torrent) { - accessKeys = append(accessKeys, accessKey) - }) + accessKeys := torrents.Keys() sort.Strings(accessKeys) for _, accessKey := range accessKeys { responses = append(responses, dav.Directory(filepath.Join(basePath, accessKey))) @@ -111,23 +107,18 @@ func handleSingleTorrent(requestPath string, t *torrent.TorrentManager) ([]byte, var responses []dav.Response // initial response is the directory itself - responses = append(responses, dav.Directory(requestPath)) - - tor.SelectedFiles.IterCb(func(filename string, file *torrent.File) { - if file.Link == "" { - // will be caught by torrent manager's repairAll - // just skip it for now - return - } - filePath := filepath.Join(requestPath, url.PathEscape(filename)) + responses = append(responses, dav.Directory(requestPath+"/")) + filenames := tor.SelectedFiles.Keys() + sort.Strings(filenames) + for _, filename := range filenames { + file, _ := tor.SelectedFiles.Get(filename) responses = append(responses, dav.File( - filePath, + filepath.Join(requestPath, filename), file.Bytes, convertRFC3339toRFC1123(tor.LatestAdded), file.Link, )) - }) - + } resp := &dav.MultiStatus{ XMLNS: "DAV:", Response: responses, diff --git a/internal/http/listing.go b/internal/http/listing.go index 210976b..c725f28 100644 --- a/internal/http/listing.go +++ b/internal/http/listing.go @@ -71,16 +71,11 @@ func handleListOfTorrents(requestPath string, t *torrent.TorrentManager) (*strin } htmlDoc := "
    " - - var accessKeys []string - torrents.IterCb(func(accessKey string, _ *torrent.Torrent) { - accessKeys = append(accessKeys, accessKey) - }) + accessKeys := torrents.Keys() sort.Strings(accessKeys) for _, accessKey := range accessKeys { htmlDoc = htmlDoc + fmt.Sprintf("
  1. %s
  2. ", filepath.Join(requestPath, url.PathEscape(accessKey)), accessKey) } - return &htmlDoc, nil } @@ -97,16 +92,17 @@ func handleSingleTorrent(requestPath string, t *torrent.TorrentManager) (*string } htmlDoc := "
      " - - tor.SelectedFiles.IterCb(func(filename string, file *torrent.File) { + filenames := tor.SelectedFiles.Keys() + sort.Strings(filenames) + for _, filename := range filenames { + file, _ := tor.SelectedFiles.Get(filename) if file.Link == "" { // will be caught by torrent manager's repairAll // just skip it for now - return + continue } filePath := filepath.Join(requestPath, url.PathEscape(filename)) htmlDoc += fmt.Sprintf("
    1. %s
    2. ", filePath, filename) - }) - + } return &htmlDoc, nil } diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index b8a487d..0e65447 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -26,7 +26,6 @@ type TorrentManager struct { checksum string api *realdebrid.RealDebrid workerPool chan bool - mu *sync.Mutex log *zap.SugaredLogger } @@ -40,7 +39,6 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid) * requiredVersion: "10.11.2023", api: api, workerPool: make(chan bool, cfg.GetNumOfWorkers()), - mu: &sync.Mutex{}, log: logutil.NewLogger().Named("manager"), } @@ -99,11 +97,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid) * } // get filenames - var filenames []string - torrent.SelectedFiles.IterCb(func(_ string, file *File) { - filenames = append(filenames, file.Path) - }) - + filenames := torrent.SelectedFiles.Keys() // Map torrents to directories switch t.cfg.GetVersion() { case "v1":