84 lines
2.5 KiB
Go
84 lines
2.5 KiB
Go
package dav
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/debridmediamanager/zurg/internal/torrent"
|
|
"github.com/debridmediamanager/zurg/pkg/dav"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func HandleListDirectories(t *torrent.TorrentManager) (*string, error) {
|
|
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">"
|
|
// initial response is the directory itself
|
|
davDoc += dav.BaseDirectory("", "")
|
|
|
|
directories := t.DirectoryMap.Keys()
|
|
sort.Strings(directories)
|
|
for _, directory := range directories {
|
|
if strings.HasPrefix(directory, "int__") {
|
|
continue
|
|
}
|
|
davDoc += dav.Directory(directory, "")
|
|
}
|
|
|
|
davDoc += "</d:multistatus>"
|
|
return &davDoc, nil
|
|
}
|
|
|
|
func HandleListTorrents(directory string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
|
|
_, ok := t.DirectoryMap.Get(directory)
|
|
if !ok {
|
|
return nil, fmt.Errorf("cannot find directory %s", directory)
|
|
}
|
|
|
|
if resp, ok := t.ResponseCache.Get(directory + ".dav"); !ok {
|
|
log.Debugf("Generating xml for directory %s", directory)
|
|
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">"
|
|
davDoc += dav.Directory("", "")
|
|
directories := t.DirectoryMap.Keys()
|
|
sort.Strings(directories)
|
|
for _, directory := range directories {
|
|
davDoc += dav.Directory(directory, "")
|
|
}
|
|
davDoc += "</d:multistatus>"
|
|
return &davDoc, nil
|
|
} else {
|
|
davDoc := resp.(*string)
|
|
return davDoc, nil
|
|
}
|
|
}
|
|
|
|
func HandleListFiles(directory, torrentName string, t *torrent.TorrentManager, log *zap.SugaredLogger) (*string, error) {
|
|
torrents, ok := t.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)
|
|
}
|
|
|
|
if resp, ok := t.ResponseCache.Get(directory + "/" + torrentName + ".dav"); !ok {
|
|
log.Debugf("Generating xml for torrent %s", torrentName)
|
|
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" + dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded)
|
|
filenames := tor.SelectedFiles.Keys()
|
|
sort.Strings(filenames)
|
|
for _, filename := range filenames {
|
|
file, _ := tor.SelectedFiles.Get(filename)
|
|
if file == nil || !strings.HasPrefix(file.Link, "http") {
|
|
continue
|
|
}
|
|
davDoc += dav.File(filename, file.Bytes, file.Ended)
|
|
}
|
|
davDoc += "</d:multistatus>"
|
|
return &davDoc, nil
|
|
} else {
|
|
davDoc := resp.(*string)
|
|
return davDoc, nil
|
|
}
|
|
}
|