62 lines
1.4 KiB
Go
62 lines
1.4 KiB
Go
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 := "<ul>"
|
|
|
|
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, url.PathEscape(item.Name))
|
|
htmlDoc += fmt.Sprintf("<li><a href=\"%s/\">%s</a></li>", path, item.Name)
|
|
}
|
|
|
|
return htmlDoc, nil
|
|
}
|
|
|
|
func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent) (string, error) {
|
|
htmlDoc := "<ul>"
|
|
|
|
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)
|
|
if finalName[filename] {
|
|
// fragment := davextra.GetLinkFragment(file.Link)
|
|
// filename = davextra.InsertLinkFragment(filename, fragment)
|
|
continue
|
|
}
|
|
finalName[filename] = true
|
|
|
|
filePath := filepath.Join(currentPath, url.PathEscape(filename))
|
|
htmlDoc += fmt.Sprintf("<li><a href=\"%s\">%s</a></li>", filePath, filename)
|
|
}
|
|
}
|
|
|
|
return htmlDoc, nil
|
|
}
|