package http
import (
"fmt"
"net/url"
"path/filepath"
"github.com/debridmediamanager.com/zurg/internal/torrent"
)
// createMultiTorrentResponse creates a WebDAV response for a list of torrents
func createMultiTorrentResponse(basePath string, torrents []torrent.Torrent) (string, error) {
htmlDoc := "
"
seen := make(map[string]bool)
for _, item := range torrents {
if item.Progress != 100 {
continue
}
if _, exists := seen[item.AccessKey]; exists {
continue
}
seen[item.AccessKey] = true
path := filepath.Join(basePath, url.PathEscape(item.AccessKey))
htmlDoc += fmt.Sprintf("- %s
", path, item.AccessKey)
}
return htmlDoc, nil
}
func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (string, error) {
htmlDoc := ""
finalName := make(map[string]bool)
currentPath := filepath.Join(basePath)
for _, torrent := range torrents {
for _, file := range torrent.SelectedFiles {
if file.Link == "" {
// TODO: fix the file?
// log.Println("File has no link, skipping", file.Path)
continue
}
filename := filepath.Base(file.Path)
if finalName[filename] {
continue
}
finalName[filename] = true
filePath := filepath.Join(currentPath, url.PathEscape(filename))
htmlDoc += fmt.Sprintf("- %s
", filePath, filename)
}
}
return htmlDoc, nil
}