package http import ( "bytes" "fmt" "net/url" "path/filepath" "sort" "strings" "github.com/debridmediamanager/zurg/internal/config" "github.com/debridmediamanager/zurg/internal/torrent" "github.com/debridmediamanager/zurg/pkg/logutil" ) func HandleListDirectories(torMgr *torrent.TorrentManager) ([]byte, error) { var buf bytes.Buffer buf.WriteString("
    ") directories := torMgr.DirectoryMap.Keys() sort.Strings(directories) for _, directory := range directories { if strings.HasPrefix(directory, "int__") { continue } directoryPath := url.PathEscape(directory) buf.WriteString(fmt.Sprintf("
  1. %s
  2. ", directoryPath, directory)) } return buf.Bytes(), nil } func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) } var buf bytes.Buffer buf.WriteString("
      ") var allTorrents []*torrent.Torrent torrents.IterCb(func(_ string, tor *torrent.Torrent) { if tor.AllInProgress() { return } allTorrents = append(allTorrents, tor) }) sort.Slice(allTorrents, func(i, j int) bool { return allTorrents[i].AccessKey < allTorrents[j].AccessKey }) for _, tor := range allTorrents { buf.WriteString(fmt.Sprintf("
    1. %s
    2. ", filepath.Join(directory, url.PathEscape(tor.AccessKey)), tor.AccessKey)) } return buf.Bytes(), nil } func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { torrents, ok := torMgr.DirectoryMap.Get(directory) if !ok { return nil, fmt.Errorf("cannot find directory %s", directory) } tor, ok := torrents.Get(torrentName) if !ok { return nil, fmt.Errorf("cannot find torrent %s", torrentName) } dirCfg := torMgr.Config.(*config.ZurgConfigV1).GetDirectoryConfig(directory) biggestFileSize := int64(0) if dirCfg.OnlyShowTheBiggestFile { biggestFileSize = tor.ComputeBiggestFileSize() } var buf bytes.Buffer buf.WriteString("
        ") filenames := tor.SelectedFiles.Keys() sort.Strings(filenames) for _, filename := range filenames { file, ok := tor.SelectedFiles.Get(filename) if !ok || !strings.HasPrefix(file.Link, "http") { continue } if dirCfg.OnlyShowTheBiggestFile && file.Bytes < biggestFileSize { continue } filePath := filepath.Join(directory, torrentName, url.PathEscape(filename)) buf.WriteString(fmt.Sprintf("
      1. %s
      2. ", filePath, filename)) } return buf.Bytes(), nil }