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

@@ -13,18 +13,19 @@ import (
) )
func HandleListDirectories(torMgr *torrent.TorrentManager) ([]byte, error) { func HandleListDirectories(torMgr *torrent.TorrentManager) ([]byte, error) {
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" var buf bytes.Buffer
davDoc += dav.BaseDirectory("", "") buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
buf.WriteString(dav.BaseDirectory("", ""))
directories := torMgr.DirectoryMap.Keys() directories := torMgr.DirectoryMap.Keys()
sort.Strings(directories) sort.Strings(directories)
for _, directory := range directories { for _, directory := range directories {
if strings.HasPrefix(directory, "int__") { if strings.HasPrefix(directory, "int__") {
continue continue
} }
davDoc += dav.Directory(directory, "") buf.WriteString(dav.Directory(directory, ""))
} }
davDoc += "</d:multistatus>" buf.WriteString("</d:multistatus>")
return []byte(davDoc), nil return buf.Bytes(), nil
} }
func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) {

View File

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

View File

@@ -89,7 +89,7 @@ func (zr *ZurgRouter) httpTorrentDirectoryHandler(resp http.ResponseWriter, req
} }
resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"") resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK) resp.WriteHeader(http.StatusOK)
fmt.Fprint(resp, *out) resp.Write(out)
} }
func (zr *ZurgRouter) httpDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { func (zr *ZurgRouter) httpDirectoryHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
@@ -101,7 +101,7 @@ func (zr *ZurgRouter) httpDirectoryHandler(resp http.ResponseWriter, req *http.R
} }
resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"") resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK) resp.WriteHeader(http.StatusOK)
fmt.Fprint(resp, *out) resp.Write(out)
} }
func (zr *ZurgRouter) httpRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { func (zr *ZurgRouter) httpRootHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {
@@ -112,7 +112,7 @@ func (zr *ZurgRouter) httpRootHandler(resp http.ResponseWriter, req *http.Reques
} }
resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"") resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"")
resp.WriteHeader(http.StatusOK) resp.WriteHeader(http.StatusOK)
fmt.Fprint(resp, *out) resp.Write(out)
} }
func (zr *ZurgRouter) propfindFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) { func (zr *ZurgRouter) propfindFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {