package http import ( "fmt" "net/http" "net/url" "path" "path/filepath" "sort" "strings" "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) { 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(t) 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(t *torrent.TorrentManager) (*string, error) { htmlDoc := "
    " directories := t.DirectoryMap.Keys() sort.Strings(directories) for _, directory := range directories { directoryPath := url.PathEscape(directory) htmlDoc += fmt.Sprintf("
  1. %s
  2. ", 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 := "
      " var allTorrents []*torrent.Torrent torrents.IterCb(func(_ string, tor *torrent.Torrent) { if tor.InProgress() { return } allTorrents = append(allTorrents, tor) }) sort.Slice(allTorrents, func(i, j int) bool { return allTorrents[i].AccessKey < allTorrents[j].AccessKey }) for _, tor := range allTorrents { htmlDoc = htmlDoc + fmt.Sprintf("
    1. %s
    2. ", filepath.Join(requestPath, url.PathEscape(tor.AccessKey)), tor.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 := "
        " filenames := tor.SelectedFiles.Keys() sort.Strings(filenames) for _, filename := range filenames { file, _ := tor.SelectedFiles.Get(filename) if file == nil || !strings.HasPrefix(file.Link, "http") { // will be caught by torrent manager's repairAll // just skip it for now continue } filePath := filepath.Join(requestPath, url.PathEscape(filename)) htmlDoc += fmt.Sprintf("
      1. %s
      2. ", filePath, filename) } return &htmlDoc, nil }