Allow accessing same filename differently
This commit is contained in:
@@ -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