use a new thread safe map

This commit is contained in:
Ben Sarmiento
2023-11-18 12:53:39 +01:00
parent b669f8d673
commit 0e9302f3b5
15 changed files with 577 additions and 535 deletions

View File

@@ -4,8 +4,10 @@ import (
"encoding/xml"
"fmt"
"net/http"
"net/url"
"path"
"path/filepath"
"sort"
"strings"
"github.com/debridmediamanager.com/zurg/internal/config"
@@ -27,11 +29,11 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
switch {
case len(filteredSegments) == 1 && filteredSegments[0] == "":
output, err = handleRoot(w, r, c)
output, err = handleRoot(c)
case len(filteredSegments) == 1:
output, err = handleListOfTorrents(requestPath, w, r, t, c)
output, err = handleListOfTorrents(requestPath, t)
case len(filteredSegments) == 2:
output, err = handleSingleTorrent(requestPath, w, r, t)
output, err = handleSingleTorrent(requestPath, t)
default:
log.Warnf("Request %s %s not found", r.Method, requestPath)
http.Error(w, "Not Found", http.StatusNotFound)
@@ -56,7 +58,7 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
}
}
func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface) ([]byte, error) {
func handleRoot(c config.ConfigInterface) ([]byte, error) {
var responses []dav.Response
responses = append(responses, dav.Directory(""))
for _, directory := range c.GetDirectories() {
@@ -69,68 +71,23 @@ func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface
return xml.Marshal(rootResponse)
}
func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) ([]byte, error) {
func handleListOfTorrents(requestPath string, t *torrent.TorrentManager) ([]byte, error) {
basePath := path.Base(requestPath)
for _, directory := range c.GetDirectories() {
if basePath == directory {
var responses []dav.Response
responses = append(responses, dav.Directory(basePath))
for el := t.TorrentMap.Front(); el != nil; el = el.Next() {
accessKey := el.Key
torrent := el.Value
if torrent.InProgress {
continue
}
for _, dir := range torrent.Directories {
if dir == basePath {
path := filepath.Join(basePath, accessKey)
responses = append(responses, dav.Directory(path))
break
}
}
}
resp := &dav.MultiStatus{
XMLNS: "DAV:",
Response: responses,
}
return xml.Marshal(resp)
}
torrents, ok := t.DirectoryMap.Get(basePath)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", basePath)
}
return nil, fmt.Errorf("cannot find directory when generating list: %s", requestPath)
}
func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
accessKey := path.Base(requestPath)
torrent, exists := t.TorrentMap.Get(accessKey)
if !exists {
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
}
var responses []dav.Response
// initial response is the directory itself
responses = append(responses, dav.Directory(requestPath))
responses = append(responses, dav.Directory(basePath))
for el := torrent.SelectedFiles.Front(); el != nil; el = el.Next() {
file := el.Value
if file.Link == "" {
// will be caught by torrent manager's repairAll
// just skip it for now
continue
}
filename := filepath.Base(file.Path)
filePath := filepath.Join(requestPath, filename)
responses = append(responses, dav.File(
filePath,
file.Bytes,
convertRFC3339toRFC1123(torrent.LatestAdded),
file.Link,
))
var accessKeys []string
torrents.IterCb(func(accessKey string, _ *torrent.Torrent) {
accessKeys = append(accessKeys, accessKey)
})
sort.Strings(accessKeys)
for _, accessKey := range accessKeys {
responses = append(responses, dav.Directory(filepath.Join(basePath, accessKey)))
}
resp := &dav.MultiStatus{
@@ -139,3 +96,41 @@ func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Requ
}
return xml.Marshal(resp)
}
func handleSingleTorrent(requestPath string, t *torrent.TorrentManager) ([]byte, error) {
basePath := path.Base(path.Dir(requestPath))
torrents, ok := t.DirectoryMap.Get(basePath)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", basePath)
}
accessKey := path.Base(requestPath)
tor, ok := torrents.Get(accessKey)
if !ok {
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
}
var responses []dav.Response
// initial response is the directory itself
responses = append(responses, dav.Directory(requestPath))
tor.SelectedFiles.IterCb(func(filename string, file *torrent.File) {
if file.Link == "" {
// will be caught by torrent manager's repairAll
// just skip it for now
return
}
filePath := filepath.Join(requestPath, url.PathEscape(filename))
responses = append(responses, dav.File(
filePath,
file.Bytes,
convertRFC3339toRFC1123(tor.LatestAdded),
file.Link,
))
})
resp := &dav.MultiStatus{
XMLNS: "DAV:",
Response: responses,
}
return xml.Marshal(resp)
}