Files
zurg/internal/http/listing.go
Ben Sarmiento abf4183af5 polishing
2023-11-11 04:21:35 +01:00

112 lines
3.2 KiB
Go

package http
import (
"fmt"
"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"
)
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)
var output *string
var err error
filteredSegments := removeEmptySegments(strings.Split(requestPath, "/"))
switch {
case len(filteredSegments) == 1:
output, err = handleRoot(w, r, c)
case len(filteredSegments) == 2:
output, err = handleListOfTorrents(requestPath, w, r, t, c)
case len(filteredSegments) == 3:
output, err = handleSingleTorrent(requestPath, w, r, t)
default:
log.Warnf("Request %s %s not found", r.Method, requestPath)
http.Error(w, "Not Found", http.StatusNotFound)
return
}
if err != nil {
if strings.Contains(err.Error(), "cannot find") {
http.Error(w, "Not Found", http.StatusNotFound)
return
}
log.Errorf("Error processing request: %v", err)
http.Error(w, "Server error", http.StatusInternalServerError)
return
}
if output != nil {
w.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, *output)
}
}
func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface) (*string, error) {
htmlDoc := "<ul>"
for _, directory := range c.GetDirectories() {
directoryPath := url.PathEscape(directory)
htmlDoc += fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", directoryPath, directory)
}
return &htmlDoc, nil
}
func handleListOfTorrents(requestPath string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) (*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
}
}
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) (*string, error) {
accessKey := path.Base(requestPath)
torrent, _ := t.TorrentMap.Get(accessKey)
if torrent == nil {
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
}
htmlDoc := "<ol>"
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, url.PathEscape(filename))
htmlDoc += fmt.Sprintf("<li><a href=\"%s\">%s</a></li>", filePath, filename)
}
return &htmlDoc, nil
}