Allow accessing same filename differently
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user