Allow accessing same filename differently

This commit is contained in:
Ben Sarmiento
2023-10-17 10:50:10 +02:00
parent da2c53bf86
commit c5f365c8b4
8 changed files with 124 additions and 60 deletions

View File

@@ -5,6 +5,7 @@ import (
"path/filepath"
"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"
)
@@ -45,21 +46,23 @@ func createSingleTorrentResponse(torrent realdebrid.Torrent, db *repo.Database)
// Create a map for O(1) lookups of the cached links
cachedLinksMap := make(map[string]*repo.DavFile)
for _, u := range davFiles.Files {
cachedLinksMap[u.Link] = u
for _, cached := range davFiles.Files {
cachedLinksMap[cached.Link] = cached
}
for _, link := range torrent.Links {
if u, exists := cachedLinksMap[link]; exists {
if u.Filesize == 0 {
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
continue
}
path := filepath.Join(currentPath, u.Filename)
filenameV2 := davextra.InsertLinkFragment(unrestrict.Filename, davextra.GetLinkFragment(unrestrict.Link))
path := filepath.Join(currentPath, filenameV2)
response := dav.File(
path,
int(u.Filesize),
int(unrestrict.Filesize),
torrent.Added, // Assuming you want to use the torrent added time here
link,
)
responses = append(responses, response)
} else {
@@ -67,8 +70,8 @@ func createSingleTorrentResponse(torrent realdebrid.Torrent, db *repo.Database)
unrestrictFn := func() (realdebrid.UnrestrictResponse, error) {
return realdebrid.UnrestrictCheck(os.Getenv("RD_TOKEN"), link)
}
unrestrictResponse := realdebrid.RetryUntilOk(unrestrictFn)
if unrestrictResponse == nil {
unrestrict := realdebrid.RetryUntilOk(unrestrictFn)
if unrestrict == nil {
db.Insert(torrent.Hash, torrent.Filename, realdebrid.UnrestrictResponse{
Filename: "",
Filesize: 0,
@@ -77,21 +80,20 @@ func createSingleTorrentResponse(torrent realdebrid.Torrent, db *repo.Database)
})
continue
} else {
db.Insert(torrent.Hash, torrent.Filename, *unrestrictResponse)
db.Insert(torrent.Hash, torrent.Filename, *unrestrict)
}
path := filepath.Join(currentPath, unrestrictResponse.Filename)
filenameV2 := davextra.InsertLinkFragment(unrestrict.Filename, davextra.GetLinkFragment(unrestrict.Link))
path := filepath.Join(currentPath, filenameV2)
response := dav.File(
path,
int(unrestrictResponse.Filesize),
int(unrestrict.Filesize),
torrent.Added,
link,
)
responses = append(responses, response)
}
}
// TODO: dedupe the links in the response
return &dav.MultiStatus{
XMLNS: "DAV:",
Response: responses,