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

@@ -14,7 +14,7 @@ env:
jobs: jobs:
build-and-push-image: build-and-push-image:
runs-on: ubuntu-latest runs-on: self-hosted
permissions: permissions:
contents: read contents: read
packages: write packages: write

View File

@@ -9,13 +9,12 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/torrent" "github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/dav" "github.com/debridmediamanager.com/zurg/pkg/dav"
"github.com/debridmediamanager.com/zurg/pkg/logutil" "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") log := logutil.NewLogger().Named("dav")
requestPath := path.Clean(r.URL.Path) requestPath := path.Clean(r.URL.Path)
@@ -28,7 +27,7 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
switch { switch {
case len(filteredSegments) == 1 && filteredSegments[0] == "": case len(filteredSegments) == 1 && filteredSegments[0] == "":
output, err = handleRoot(c) output, err = handleRoot(t)
case len(filteredSegments) == 1: case len(filteredSegments) == 1:
output, err = handleListOfTorrents(requestPath, t) output, err = handleListOfTorrents(requestPath, t)
case len(filteredSegments) == 2: 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 var responses []dav.Response
responses = append(responses, dav.Directory("")) 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)) responses = append(responses, dav.Directory(directory))
} }
rootResponse := dav.MultiStatus{ rootResponse := dav.MultiStatus{

View File

@@ -9,12 +9,11 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/torrent" "github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/logutil" "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") log := logutil.NewLogger().Named("http")
requestPath := path.Clean(r.URL.Path) 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, "/")) filteredSegments := removeEmptySegments(strings.Split(requestPath, "/"))
switch { switch {
case len(filteredSegments) == 1: case len(filteredSegments) == 1:
output, err = handleRoot(c) output, err = handleRoot(t)
case len(filteredSegments) == 2: case len(filteredSegments) == 2:
output, err = handleListOfTorrents(requestPath, t) output, err = handleListOfTorrents(requestPath, t)
case len(filteredSegments) == 3: 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) { func handleRoot(t *torrent.TorrentManager) (*string, error) {
htmlDoc := "<ul>" htmlDoc := "<ol>"
directories := t.DirectoryMap.Keys()
for _, directory := range c.GetDirectories() { sort.Strings(directories)
for _, directory := range directories {
directoryPath := url.PathEscape(directory) directoryPath := url.PathEscape(directory)
htmlDoc += fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", directoryPath, 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 { if countNonEmptySegments(strings.Split(requestPath, "/")) > 3 {
universal.HandleGetRequest(w, r, t, c, cache) universal.HandleGetRequest(w, r, t, c, cache)
} else { } else {
zurghttp.HandleDirectoryListing(w, r, t, c) zurghttp.HandleDirectoryListing(w, r, t)
} }
case http.MethodHead: 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) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case "PROPFIND": case "PROPFIND":
dav.HandlePropfindRequest(w, r, t, c) dav.HandlePropfindRequest(w, r, t)
case http.MethodGet: case http.MethodGet:
universal.HandleGetRequest(w, r, t, c, cache) 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 there are less than 3 segments, return an error or adjust as needed
if len(segments) <= 3 { if len(segments) <= 3 {
if isDav { if isDav {
dav.HandlePropfindRequest(w, r, t, c) dav.HandlePropfindRequest(w, r, t)
} else { } else {
intHttp.HandleDirectoryListing(w, r, t, c) intHttp.HandleDirectoryListing(w, r, t)
} }
return return
} }