Refactor torrent manager

This commit is contained in:
Ben Sarmiento
2023-11-09 02:34:04 +01:00
parent 9dfd6c32d5
commit 15a0ba95d8
14 changed files with 436 additions and 516 deletions

View File

@@ -5,26 +5,19 @@ import (
"net/http"
"net/url"
"path"
"path/filepath"
"strings"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/logutil"
"github.com/hashicorp/golang-lru/v2/expirable"
)
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) {
log := logutil.NewLogger().Named("http")
requestPath := path.Clean(r.URL.Path)
if data, exists := cache.Get(requestPath); exists {
w.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, data)
return
}
var output *string
var err error
@@ -49,8 +42,6 @@ func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.T
}
if output != nil {
cache.Add(requestPath, *output)
w.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, *output)
@@ -73,12 +64,19 @@ func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Req
for _, directory := range c.GetDirectories() {
if basePath == directory {
torrents := t.GetByDirectory(basePath)
resp, err := createMultiTorrentResponse(requestPath, torrents)
if err != nil {
return nil, fmt.Errorf("cannot read directory (%s): %w", basePath, err)
htmlDoc := "<ol>"
for name, torrent := range t.TorrentMap {
if len(torrent.SelectedFiles) == 0 {
continue
}
for _, dir := range torrent.Directories {
if dir == basePath {
htmlDoc += fmt.Sprintf("<li><a href=\"%s/\">%s</a></li>", filepath.Join(requestPath, url.PathEscape(name)), name)
break
}
}
}
return &resp, nil
return &htmlDoc, nil
}
}
@@ -86,18 +84,17 @@ func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Req
}
func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) (*string, error) {
fullDir := path.Dir(requestPath)
directory := path.Base(fullDir)
torrentName := path.Base(requestPath)
sameNameTorrents := t.FindAllTorrentsWithName(directory, torrentName)
if len(sameNameTorrents) == 0 {
return nil, fmt.Errorf("cannot find directory when generating single torrent: %s", requestPath)
htmlDoc := "<ol>"
for _, file := range t.TorrentMap[torrentName].SelectedFiles {
if file.Link == "" {
// TODO: fix the file?
fmt.Printf("File %s has no link, skipping\n", file.Path)
continue
}
filename := filepath.Base(file.Path)
filePath := filepath.Join(requestPath, url.PathEscape(filename))
htmlDoc += fmt.Sprintf("<li><a href=\"%s\">%s</a></li>", filePath, filename)
}
resp, err := createSingleTorrentResponse(requestPath, sameNameTorrents)
if err != nil {
return nil, fmt.Errorf("cannot read directory (%s): %w", requestPath, err)
}
return &resp, nil
return &htmlDoc, nil
}