package http import ( "fmt" "net/http" "net/url" "path" "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]) { 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 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.Errorf("Request %s %s not found", r.Method, requestPath) http.Error(w, "Not Found", http.StatusNotFound) return } if err != nil { log.Errorf("Error processing request: %v", err) http.Error(w, "Server error", http.StatusInternalServerError) return } 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) } } func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface) (*string, error) { htmlDoc := "