75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package http
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/debridmediamanager/zurg/internal/torrent"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func HandleListDirectories(torMgr *torrent.TorrentManager) (*string, error) {
|
|
htmlDoc := "<ol>"
|
|
directories := torMgr.DirectoryMap.Keys()
|
|
sort.Strings(directories)
|
|
for _, directory := range directories {
|
|
if strings.HasPrefix(directory, "int__") {
|
|
continue
|
|
}
|
|
directoryPath := url.PathEscape(directory)
|
|
htmlDoc += fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", directoryPath, directory)
|
|
}
|
|
|
|
return &htmlDoc, nil
|
|
}
|
|
|
|
func HandleListTorrents(directory string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
|
|
torrents, ok := t.DirectoryMap.Get(directory)
|
|
if !ok {
|
|
return nil, fmt.Errorf("cannot find directory %s", directory)
|
|
}
|
|
|
|
htmlDoc := "<ol>"
|
|
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 {
|
|
htmlDoc = htmlDoc + fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", filepath.Join(directory, url.PathEscape(tor.AccessKey)), tor.AccessKey)
|
|
}
|
|
return &htmlDoc, nil
|
|
}
|
|
|
|
func HandleListFiles(directory, torrentName string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
|
|
torrents, ok := t.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)
|
|
}
|
|
|
|
htmlDoc := "<ol>"
|
|
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
|
|
}
|
|
filePath := filepath.Join(directory, torrentName, url.PathEscape(filename))
|
|
htmlDoc += fmt.Sprintf("<li><a href=\"/http/%s\">%s</a></li>", filePath, filename)
|
|
}
|
|
return &htmlDoc, nil
|
|
}
|