82 lines
2.3 KiB
Go
82 lines
2.3 KiB
Go
package dav
|
|
|
|
import (
|
|
"log"
|
|
"path/filepath"
|
|
|
|
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
|
"github.com/debridmediamanager.com/zurg/pkg/dav"
|
|
"github.com/debridmediamanager.com/zurg/pkg/davextra"
|
|
)
|
|
|
|
// createMultiTorrentResponse creates a WebDAV response for a list of torrents
|
|
func createMultiTorrentResponse(basePath string, torrents []torrent.Torrent) (*dav.MultiStatus, error) {
|
|
var responses []dav.Response
|
|
responses = append(responses, dav.Directory(basePath))
|
|
|
|
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, item.Name)
|
|
responses = append(responses, dav.Directory(path))
|
|
}
|
|
|
|
return &dav.MultiStatus{
|
|
XMLNS: "DAV:",
|
|
Response: responses,
|
|
}, nil
|
|
}
|
|
|
|
// createTorrentResponse creates a WebDAV response for a single torrent
|
|
// but it also handles the case where there are many torrents with the same name
|
|
func createSingleTorrentResponse(basePath string, torrents []torrent.Torrent, t *torrent.TorrentManager) (*dav.MultiStatus, error) {
|
|
var responses []dav.Response
|
|
// initial response is the directory itself
|
|
currentPath := filepath.Join(basePath, torrents[0].Name)
|
|
responses = append(responses, dav.Directory(currentPath))
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
var torrentResponses []dav.Response
|
|
for _, torrent := range torrents {
|
|
for _, file := range torrent.SelectedFiles {
|
|
if file.Link == "" {
|
|
log.Println("File has no link, skipping", file.Path)
|
|
// TODO: trigger a re-add for the file
|
|
// It is selected but no link is available
|
|
// I think this is handled on the manager side so we just need to refresh
|
|
t.RefreshInfo(torrent.ID)
|
|
continue
|
|
}
|
|
filename := filepath.Base(file.Path)
|
|
fragment := davextra.GetLinkFragment(file.Link)
|
|
filename = davextra.InsertLinkFragment(filename, fragment)
|
|
if _, exists := seen[filename]; exists {
|
|
continue
|
|
}
|
|
seen[filename] = true
|
|
filePath := filepath.Join(currentPath, filename)
|
|
torrentResponses = append(torrentResponses, dav.File(
|
|
filePath,
|
|
file.Bytes,
|
|
convertRFC3339toRFC1123(torrent.Added),
|
|
file.Link,
|
|
))
|
|
}
|
|
}
|
|
responses = append(responses, torrentResponses...)
|
|
|
|
return &dav.MultiStatus{
|
|
XMLNS: "DAV:",
|
|
Response: responses,
|
|
}, nil
|
|
}
|