Use new router
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user