Optimizations
This commit is contained in:
@@ -9,21 +9,24 @@ import (
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/dav"
|
||||
"github.com/dgraph-io/ristretto"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, log *zap.SugaredLogger) {
|
||||
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, cache *ristretto.Cache, log *zap.SugaredLogger) {
|
||||
requestPath := path.Clean(r.URL.Path)
|
||||
filteredSegments := splitIntoSegments(requestPath)
|
||||
|
||||
var output *string
|
||||
var err error
|
||||
|
||||
filteredSegments := splitIntoSegments(requestPath)
|
||||
switch {
|
||||
case len(filteredSegments) == 0:
|
||||
err = handleListDirectories(w, t)
|
||||
output, err = handleListDirectories(w, t)
|
||||
case len(filteredSegments) == 1:
|
||||
err = handleListTorrents(w, requestPath, t)
|
||||
output, err = handleListTorrents(w, requestPath, t)
|
||||
case len(filteredSegments) == 2:
|
||||
err = handleListFiles(w, requestPath, t)
|
||||
output, err = handleListFiles(w, requestPath, t)
|
||||
default:
|
||||
log.Warnf("Request %s %s not found", r.Method, requestPath)
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
@@ -40,13 +43,17 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
|
||||
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) error {
|
||||
fmt.Fprint(w, "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
||||
func handleListDirectories(w http.ResponseWriter, t *torrent.TorrentManager) (*string, error) {
|
||||
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">"
|
||||
// initial response is the directory itself
|
||||
fmt.Fprint(w, dav.BaseDirectory("", ""))
|
||||
davDoc += dav.BaseDirectory("", "")
|
||||
|
||||
directories := t.DirectoryMap.Keys()
|
||||
sort.Strings(directories)
|
||||
@@ -54,61 +61,40 @@ func handleListDirectories(w http.ResponseWriter, t *torrent.TorrentManager) err
|
||||
if strings.HasPrefix(directory, "int__") {
|
||||
continue
|
||||
}
|
||||
fmt.Fprint(w, dav.Directory(directory, ""))
|
||||
davDoc += dav.Directory(directory, "")
|
||||
}
|
||||
|
||||
fmt.Fprint(w, "</d:multistatus>")
|
||||
return nil
|
||||
davDoc += "</d:multistatus>"
|
||||
return &davDoc, nil
|
||||
}
|
||||
|
||||
func handleListTorrents(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) error {
|
||||
func handleListTorrents(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) (*string, error) {
|
||||
basePath := path.Base(requestPath)
|
||||
torrents, ok := t.DirectoryMap.Get(basePath)
|
||||
_, ok := t.DirectoryMap.Get(basePath)
|
||||
if !ok {
|
||||
return fmt.Errorf("cannot find directory %s", basePath)
|
||||
return nil, fmt.Errorf("cannot find directory %s", basePath)
|
||||
}
|
||||
|
||||
fmt.Fprint(w, "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
||||
// initial response is the directory itself
|
||||
fmt.Fprint(w, dav.BaseDirectory(basePath, ""))
|
||||
resp, _ := t.ResponseCache.Get(basePath + ".dav")
|
||||
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" + dav.BaseDirectory(basePath, "") + dav.BaseDirectory(basePath, "") + resp.(string) + "</d:multistatus>"
|
||||
|
||||
var allTorrents []torrent.Torrent
|
||||
torrents.IterCb(func(key string, tor *torrent.Torrent) {
|
||||
if tor.AllInProgress() {
|
||||
return
|
||||
}
|
||||
copy := *tor
|
||||
copy.AccessKey = key
|
||||
allTorrents = append(allTorrents, copy)
|
||||
})
|
||||
sort.Slice(allTorrents, func(i, j int) bool {
|
||||
return allTorrents[i].AccessKey < allTorrents[j].AccessKey
|
||||
})
|
||||
|
||||
for _, tor := range allTorrents {
|
||||
fmt.Fprint(w, dav.Directory(tor.AccessKey, tor.LatestAdded))
|
||||
}
|
||||
|
||||
fmt.Fprint(w, "</d:multistatus>")
|
||||
return nil
|
||||
return &davDoc, nil
|
||||
}
|
||||
|
||||
func handleListFiles(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) error {
|
||||
func handleListFiles(w http.ResponseWriter, requestPath string, t *torrent.TorrentManager) (*string, 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)
|
||||
return nil, 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)
|
||||
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
|
||||
}
|
||||
|
||||
fmt.Fprint(w, "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
||||
// initial response is the directory itself
|
||||
fmt.Fprint(w, dav.BaseDirectory(requestPath, tor.LatestAdded))
|
||||
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" + dav.BaseDirectory(requestPath, tor.LatestAdded)
|
||||
|
||||
filenames := tor.SelectedFiles.Keys()
|
||||
sort.Strings(filenames)
|
||||
@@ -117,9 +103,9 @@ func handleListFiles(w http.ResponseWriter, requestPath string, t *torrent.Torre
|
||||
if file == nil || !strings.HasPrefix(file.Link, "http") {
|
||||
continue
|
||||
}
|
||||
fmt.Fprint(w, dav.File(filename, file.Bytes, file.Ended))
|
||||
davDoc += dav.File(filename, file.Bytes, file.Ended)
|
||||
}
|
||||
|
||||
fmt.Fprint(w, "</d:multistatus>")
|
||||
return nil
|
||||
davDoc += "</d:multistatus>"
|
||||
return &davDoc, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user