package dav
import (
"fmt"
"net/http"
"path"
"sort"
"strings"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/dav"
"go.uber.org/zap"
)
func HandlePropfindRequest(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 = handleListDirectories(w, t)
case len(filteredSegments) == 1:
err = handleListTorrents(w, requestPath, t)
case len(filteredSegments) == 2:
err = handleListFiles(w, requestPath, 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 handleListDirectories(w http.ResponseWriter, t *torrent.TorrentManager) error {
fmt.Fprint(w, "")
// initial response is the directory itself
fmt.Fprint(w, dav.BaseDirectory("", ""))
directories := t.DirectoryMap.Keys()
sort.Strings(directories)
for _, directory := range directories {
if strings.HasPrefix(directory, "int__") {
continue
}
fmt.Fprint(w, dav.Directory(directory, ""))
}
fmt.Fprint(w, "")
return nil
}
func handleListTorrents(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) error {
basePath := path.Base(requestPath)
torrents, ok := t.DirectoryMap.Get(basePath)
if !ok {
return fmt.Errorf("cannot find directory %s", basePath)
}
fmt.Fprint(w, "")
// initial response is the directory itself
fmt.Fprint(w, dav.BaseDirectory(basePath, ""))
allTorrents := torrents.Keys()
sort.Strings(allTorrents)
for _, accessKey := range allTorrents {
tor, _ := torrents.Get(accessKey)
fmt.Fprint(w, dav.Directory(tor.AccessKey, tor.LatestAdded))
}
fmt.Fprint(w, "")
return nil
}
func handleListFiles(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) error {
requestPath = strings.Trim(requestPath, "/")
basePath := path.Base(path.Dir(requestPath))
torrents, ok := t.DirectoryMap.Get(basePath)
if !ok {
return fmt.Errorf("cannot find directory %s", basePath)
}
accessKey := path.Base(requestPath)
tor, ok := torrents.Get(accessKey)
if !ok {
return fmt.Errorf("cannot find torrent %s", accessKey)
}
fmt.Fprint(w, "")
// initial response is the directory itself
fmt.Fprint(w, dav.BaseDirectory(requestPath, tor.LatestAdded))
filenames := tor.SelectedFiles.Keys()
sort.Strings(filenames)
for _, filename := range filenames {
file, _ := tor.SelectedFiles.Get(filename)
if file == nil || !strings.HasPrefix(file.Link, "http") {
continue
}
fmt.Fprint(w, dav.File(filename, file.Bytes, file.Ended))
}
fmt.Fprint(w, "")
return nil
}