From ff72265bfb841509317c70586dc687f6dfa07a7d Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sun, 10 Dec 2023 21:39:58 +0100 Subject: [PATCH] Convert everything to bytes --- internal/dav/listing.go | 11 ++++++----- internal/http/listing.go | 28 ++++++++++++++++------------ internal/router/router.go | 6 +++--- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/internal/dav/listing.go b/internal/dav/listing.go index 156856d..9713c26 100644 --- a/internal/dav/listing.go +++ b/internal/dav/listing.go @@ -13,18 +13,19 @@ import ( ) func HandleListDirectories(torMgr *torrent.TorrentManager) ([]byte, error) { - davDoc := "" - davDoc += dav.BaseDirectory("", "") + var buf bytes.Buffer + buf.WriteString("") + buf.WriteString(dav.BaseDirectory("", "")) directories := torMgr.DirectoryMap.Keys() sort.Strings(directories) for _, directory := range directories { if strings.HasPrefix(directory, "int__") { continue } - davDoc += dav.Directory(directory, "") + buf.WriteString(dav.Directory(directory, "")) } - davDoc += "" - return []byte(davDoc), nil + buf.WriteString("") + return buf.Bytes(), nil } func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { diff --git a/internal/http/listing.go b/internal/http/listing.go index ed671c8..df21f04 100644 --- a/internal/http/listing.go +++ b/internal/http/listing.go @@ -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 := "
    " +func HandleListDirectories(torMgr *torrent.TorrentManager) ([]byte, error) { + var buf bytes.Buffer + buf.WriteString("
      ") 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("
    1. %s
    2. ", directoryPath, directory) + buf.WriteString(fmt.Sprintf("
    3. %s
    4. ", 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 := "
        " + var buf bytes.Buffer + buf.WriteString("
          ") 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("
        1. %s
        2. ", filepath.Join(directory, url.PathEscape(tor.AccessKey)), tor.AccessKey) + buf.WriteString(fmt.Sprintf("
        3. %s
        4. ", 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 := "
            " + var buf bytes.Buffer + buf.WriteString("
              ") 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("
            1. %s
            2. ", filePath, filename) + buf.WriteString(fmt.Sprintf("
            3. %s
            4. ", filePath, filename)) } - return &htmlDoc, nil + return buf.Bytes(), nil } diff --git a/internal/router/router.go b/internal/router/router.go index c030645..478848c 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -89,7 +89,7 @@ func (zr *ZurgRouter) httpTorrentDirectoryHandler(resp http.ResponseWriter, req } resp.Header().Set("Content-Type", "text/html; charset=\"utf-8\"") resp.WriteHeader(http.StatusOK) - fmt.Fprint(resp, *out) + resp.Write(out) } 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.WriteHeader(http.StatusOK) - fmt.Fprint(resp, *out) + resp.Write(out) } 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.WriteHeader(http.StatusOK) - fmt.Fprint(resp, *out) + resp.Write(out) } func (zr *ZurgRouter) propfindFileHandler(resp http.ResponseWriter, req *http.Request, params httprouter.Params) {