Exclusive for dav endpoint
This commit is contained in:
138
internal/dav/dav.go
Normal file
138
internal/dav/dav.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package dav
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/debridmediamanager/zurg/internal/config"
|
||||
"github.com/debridmediamanager/zurg/internal/torrent"
|
||||
"github.com/debridmediamanager/zurg/internal/version"
|
||||
"github.com/debridmediamanager/zurg/pkg/dav"
|
||||
)
|
||||
|
||||
func ServeRootDirectoryForDav(torMgr *torrent.TorrentManager) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
||||
buf.WriteString(dav.BaseDirectory(addSlash(""), ""))
|
||||
directories := torMgr.DirectoryMap.Keys()
|
||||
sort.Strings(directories)
|
||||
for _, directory := range directories {
|
||||
if strings.HasPrefix(directory, "int__") {
|
||||
continue
|
||||
}
|
||||
buf.WriteString(dav.Directory(directory, ""))
|
||||
}
|
||||
buf.WriteString(dav.Directory(config.DOWNLOADS, ""))
|
||||
_, size := version.GetFile()
|
||||
buf.WriteString(dav.File(version.FILE, size, ""))
|
||||
buf.WriteString("</d:multistatus>")
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func ServeTorrentsListForDav(directory string, torMgr *torrent.TorrentManager) ([]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(addSlash(directory), ""))
|
||||
torrentNames := torrents.Keys()
|
||||
sort.Strings(torrentNames)
|
||||
for _, torrentName := range torrentNames {
|
||||
tor, ok := torrents.Get(torrentName)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
buf.WriteString(dav.Directory(torMgr.GetKey(tor), tor.Added))
|
||||
}
|
||||
buf.WriteString("</d:multistatus>")
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func ServeFilesListForDav(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]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)
|
||||
}
|
||||
|
||||
dirCfg := torMgr.Config.(*config.ZurgConfigV1).GetDirectoryConfig(directory)
|
||||
biggestFileSize := int64(0)
|
||||
if dirCfg.OnlyShowTheBiggestFile {
|
||||
biggestFileSize = tor.ComputeBiggestFileSize()
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
||||
buf.WriteString(dav.BaseDirectory(addSlash(filepath.Join(directory, torMgr.GetKey(tor))), tor.Added))
|
||||
filenames := tor.SelectedFiles.Keys()
|
||||
sort.Strings(filenames)
|
||||
for _, filename := range filenames {
|
||||
file, _ := tor.SelectedFiles.Get(filename)
|
||||
if file.State.Is("deleted_file") {
|
||||
continue
|
||||
}
|
||||
if file.State.Is("broken_file") && shouldHideBrokenTorrents {
|
||||
continue
|
||||
}
|
||||
if dirCfg.OnlyShowTheBiggestFile && file.Bytes < biggestFileSize {
|
||||
continue
|
||||
}
|
||||
if dirCfg.OnlyShowFilesWithSizeLte > 0 && file.Bytes > dirCfg.OnlyShowFilesWithSizeLte {
|
||||
continue
|
||||
}
|
||||
if dirCfg.OnlyShowFilesWithSizeGte > 0 && file.Bytes < dirCfg.OnlyShowFilesWithSizeGte {
|
||||
continue
|
||||
}
|
||||
buf.WriteString(dav.File(filename, file.Bytes, file.Ended))
|
||||
}
|
||||
buf.WriteString("</d:multistatus>")
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func HandleSingleFile(directory, torrentName, fileName string, torMgr *torrent.TorrentManager) ([]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 || file.State.Is("deleted_file") {
|
||||
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(addSlash(filepath.Join(directory, torMgr.GetKey(tor))), tor.Added))
|
||||
buf.WriteString(dav.File(fileName, file.Bytes, file.Ended))
|
||||
buf.WriteString("</d:multistatus>")
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
func ServeDownloadsListForDav(torMgr *torrent.TorrentManager) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">")
|
||||
buf.WriteString(dav.BaseDirectory(config.DOWNLOADS, ""))
|
||||
filenames := torMgr.DownloadMap.Keys()
|
||||
sort.Strings(filenames)
|
||||
for _, filename := range filenames {
|
||||
download, ok := torMgr.DownloadMap.Get(filename)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
buf.WriteString(dav.File(filename, download.Filesize, download.Generated))
|
||||
}
|
||||
buf.WriteString("</d:multistatus>")
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user