A lot of rewrite here
This commit is contained in:
91
internal/dav/getfile.go
Normal file
91
internal/dav/getfile.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package dav
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/internal/torrent"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
)
|
||||
|
||||
// HandleGetRequest handles a GET request to a file
|
||||
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) {
|
||||
requestPath := path.Clean(r.URL.Path)
|
||||
|
||||
segments := strings.Split(requestPath, "/")
|
||||
// If there are less than 3 segments, return an error or adjust as needed
|
||||
if len(segments) < 3 {
|
||||
log.Println("Invalid url", requestPath)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
}
|
||||
|
||||
// Get the last two segments
|
||||
torrentName := segments[len(segments)-2]
|
||||
torrents := findAllTorrentsWithName(t, torrentName)
|
||||
if torrents == nil {
|
||||
log.Println("Cannot find directory", requestPath)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
filename := segments[len(segments)-1]
|
||||
|
||||
filenameV2, linkFragment := extractIDFromFilename(filename)
|
||||
link := findLinkByFragment(torrents, linkFragment)
|
||||
|
||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
||||
return realdebrid.UnrestrictLink(os.Getenv("RD_TOKEN"), link)
|
||||
}
|
||||
resp := realdebrid.RetryUntilOk(unrestrictFn)
|
||||
if resp == nil {
|
||||
// TODO: Delete the link from the database
|
||||
log.Println("Cannot unrestrict link")
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if resp.Filename != filenameV2 {
|
||||
// TODO: Redo the logic to handle mismatch
|
||||
log.Println("Filename mismatch", resp.Filename, filenameV2)
|
||||
}
|
||||
http.Redirect(w, r, resp.Download, http.StatusFound)
|
||||
}
|
||||
|
||||
// extractIDFromFilename extracts the link ID from a filename
|
||||
func extractIDFromFilename(filename string) (string, string) {
|
||||
filenameV2, err := url.PathUnescape(filename)
|
||||
if err != nil {
|
||||
filenameV2 = filename
|
||||
}
|
||||
ext := filepath.Ext(filenameV2)
|
||||
name := strings.TrimSuffix(filenameV2, ext)
|
||||
|
||||
r := regexp.MustCompile(`\sDMM(\w+)`)
|
||||
matches := r.FindStringSubmatch(name)
|
||||
if len(matches) < 2 {
|
||||
// No ID found
|
||||
return filenameV2, ""
|
||||
}
|
||||
|
||||
// Remove ID from filename
|
||||
originalName := strings.Replace(name, matches[0], "", 1)
|
||||
return originalName + ext, matches[1]
|
||||
}
|
||||
|
||||
// findLinkByFragment finds a link by a fragment, it might be wrong
|
||||
func findLinkByFragment(torrents []realdebrid.Torrent, fragment string) string {
|
||||
for _, torrent := range torrents {
|
||||
for _, link := range torrent.Links {
|
||||
if strings.HasPrefix(link, fmt.Sprintf("https://real-debrid.com/d/%s", fragment)) {
|
||||
return link
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user