Files
zurg/internal/http/listing.go
2023-11-18 12:53:39 +01:00

113 lines
3.0 KiB
Go

package http
import (
"fmt"
"net/http"
"net/url"
"path"
"path/filepath"
"sort"
"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(c)
case len(filteredSegments) == 2:
output, err = handleListOfTorrents(requestPath, t)
case len(filteredSegments) == 3:
output, err = handleSingleTorrent(requestPath, 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(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, t *torrent.TorrentManager) (*string, error) {
basePath := path.Base(requestPath)
torrents, ok := t.DirectoryMap.Get(basePath)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", basePath)
}
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, 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)
tor, ok := torrents.Get(accessKey)
if !ok {
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
}
htmlDoc := "<ol>"
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))
htmlDoc += fmt.Sprintf("<li><a href=\"%s\">%s</a></li>", filePath, filename)
})
return &htmlDoc, nil
}