103 lines
3.3 KiB
Go
103 lines
3.3 KiB
Go
package dav
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/debridmediamanager/zurg/internal/torrent"
|
|
"github.com/debridmediamanager/zurg/pkg/dav"
|
|
"github.com/debridmediamanager/zurg/pkg/logutil"
|
|
)
|
|
|
|
func HandleListDirectories(torMgr *torrent.TorrentManager) ([]byte, error) {
|
|
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">"
|
|
davDoc += dav.BaseDirectory("", "")
|
|
directories := torMgr.DirectoryMap.Keys()
|
|
sort.Strings(directories)
|
|
for _, directory := range directories {
|
|
if strings.HasPrefix(directory, "int__") {
|
|
continue
|
|
}
|
|
davDoc += dav.Directory(directory, "")
|
|
}
|
|
davDoc += "</d:multistatus>"
|
|
return []byte(davDoc), nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
|
buf.WriteString(dav.BaseDirectory(directory, ""))
|
|
var allTorrents []*torrent.Torrent
|
|
torrents.IterCb(func(_ string, tor *torrent.Torrent) {
|
|
if tor.AllInProgress() {
|
|
return
|
|
}
|
|
allTorrents = append(allTorrents, tor)
|
|
})
|
|
sort.Slice(allTorrents, func(i, j int) bool {
|
|
return allTorrents[i].AccessKey < allTorrents[j].AccessKey
|
|
})
|
|
for _, tor := range allTorrents {
|
|
buf.WriteString(dav.Directory(tor.AccessKey, tor.LatestAdded))
|
|
}
|
|
buf.WriteString("</d:multistatus>")
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
tor, ok := torrents.Get(torrentName)
|
|
if !ok {
|
|
return nil, fmt.Errorf("cannot find torrent %s", torrentName)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
|
buf.WriteString(dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded))
|
|
filenames := tor.SelectedFiles.Keys()
|
|
sort.Strings(filenames)
|
|
for _, filename := range filenames {
|
|
file, ok := tor.SelectedFiles.Get(filename)
|
|
if !ok || !strings.HasPrefix(file.Link, "http") {
|
|
continue
|
|
}
|
|
buf.WriteString(dav.File(filename, file.Bytes, file.Ended))
|
|
}
|
|
buf.WriteString("</d:multistatus>")
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
func HandlePropfindFile(directory, torrentName, fileName 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)
|
|
}
|
|
tor, ok := torrents.Get(torrentName)
|
|
if !ok {
|
|
return nil, fmt.Errorf("cannot find torrent %s", torrentName)
|
|
}
|
|
file, ok := tor.SelectedFiles.Get(fileName)
|
|
if !ok || !strings.HasPrefix(file.Link, "http") {
|
|
return nil, fmt.Errorf("cannot find file %s", fileName)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
|
buf.WriteString(dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded))
|
|
buf.WriteString(dav.File(fileName, file.Bytes, file.Ended))
|
|
buf.WriteString("</d:multistatus>")
|
|
return buf.Bytes(), nil
|
|
}
|