diff --git a/internal/dav/listing.go b/internal/dav/listing.go index 5b4f8fc..156856d 100644 --- a/internal/dav/listing.go +++ b/internal/dav/listing.go @@ -1,6 +1,7 @@ package dav import ( + "bytes" "fmt" "path/filepath" "sort" @@ -32,8 +33,9 @@ func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *l return nil, fmt.Errorf("cannot find directory %s", directory) } - davDoc := "" - davDoc += dav.Directory("", "") + var buf bytes.Buffer + buf.WriteString("") + buf.WriteString(dav.BaseDirectory(directory, "")) var allTorrents []*torrent.Torrent torrents.IterCb(func(_ string, tor *torrent.Torrent) { if tor.AllInProgress() { @@ -45,10 +47,10 @@ func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *l return allTorrents[i].AccessKey < allTorrents[j].AccessKey }) for _, tor := range allTorrents { - davDoc += dav.Directory(tor.AccessKey, tor.LatestAdded) + buf.WriteString(dav.Directory(tor.AccessKey, tor.LatestAdded)) } - davDoc += "" - return []byte(davDoc), nil + buf.WriteString("") + return buf.Bytes(), nil } func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { @@ -61,7 +63,9 @@ func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManag return nil, fmt.Errorf("cannot find torrent %s", torrentName) } - davDoc := "" + dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded) + var buf bytes.Buffer + buf.WriteString("") + buf.WriteString(dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded)) filenames := tor.SelectedFiles.Keys() sort.Strings(filenames) for _, filename := range filenames { @@ -69,10 +73,10 @@ func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManag if !ok || !strings.HasPrefix(file.Link, "http") { continue } - davDoc += dav.File(filename, file.Bytes, file.Ended) + buf.WriteString(dav.File(filename, file.Bytes, file.Ended)) } - davDoc += "" - return []byte(davDoc), nil + buf.WriteString("") + return buf.Bytes(), nil } func HandlePropfindFile(directory, torrentName, fileName string, torMgr *torrent.TorrentManager, log *logutil.Logger) ([]byte, error) { @@ -88,8 +92,11 @@ func HandlePropfindFile(directory, torrentName, fileName string, torMgr *torrent if !ok || !strings.HasPrefix(file.Link, "http") { return nil, fmt.Errorf("cannot find file %s", fileName) } - davDoc := "" + dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded) - davDoc += dav.File(fileName, file.Bytes, file.Ended) - davDoc += "" - return []byte(davDoc), nil + + var buf bytes.Buffer + buf.WriteString("") + buf.WriteString(dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded)) + buf.WriteString(dav.File(fileName, file.Bytes, file.Ended)) + buf.WriteString("") + return buf.Bytes(), nil }