A lot of rewrite here

This commit is contained in:
Ben Sarmiento
2023-10-18 00:17:07 +02:00
parent 44216343e2
commit 0886c93250
14 changed files with 399 additions and 482 deletions

View File

@@ -1,26 +1,29 @@
package dav
import (
"os"
"fmt"
"path/filepath"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/dav"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
"github.com/debridmediamanager.com/zurg/pkg/repo"
)
// createMultiTorrentResponse creates a WebDAV response for a list of torrents
func createMultiTorrentResponse(torrents []realdebrid.Torrent) (*dav.MultiStatus, error) {
var responses []dav.Response
// initial response is the directory itself
responses = append(responses, dav.Directory("/torrents"))
// add all files and directories in the directory
seen := make(map[string]bool)
for _, item := range torrents {
if item.Progress != 100 {
continue
}
if _, exists := seen[item.Filename]; exists {
continue
}
seen[item.Filename] = true
path := filepath.Join("/torrents", item.Filename)
responses = append(responses, dav.Directory(path))
@@ -32,66 +35,76 @@ func createMultiTorrentResponse(torrents []realdebrid.Torrent) (*dav.MultiStatus
}, nil
}
func createSingleTorrentResponse(torrent realdebrid.Torrent, db *repo.Database) (*dav.MultiStatus, error) {
// createTorrentResponse creates a WebDAV response for torrents with the same name
func createCombinedTorrentResponse(torrents []realdebrid.Torrent, t *torrent.TorrentManager) (*dav.MultiStatus, error) {
var responses []dav.Response
// initial response is the directory itself
currentPath := filepath.Join("/torrents", torrent.Filename)
currentPath := filepath.Join("/torrents", torrents[0].Filename)
responses = append(responses, dav.Directory(currentPath))
davFiles, err := db.GetMultiple(torrent.Hash)
if err != nil {
return nil, err
}
seen := make(map[string]bool)
idx := 0
// Create a map for O(1) lookups of the cached links
cachedLinksMap := make(map[string]*repo.DavFile)
for _, cached := range davFiles.Files {
cachedLinksMap[cached.Link] = cached
}
for _, link := range torrent.Links {
if unrestrict, exists := cachedLinksMap[link]; exists {
if unrestrict.Filesize == 0 {
// This link is cached but the filesize is 0
// This means that the link is dead
var torrentResponses []dav.Response
for _, torrent := range torrents {
info := t.GetInfo(torrent.ID)
if info == nil {
continue
}
var selectedFiles []realdebrid.File
for _, file := range info.Files {
if file.Selected == 0 {
continue
}
filenameV2 := davextra.InsertLinkFragment(unrestrict.Filename, davextra.GetLinkFragment(unrestrict.Link))
path := filepath.Join(currentPath, filenameV2)
response := dav.File(
path,
unrestrict.Filesize,
convertDate(torrent.Added),
link,
)
responses = append(responses, response)
selectedFiles = append(selectedFiles, file)
}
if len(selectedFiles) != len(info.Links) {
fmt.Println("Links and files do not match", info.Filename)
// TODO: Add auto-healing for this
// for _, link := range info.Links {
// unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
// return realdebrid.UnrestrictCheck(os.Getenv("RD_TOKEN"), link)
// }
// resp := realdebrid.RetryUntilOk(unrestrictFn)
// if resp == nil {
// continue
// } else {
// if _, exists := seen[resp.Filename]; exists {
// continue
// }
// seen[resp.Filename] = true
// filePath := filepath.Join(currentPath, resp.Filename)
// torrentResponses = append(torrentResponses,
// dav.File(
// filePath,
// resp.Filesize,
// info.Added,
// resp.Link,
// ),
// )
// }
// }
} else {
// This link is not cached yet
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
return realdebrid.UnrestrictCheck(os.Getenv("RD_TOKEN"), link)
for _, file := range selectedFiles {
filename := filepath.Base(file.Path)
if _, exists := seen[filename]; exists {
continue
}
seen[filename] = true
filePath := filepath.Join(currentPath, filename)
torrentResponses = append(torrentResponses, dav.File(
filePath,
file.Bytes,
convertDate(info.Added),
info.Links[idx],
))
idx++
}
unrestrict := realdebrid.RetryUntilOk(unrestrictFn)
if unrestrict == nil {
db.Insert(torrent.Hash, torrent.Filename, realdebrid.UnrestrictResponse{
Filename: "",
Filesize: 0,
Link: link,
Host: "",
})
continue
}
db.Insert(torrent.Hash, torrent.Filename, *unrestrict)
filenameV2 := davextra.InsertLinkFragment(unrestrict.Filename, davextra.GetLinkFragment(unrestrict.Link))
path := filepath.Join(currentPath, filenameV2)
response := dav.File(
path,
unrestrict.Filesize,
convertDate(torrent.Added),
link,
)
responses = append(responses, response)
}
}
responses = append(responses, torrentResponses...)
return &dav.MultiStatus{
XMLNS: "DAV:",