diff --git a/internal/dav/getfile.go b/internal/dav/getfile.go index 37be857..b42c7c5 100644 --- a/internal/dav/getfile.go +++ b/internal/dav/getfile.go @@ -38,7 +38,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent torrentName := segments[len(segments)-2] filename := segments[len(segments)-1] - torrents := findAllTorrentsWithName(t, baseDirectory, torrentName) + torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName) if torrents == nil { log.Println("Cannot find torrent", torrentName) http.Error(w, "Cannot find file", http.StatusNotFound) diff --git a/internal/dav/propfind.go b/internal/dav/propfind.go index 76b7bf5..f982f3b 100644 --- a/internal/dav/propfind.go +++ b/internal/dav/propfind.go @@ -92,7 +92,7 @@ func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Requ directory := path.Dir(requestPath) torrentName := path.Base(requestPath) - sameNameTorrents := findAllTorrentsWithName(t, directory, torrentName) + sameNameTorrents := t.FindAllTorrentsWithName(directory, torrentName) if len(sameNameTorrents) == 0 { return nil, fmt.Errorf("cannot find directory when generating single torrent: %s", requestPath) } diff --git a/internal/dav/util.go b/internal/dav/util.go index aa40a77..d1670f1 100644 --- a/internal/dav/util.go +++ b/internal/dav/util.go @@ -2,10 +2,7 @@ package dav import ( "log" - "strings" "time" - - "github.com/debridmediamanager.com/zurg/internal/torrent" ) // convertRFC3339toRFC1123 converts a date from RFC3339 to RFC1123 @@ -17,15 +14,3 @@ func convertRFC3339toRFC1123(input string) string { } return t.Format("Mon, 02 Jan 2006 15:04:05 GMT") } - -// findAllTorrentsWithName finds all torrents in a given directory with a given name -func findAllTorrentsWithName(t *torrent.TorrentManager, directory, torrentName string) []torrent.Torrent { - matchingTorrents := make([]torrent.Torrent, 0, 10) - torrents := t.GetByDirectory(directory) - for i := range torrents { - if torrents[i].Name == torrentName || strings.HasPrefix(torrents[i].Name, torrentName) { - matchingTorrents = append(matchingTorrents, torrents[i]) - } - } - return matchingTorrents -} diff --git a/internal/http/get.go b/internal/http/get.go index 8a40562..f86f1a4 100644 --- a/internal/http/get.go +++ b/internal/http/get.go @@ -44,7 +44,7 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren torrentName := segments[len(segments)-2] filename := segments[len(segments)-1] - torrents := findAllTorrentsWithName(t, baseDirectory, torrentName) + torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName) if torrents == nil { log.Println("Cannot find torrent", torrentName, segments) http.Error(w, "Cannot find file", http.StatusNotFound) @@ -115,7 +115,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent torrentName := segments[len(segments)-2] filename := segments[len(segments)-1] - torrents := findAllTorrentsWithName(t, baseDirectory, torrentName) + torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName) if torrents == nil { log.Println("Cannot find torrent", torrentName) http.Error(w, "Cannot find file", http.StatusNotFound) @@ -172,17 +172,6 @@ func getFile(torrents []torrent.Torrent, filename, fragment string) (*torrent.To return nil, nil } -func findAllTorrentsWithName(t *torrent.TorrentManager, directory, torrentName string) []torrent.Torrent { - matchingTorrents := make([]torrent.Torrent, 0, 10) - torrents := t.GetByDirectory(directory) - for i := range torrents { - if torrents[i].Name == torrentName || strings.HasPrefix(torrents[i].Name, torrentName) { - matchingTorrents = append(matchingTorrents, torrents[i]) - } - } - return matchingTorrents -} - func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) { requestPath := path.Clean(r.URL.Path) @@ -258,7 +247,7 @@ func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Requ directory := path.Base(fullDir) torrentName := path.Base(requestPath) - sameNameTorrents := findAllTorrentsWithName(t, directory, torrentName) + sameNameTorrents := t.FindAllTorrentsWithName(directory, torrentName) if len(sameNameTorrents) == 0 { return nil, fmt.Errorf("cannot find directory when generating single torrent: %s", requestPath) }