Convert everything to bytes

This commit is contained in:
Ben Sarmiento
2023-12-10 21:39:58 +01:00
parent 97161862ee
commit ff72265bfb
3 changed files with 25 additions and 20 deletions

View File

@@ -1,6 +1,7 @@
package http
import (
"bytes"
"fmt"
"net/url"
"path/filepath"
@@ -12,8 +13,9 @@ import (
"github.com/debridmediamanager/zurg/pkg/logutil"
)
func HandleListDirectories(torMgr *torrent.TorrentManager) (*string, error) {
htmlDoc := "<ol>"
func HandleListDirectories(torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<ol>")
directories := torMgr.DirectoryMap.Keys()
sort.Strings(directories)
for _, directory := range directories {
@@ -21,19 +23,20 @@ func HandleListDirectories(torMgr *torrent.TorrentManager) (*string, error) {
continue
}
directoryPath := url.PathEscape(directory)
htmlDoc += fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", directoryPath, directory)
buf.WriteString(fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", directoryPath, directory))
}
return &htmlDoc, nil
return buf.Bytes(), nil
}
func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *logutil.Logger) (*string, error) {
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)
}
htmlDoc := "<ol>"
var buf bytes.Buffer
buf.WriteString("<ol>")
var allTorrents []*torrent.Torrent
torrents.IterCb(func(_ string, tor *torrent.Torrent) {
if tor.AllInProgress() {
@@ -45,12 +48,12 @@ func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *l
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)
buf.WriteString(fmt.Sprintf("<li><a href=\"/http/%s/\">%s</a></li>", filepath.Join(directory, url.PathEscape(tor.AccessKey)), tor.AccessKey))
}
return &htmlDoc, nil
return buf.Bytes(), nil
}
func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManager, log *logutil.Logger) (*string, error) {
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)
@@ -66,7 +69,8 @@ func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManag
biggestFileSize = tor.ComputeBiggestFileSize()
}
htmlDoc := "<ol>"
var buf bytes.Buffer
buf.WriteString("<ol>")
filenames := tor.SelectedFiles.Keys()
sort.Strings(filenames)
for _, filename := range filenames {
@@ -78,7 +82,7 @@ func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManag
continue
}
filePath := filepath.Join(directory, torrentName, url.PathEscape(filename))
htmlDoc += fmt.Sprintf("<li><a href=\"/http/%s\">%s</a></li>", filePath, filename)
buf.WriteString(fmt.Sprintf("<li><a href=\"/http/%s\">%s</a></li>", filePath, filename))
}
return &htmlDoc, nil
return buf.Bytes(), nil
}