Finalize repair
This commit is contained in:
@@ -1,95 +0,0 @@
|
||||
package dav
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/config"
|
||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/davextra"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
"github.com/hashicorp/golang-lru/v2/expirable"
|
||||
)
|
||||
|
||||
// HandleGetRequest handles a GET request to a file
|
||||
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) {
|
||||
requestPath := path.Clean(r.URL.Path)
|
||||
if requestPath == "/favicon.ico" {
|
||||
return
|
||||
}
|
||||
segments := strings.Split(requestPath, "/")
|
||||
// If there are less than 3 segments, return an error or adjust as needed
|
||||
if len(segments) < 4 {
|
||||
// log.Println("Invalid url", requestPath)
|
||||
// http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
HandlePropfindRequest(w, r, t, c, cache)
|
||||
return
|
||||
}
|
||||
if data, exists := cache.Get(requestPath); exists {
|
||||
http.Redirect(w, r, data, http.StatusFound)
|
||||
return
|
||||
}
|
||||
// Get the last two segments
|
||||
baseDirectory := segments[len(segments)-3]
|
||||
torrentName := segments[len(segments)-2]
|
||||
filename := segments[len(segments)-1]
|
||||
|
||||
torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName)
|
||||
if torrents == nil {
|
||||
log.Println("Cannot find torrent", requestPath)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
filenameV2, linkFragment := davextra.ExtractLinkFragment(filename)
|
||||
_, file := getFile(torrents, filenameV2, linkFragment)
|
||||
if file == nil {
|
||||
log.Println("Cannot find file", requestPath)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if file.Link == "" {
|
||||
log.Println("Link not found", filename)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
link := file.Link
|
||||
|
||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
||||
return realdebrid.UnrestrictLink(c.GetToken(), link)
|
||||
}
|
||||
resp := realdebrid.RetryUntilOk(unrestrictFn)
|
||||
if resp == nil {
|
||||
log.Println("Cannot unrestrict link", link, filenameV2)
|
||||
// t.HideTheFile(torrent, file)
|
||||
http.Redirect(w, r, "https://send.nukes.wtf/tDeTd0", http.StatusFound)
|
||||
return
|
||||
}
|
||||
if resp.Filename != filenameV2 {
|
||||
actualExt := filepath.Ext(resp.Filename)
|
||||
expectedExt := filepath.Ext(filenameV2)
|
||||
if actualExt != expectedExt {
|
||||
log.Println("File extension mismatch", resp.Filename, filenameV2)
|
||||
} else {
|
||||
log.Println("Filename mismatch", resp.Filename, filenameV2)
|
||||
}
|
||||
}
|
||||
cache.Add(requestPath, resp.Download)
|
||||
http.Redirect(w, r, resp.Download, http.StatusFound)
|
||||
}
|
||||
|
||||
// getFile finds a link by a fragment, it might be wrong
|
||||
func getFile(torrents []torrent.Torrent, filename, fragment string) (*torrent.Torrent, *torrent.File) {
|
||||
for t := range torrents {
|
||||
for f, file := range torrents[t].SelectedFiles {
|
||||
fname := filepath.Base(file.Path)
|
||||
if filename == fname && strings.Contains(file.Link, fragment) {
|
||||
return &torrents[t], &torrents[t].SelectedFiles[f]
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
@@ -38,6 +38,7 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
|
||||
case len(filteredSegments) == 2:
|
||||
output, err = handleSingleTorrent(requestPath, w, r, t)
|
||||
default:
|
||||
log.Println("Not Found", r.Method, requestPath)
|
||||
writeHTTPError(w, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
@@ -105,6 +106,5 @@ func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
|
||||
func writeHTTPError(w http.ResponseWriter, errorMessage string, statusCode int) {
|
||||
log.Println(errorMessage)
|
||||
http.Error(w, errorMessage, statusCode)
|
||||
}
|
||||
Reference in New Issue
Block a user