Last batch of optimizations

This commit is contained in:
Ben Sarmiento
2023-11-19 02:55:19 +01:00
parent ae635799f8
commit 81d1df9bb5
6 changed files with 19 additions and 17 deletions

View File

@@ -9,13 +9,12 @@ import (
"sort"
"strings"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/dav"
"github.com/debridmediamanager.com/zurg/pkg/logutil"
)
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) {
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) {
log := logutil.NewLogger().Named("dav")
requestPath := path.Clean(r.URL.Path)
@@ -28,7 +27,7 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
switch {
case len(filteredSegments) == 1 && filteredSegments[0] == "":
output, err = handleRoot(c)
output, err = handleRoot(t)
case len(filteredSegments) == 1:
output, err = handleListOfTorrents(requestPath, t)
case len(filteredSegments) == 2:
@@ -57,10 +56,13 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
}
}
func handleRoot(c config.ConfigInterface) ([]byte, error) {
func handleRoot(t *torrent.TorrentManager) ([]byte, error) {
var responses []dav.Response
responses = append(responses, dav.Directory(""))
for _, directory := range c.GetDirectories() {
directories := t.DirectoryMap.Keys()
sort.Strings(directories)
for _, directory := range directories {
responses = append(responses, dav.Directory(directory))
}
rootResponse := dav.MultiStatus{

View File

@@ -9,12 +9,11 @@ import (
"sort"
"strings"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/logutil"
)
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) {
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) {
log := logutil.NewLogger().Named("http")
requestPath := path.Clean(r.URL.Path)
@@ -25,7 +24,7 @@ func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.T
filteredSegments := removeEmptySegments(strings.Split(requestPath, "/"))
switch {
case len(filteredSegments) == 1:
output, err = handleRoot(c)
output, err = handleRoot(t)
case len(filteredSegments) == 2:
output, err = handleListOfTorrents(requestPath, t)
case len(filteredSegments) == 3:
@@ -52,10 +51,11 @@ func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.T
}
}
func handleRoot(c config.ConfigInterface) (*string, error) {
htmlDoc := "<ul>"
for _, directory := range c.GetDirectories() {
func handleRoot(t *torrent.TorrentManager) (*string, error) {
htmlDoc := "<ol>"
directories := t.DirectoryMap.Keys()
sort.Strings(directories)
for _, directory := range directories {
directoryPath := url.PathEscape(directory)
htmlDoc += fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", directoryPath, directory)
}

View File

@@ -25,7 +25,7 @@ func Router(mux *http.ServeMux, c config.ConfigInterface, t *torrent.TorrentMana
if countNonEmptySegments(strings.Split(requestPath, "/")) > 3 {
universal.HandleGetRequest(w, r, t, c, cache)
} else {
zurghttp.HandleDirectoryListing(w, r, t, c)
zurghttp.HandleDirectoryListing(w, r, t)
}
case http.MethodHead:
@@ -40,7 +40,7 @@ func Router(mux *http.ServeMux, c config.ConfigInterface, t *torrent.TorrentMana
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "PROPFIND":
dav.HandlePropfindRequest(w, r, t, c)
dav.HandlePropfindRequest(w, r, t)
case http.MethodGet:
universal.HandleGetRequest(w, r, t, c, cache)

View File

@@ -34,9 +34,9 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentM
// If there are less than 3 segments, return an error or adjust as needed
if len(segments) <= 3 {
if isDav {
dav.HandlePropfindRequest(w, r, t, c)
dav.HandlePropfindRequest(w, r, t)
} else {
intHttp.HandleDirectoryListing(w, r, t, c)
intHttp.HandleDirectoryListing(w, r, t)
}
return
}