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,
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/debridmediamanager.com/zurg/pkg/repo"
|
||||
)
|
||||
|
||||
func findTorrentByFilename(torrents []realdebrid.Torrent, filename string) *realdebrid.Torrent {
|
||||
func findTorrentByName(torrents []realdebrid.Torrent, filename string) *realdebrid.Torrent {
|
||||
for _, torrent := range torrents {
|
||||
if torrent.Filename == filename {
|
||||
return &torrent
|
||||
@@ -41,9 +41,10 @@ func Router(mux *http.ServeMux, db *repo.Database) {
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
requestPath := path.Clean(r.URL.Path)
|
||||
|
||||
log.Println(r.Method, requestPath)
|
||||
|
||||
switch r.Method {
|
||||
case "PROPFIND":
|
||||
log.Println("PROPFIND", requestPath)
|
||||
var output []byte
|
||||
var err error
|
||||
|
||||
@@ -59,8 +60,8 @@ func Router(mux *http.ServeMux, db *repo.Database) {
|
||||
}
|
||||
output, err = xml.MarshalIndent(allTorrentsResponse, "", " ")
|
||||
} else {
|
||||
lastSegment := path.Base(requestPath)
|
||||
torrent := findTorrentByFilename(torrents, lastSegment)
|
||||
torrentName := path.Base(requestPath)
|
||||
torrent := findTorrentByName(torrents, torrentName)
|
||||
if torrent == nil {
|
||||
log.Println("Cannot find directory")
|
||||
http.Error(w, "Cannot find directory", http.StatusNotFound)
|
||||
@@ -88,33 +89,41 @@ func Router(mux *http.ServeMux, db *repo.Database) {
|
||||
fmt.Fprintf(w, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n%s\n", output)
|
||||
|
||||
case http.MethodOptions:
|
||||
log.Println("OPTIONS", requestPath)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
case http.MethodGet:
|
||||
log.Println("GET", requestPath)
|
||||
segments := strings.Split(requestPath, "/")
|
||||
|
||||
// If there are less than 2 segments, return an error or adjust as needed
|
||||
if len(segments) < 2 {
|
||||
// If there are less than 3 segments, return an error or adjust as needed
|
||||
if len(segments) < 3 {
|
||||
log.Println("Cannot find file")
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
}
|
||||
|
||||
// Get the last two segments
|
||||
secondLast := segments[len(segments)-2]
|
||||
last := segments[len(segments)-1]
|
||||
unrestrict, dbErr := db.Get(secondLast, last)
|
||||
torrentName := segments[len(segments)-2]
|
||||
torrent := findTorrentByName(torrents, torrentName)
|
||||
if torrent == nil {
|
||||
log.Println("Cannot find directory")
|
||||
http.Error(w, "Cannot find directory", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
filename := segments[len(segments)-1]
|
||||
unrestrict, dbErr := db.Get(torrent.Hash, filename)
|
||||
if dbErr != nil {
|
||||
log.Printf("Cannot find file in db: %v", dbErr.Error())
|
||||
http.Error(w, fmt.Sprintf("Cannot find file in db: %v", dbErr.Error()), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := realdebrid.UnrestrictLink(os.Getenv("RD_TOKEN"), unrestrict.Link)
|
||||
if err != nil {
|
||||
unrestrictFn := func() (realdebrid.UnrestrictResponse, error) {
|
||||
return realdebrid.UnrestrictLink(os.Getenv("RD_TOKEN"), unrestrict.Link)
|
||||
}
|
||||
resp := realdebrid.RetryUntilOk(unrestrictFn)
|
||||
if resp == nil {
|
||||
// TODO: Delete the link from the database
|
||||
log.Printf("Cannot unrestrict link: %v", err.Error())
|
||||
http.Error(w, fmt.Sprintf("Cannot unrestrict link: %v", err.Error()), http.StatusInternalServerError)
|
||||
http.Error(w, fmt.Sprintf("Cannot unrestrict link: %v", err.Error()), http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, resp.Download, http.StatusFound)
|
||||
|
||||
Reference in New Issue
Block a user