Use new router

This commit is contained in:
Ben Sarmiento
2023-11-30 22:46:29 +01:00
parent 5472f7d08d
commit 0ad879066e
13 changed files with 311 additions and 384 deletions

View File

@@ -2,88 +2,36 @@ package dav
import (
"fmt"
"net/http"
"path"
"strings"
"github.com/debridmediamanager/zurg/internal/torrent"
"go.uber.org/zap"
)
func HandleDeleteRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) {
requestPath := path.Clean(r.URL.Path)
filteredSegments := splitIntoSegments(requestPath)
var err error
switch {
case len(filteredSegments) == 0:
err = fmt.Errorf("cannot delete root")
case len(filteredSegments) == 1:
err = fmt.Errorf("cannot delete configured directory")
case len(filteredSegments) == 2:
err = handleDeleteTorrent(w, filteredSegments, t)
case len(filteredSegments) >= 3:
err = handleDeleteFile(w, filteredSegments, t)
default:
log.Warnf("Request %s %s not found", r.Method, requestPath)
http.Error(w, "Not Found", http.StatusNotFound)
return
}
if err != nil {
if strings.Contains(err.Error(), "cannot find") {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
log.Errorf("Error processing request: %v", err)
http.Error(w, "Server error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
}
func handleDeleteTorrent(w http.ResponseWriter, segments []string, t *torrent.TorrentManager) error {
directory := segments[0]
func HandleDeleteTorrent(directory, torrentName string, t *torrent.TorrentManager) error {
torrents, ok := t.DirectoryMap.Get(directory)
if !ok {
return fmt.Errorf("cannot find directory %s", directory)
}
accessKey := segments[1]
tor, ok := torrents.Get(accessKey)
if !ok {
return fmt.Errorf("cannot find torrent %s", accessKey)
if !torrents.Has(torrentName) {
return fmt.Errorf("cannot find torrent %s", torrentName)
}
t.Delete(tor.AccessKey)
w.WriteHeader(http.StatusNoContent)
t.Delete(torrentName)
return nil
}
func handleDeleteFile(w http.ResponseWriter, segments []string, t *torrent.TorrentManager) error {
directory := segments[0]
func HandleDeleteFile(directory, torrentName, fileName string, t *torrent.TorrentManager) error {
torrents, ok := t.DirectoryMap.Get(directory)
if !ok {
return fmt.Errorf("cannot find directory %s", directory)
}
accessKey := segments[1]
tor, ok := torrents.Get(accessKey)
tor, ok := torrents.Get(torrentName)
if !ok {
return fmt.Errorf("cannot find torrent %s", accessKey)
return fmt.Errorf("cannot find torrent %s", torrentName)
}
// set filepath to last segment
filepath := segments[len(segments)-1]
file, ok := tor.SelectedFiles.Get(filepath)
file, ok := tor.SelectedFiles.Get(fileName)
if !ok {
return fmt.Errorf("cannot find file %s", filepath)
return fmt.Errorf("cannot find file %s", fileName)
}
file.Link = "unselect"
t.ScheduleForRefresh()
w.WriteHeader(http.StatusNoContent)
return nil
}

View File

@@ -2,8 +2,6 @@ package dav
import (
"fmt"
"net/http"
"path"
"path/filepath"
"sort"
"strings"
@@ -13,44 +11,7 @@ import (
"go.uber.org/zap"
)
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) {
requestPath := path.Clean(r.URL.Path)
var output *string
var err error
filteredSegments := splitIntoSegments(requestPath)
switch {
case len(filteredSegments) == 0:
output, err = handleListDirectories(w, t)
case len(filteredSegments) == 1:
output, err = handleListTorrents(w, requestPath, t, log)
case len(filteredSegments) == 2:
output, err = handleListFiles(w, requestPath, t, log)
default:
log.Warnf("Request %s %s not found", r.Method, requestPath)
http.Error(w, "Not Found", http.StatusNotFound)
return
}
if err != nil {
if strings.Contains(err.Error(), "cannot find") {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
log.Errorf("Error processing request: %v", err)
http.Error(w, "Server error", http.StatusInternalServerError)
return
}
if output != nil {
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, *output)
}
}
func handleListDirectories(w http.ResponseWriter, t *torrent.TorrentManager) (*string, error) {
func HandleListDirectories(t *torrent.TorrentManager) (*string, error) {
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">"
// initial response is the directory itself
davDoc += dav.BaseDirectory("", "")
@@ -68,8 +29,7 @@ func handleListDirectories(w http.ResponseWriter, t *torrent.TorrentManager) (*s
return &davDoc, nil
}
func handleListTorrents(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
directory := path.Base(requestPath)
func HandleListTorrents(directory string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
_, ok := t.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
@@ -92,20 +52,18 @@ func handleListTorrents(w http.ResponseWriter, requestPath string, t *torrent.To
}
}
func handleListFiles(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
directory := path.Base(path.Dir(requestPath))
func HandleListFiles(directory, torrentName string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
torrents, ok := t.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
}
accessKey := path.Base(requestPath)
tor, ok := torrents.Get(accessKey)
tor, ok := torrents.Get(torrentName)
if !ok {
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
return nil, fmt.Errorf("cannot find torrent %s", torrentName)
}
if resp, ok := t.ResponseCache.Get(directory + "/" + accessKey + ".dav"); !ok {
log.Debugf("Generating xml for torrent %s", accessKey)
if resp, ok := t.ResponseCache.Get(directory + "/" + torrentName + ".dav"); !ok {
log.Debugf("Generating xml for torrent %s", torrentName)
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" + dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded)
filenames := tor.SelectedFiles.Keys()
sort.Strings(filenames)