use a new thread safe map
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
@@ -24,11 +25,11 @@ func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.T
|
||||
filteredSegments := removeEmptySegments(strings.Split(requestPath, "/"))
|
||||
switch {
|
||||
case len(filteredSegments) == 1:
|
||||
output, err = handleRoot(w, r, c)
|
||||
output, err = handleRoot(c)
|
||||
case len(filteredSegments) == 2:
|
||||
output, err = handleListOfTorrents(requestPath, w, r, t, c)
|
||||
output, err = handleListOfTorrents(requestPath, t)
|
||||
case len(filteredSegments) == 3:
|
||||
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)
|
||||
@@ -51,7 +52,7 @@ func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.T
|
||||
}
|
||||
}
|
||||
|
||||
func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface) (*string, error) {
|
||||
func handleRoot(c config.ConfigInterface) (*string, error) {
|
||||
htmlDoc := "<ul>"
|
||||
|
||||
for _, directory := range c.GetDirectories() {
|
||||
@@ -62,50 +63,50 @@ func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface
|
||||
return &htmlDoc, nil
|
||||
}
|
||||
|
||||
func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) (*string, error) {
|
||||
func handleListOfTorrents(requestPath string, t *torrent.TorrentManager) (*string, error) {
|
||||
basePath := path.Base(requestPath)
|
||||
|
||||
for _, directory := range c.GetDirectories() {
|
||||
if basePath == directory {
|
||||
htmlDoc := "<ol>"
|
||||
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 {
|
||||
htmlDoc += fmt.Sprintf("<li><a href=\"%s/\">%s</a></li>", filepath.Join(requestPath, url.PathEscape(accessKey)), accessKey)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return &htmlDoc, nil
|
||||
}
|
||||
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)
|
||||
htmlDoc := "<ol>"
|
||||
|
||||
var accessKeys []string
|
||||
torrents.IterCb(func(accessKey string, _ *torrent.Torrent) {
|
||||
accessKeys = append(accessKeys, accessKey)
|
||||
})
|
||||
sort.Strings(accessKeys)
|
||||
for _, accessKey := range accessKeys {
|
||||
htmlDoc = htmlDoc + fmt.Sprintf("<li><a href=\"%s/\">%s</a></li>", filepath.Join(requestPath, url.PathEscape(accessKey)), accessKey)
|
||||
}
|
||||
|
||||
return &htmlDoc, nil
|
||||
}
|
||||
|
||||
func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) (*string, error) {
|
||||
func handleSingleTorrent(requestPath string, t *torrent.TorrentManager) (*string, 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)
|
||||
torrent, _ := t.TorrentMap.Get(accessKey)
|
||||
if torrent == nil {
|
||||
tor, ok := torrents.Get(accessKey)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
|
||||
}
|
||||
|
||||
htmlDoc := "<ol>"
|
||||
for el := torrent.SelectedFiles.Front(); el != nil; el = el.Next() {
|
||||
file := el.Value
|
||||
|
||||
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
|
||||
continue
|
||||
return
|
||||
}
|
||||
filename := filepath.Base(file.Path)
|
||||
filePath := filepath.Join(requestPath, url.PathEscape(filename))
|
||||
htmlDoc += fmt.Sprintf("<li><a href=\"%s\">%s</a></li>", filePath, filename)
|
||||
}
|
||||
})
|
||||
|
||||
return &htmlDoc, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user