package http
import (
"fmt"
"path/filepath"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
)
// 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.Name]; exists {
continue
}
seen[item.Name] = true
path := filepath.Join(basePath, item.Name)
htmlDoc += fmt.Sprintf("- %s
", path, item.Name)
}
return htmlDoc, nil
}
func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (string, error) {
htmlDoc := ""
nameAndLink := make(map[string]bool)
finalName := make(map[string]bool)
currentPath := filepath.Join(basePath)
for _, torrent := range torrents {
for _, file := range torrent.SelectedFiles {
if file.Link == "" {
// log.Println("File has no link, skipping", file.Path)
continue
}
filename := filepath.Base(file.Path)
key := filename + file.Link
if nameAndLink[key] {
continue
}
nameAndLink[key] = true
if finalName[filename] {
fragment := davextra.GetLinkFragment(file.Link)
filename = davextra.InsertLinkFragment(filename, fragment)
}
finalName[filename] = true
filePath := filepath.Join(currentPath, filename)
htmlDoc += fmt.Sprintf("- %s
", filePath, filename)
}
}
return htmlDoc, nil
}