Files
zurg/internal/http/listing.go
2024-07-19 00:26:11 +02:00

108 lines
3.3 KiB
Go

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/internal/version"
)
func ServeRootDirectory(torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<ol>")
directories := torMgr.DirectoryMap.Keys()
directories = append(directories, config.DOWNLOADS)
sort.Strings(directories)
for _, directory := range directories {
if strings.HasPrefix(directory, "int__") {
continue
}
directoryPath := url.PathEscape(directory)
buf.WriteString(fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", directoryPath, directory))
}
buf.WriteString(fmt.Sprintf("<li><a href=\"/http/%s\">%s</a></li>", version.FILE, version.FILE))
return buf.Bytes(), nil
}
func ServeGroupDirectory(directory string, torMgr *torrent.TorrentManager) ([]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("<ol>")
torrentNames := torrents.Keys()
sort.Strings(torrentNames)
for _, torrentName := range torrentNames {
tor, ok := torrents.Get(torrentName)
if !ok {
continue
}
buf.WriteString(fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", filepath.Join(directory, url.PathEscape(torMgr.GetKey(tor))), torMgr.GetKey(tor)))
}
return buf.Bytes(), nil
}
func ServeTorrentFiles(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]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("<ol>")
filenames := tor.SelectedFiles.Keys()
sort.Strings(filenames)
for _, filename := range filenames {
file, ok := tor.SelectedFiles.Get(filename)
if !ok || file.State.Is("deleted_file") {
continue
}
if file.State.Is("broken_file") && shouldHideBrokenTorrents {
continue
}
if dirCfg.OnlyShowTheBiggestFile && file.Bytes < biggestFileSize {
continue
}
if dirCfg.OnlyShowFilesWithSizeLte > 0 && file.Bytes > dirCfg.OnlyShowFilesWithSizeLte {
continue
}
if dirCfg.OnlyShowFilesWithSizeGte > 0 && file.Bytes < dirCfg.OnlyShowFilesWithSizeGte {
continue
}
filePath := filepath.Join(directory, torrentName, url.PathEscape(filename))
buf.WriteString(fmt.Sprintf("<li><a href=\"/http/%s\">%s</a></li>", filePath, filename))
}
return buf.Bytes(), nil
}
func ServeDownloads(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<ol>")
filenames := torMgr.DownloadMap.Keys()
sort.Strings(filenames)
for _, filename := range filenames {
filePath := filepath.Join(config.DOWNLOADS, url.PathEscape(filename))
buf.WriteString(fmt.Sprintf("<li><a href=\"/http/%s\">%s</a></li>", filePath, filename))
}
return buf.Bytes(), nil
}