Proper caching
This commit is contained in:
@@ -4,14 +4,13 @@ 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/davextra"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
)
|
||||
|
||||
@@ -24,21 +23,27 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
||||
if len(segments) < 3 {
|
||||
log.Println("Invalid url", requestPath)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Get the last two segments
|
||||
torrentName := segments[len(segments)-2]
|
||||
filename := segments[len(segments)-1]
|
||||
|
||||
torrents := findAllTorrentsWithName(t, torrentName)
|
||||
if torrents == nil {
|
||||
log.Println("Cannot find directory", requestPath)
|
||||
log.Println("Cannot find torrent", torrentName)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
filename := segments[len(segments)-1]
|
||||
|
||||
filenameV2, linkFragment := extractIDFromFilename(filename)
|
||||
link := findLinkByFragment(torrents, linkFragment)
|
||||
filenameV2, linkFragment := davextra.ExtractLinkFragment(filename)
|
||||
link := findLinkByFragment(torrents, filenameV2, linkFragment)
|
||||
if link == "" {
|
||||
log.Println("Link not found")
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
|
||||
return realdebrid.UnrestrictLink(os.Getenv("RD_TOKEN"), link)
|
||||
@@ -52,38 +57,25 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
||||
}
|
||||
if resp.Filename != filenameV2 {
|
||||
// TODO: Redo the logic to handle mismatch
|
||||
// Filename mismatch
|
||||
// [SRS] Pokemon S22E01-35 1080p WEBRip AAC 2.0 x264 CC.rar
|
||||
// Pokemon.S22E24.The.Secret.Princess.DUBBED.1080p.WEBRip.AAC.2.0.x264-SRS.mkv
|
||||
// Filename mismatch
|
||||
// Adventure.Time.S06E10.Something.Big.1080p.HMAX.WEBRip.DD.2.0.H.265.-EDGE2020.mkv
|
||||
// Adventure.Time.S06E03.James.II.1080p.HMAX.WEBRip.DD.2.0.H.265.-EDGE2020.mkv
|
||||
// Action: schedule a "cleanup" job for the parent torrent
|
||||
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 []torrent.Torrent, fragment string) string {
|
||||
func findLinkByFragment(torrents []torrent.Torrent, filename, 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
|
||||
for _, file := range torrent.SelectedFiles {
|
||||
fname := filepath.Base(file.Path)
|
||||
if filename == fname && strings.HasPrefix(file.Link, fmt.Sprintf("https://real-debrid.com/d/%s", fragment)) {
|
||||
return file.Link
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user