Add rename

This commit is contained in:
Ben Sarmiento
2023-12-10 21:00:02 +01:00
parent e46da8c589
commit 9e181c2b19
3 changed files with 130 additions and 9 deletions

View File

@@ -11,7 +11,7 @@ import (
"github.com/debridmediamanager/zurg/pkg/logutil"
)
func HandleListDirectories(torMgr *torrent.TorrentManager) (*string, error) {
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()
@@ -23,10 +23,10 @@ func HandleListDirectories(torMgr *torrent.TorrentManager) (*string, error) {
davDoc += dav.Directory(directory, "")
}
davDoc += "</d:multistatus>"
return &davDoc, nil
return []byte(davDoc), nil
}
func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *logutil.Logger) (*string, error) {
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)
@@ -48,10 +48,10 @@ func HandleListTorrents(directory string, torMgr *torrent.TorrentManager, log *l
davDoc += dav.Directory(tor.AccessKey, tor.LatestAdded)
}
davDoc += "</d:multistatus>"
return &davDoc, nil
return []byte(davDoc), nil
}
func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManager, log *logutil.Logger) (*string, error) {
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)
@@ -72,5 +72,24 @@ func HandleListFiles(directory, torrentName string, torMgr *torrent.TorrentManag
davDoc += dav.File(filename, file.Bytes, file.Ended)
}
davDoc += "</d:multistatus>"
return &davDoc, nil
return []byte(davDoc), 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)
}
davDoc := "<?xml version=\"1.0\" encoding=\"utf-8\"?><d:multistatus xmlns:d=\"DAV:\">" + dav.BaseDirectory(filepath.Join(directory, tor.AccessKey), tor.LatestAdded)
davDoc += dav.File(fileName, file.Bytes, file.Ended)
davDoc += "</d:multistatus>"
return []byte(davDoc), nil
}

41
internal/dav/rename.go Normal file
View File

@@ -0,0 +1,41 @@
package dav
import (
"fmt"
"github.com/debridmediamanager/zurg/internal/torrent"
)
func HandleRenameTorrent(directory, torrentName, newName string, torMgr *torrent.TorrentManager) error {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return fmt.Errorf("cannot find directory %s", directory)
}
torrent, ok := torrents.Get(torrentName)
if !ok {
return fmt.Errorf("cannot find torrent %s", torrentName)
}
torrents.Remove(torrentName)
torrents.Set(newName, torrent)
torrent.AccessKey = newName
return nil
}
func HandleRenameFile(directory, torrentName, fileName, newName string, torMgr *torrent.TorrentManager) error {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return fmt.Errorf("cannot find directory %s", directory)
}
torrent, ok := torrents.Get(torrentName)
if !ok {
return fmt.Errorf("cannot find torrent %s", torrentName)
}
file, ok := torrent.SelectedFiles.Get(fileName)
if !ok {
return fmt.Errorf("cannot find file %s", fileName)
}
torrent.SelectedFiles.Remove(torrentName)
torrent.SelectedFiles.Set(newName, file)
file.Path = newName
return nil
}