Refactor torrent manager
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||
)
|
||||
|
||||
// createMultiTorrentResponse creates a WebDAV response for a list of torrents
|
||||
func createMultiTorrentResponse(basePath string, torrents []torrent.Torrent) (string, error) {
|
||||
htmlDoc := "<ul>"
|
||||
|
||||
seen := make(map[string]bool)
|
||||
|
||||
for _, item := range torrents {
|
||||
if item.Progress != 100 {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[item.AccessKey]; exists {
|
||||
continue
|
||||
}
|
||||
seen[item.AccessKey] = true
|
||||
|
||||
path := filepath.Join(basePath, url.PathEscape(item.AccessKey))
|
||||
htmlDoc += fmt.Sprintf("<li><a href=\"%s/\">%s</a></li>", path, item.AccessKey)
|
||||
}
|
||||
|
||||
return htmlDoc, nil
|
||||
}
|
||||
|
||||
func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (string, error) {
|
||||
htmlDoc := "<ul>"
|
||||
|
||||
finalName := make(map[string]bool)
|
||||
|
||||
currentPath := filepath.Join(basePath)
|
||||
|
||||
for _, torrent := range torrents {
|
||||
for _, file := range torrent.SelectedFiles {
|
||||
if file.Link == "" {
|
||||
// TODO: fix the file?
|
||||
// log.Println("File has no link, skipping", file.Path)
|
||||
continue
|
||||
}
|
||||
|
||||
filename := filepath.Base(file.Path)
|
||||
if finalName[filename] {
|
||||
continue
|
||||
}
|
||||
finalName[filename] = true
|
||||
|
||||
filePath := filepath.Join(currentPath, 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