Allow accessing same filename differently

This commit is contained in:
Ben Sarmiento
2023-10-17 10:50:10 +02:00
parent da2c53bf86
commit c5f365c8b4
8 changed files with 124 additions and 60 deletions

View File

@@ -5,6 +5,7 @@ import (
"path/filepath" "path/filepath"
"github.com/debridmediamanager.com/zurg/pkg/dav" "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/realdebrid"
"github.com/debridmediamanager.com/zurg/pkg/repo" "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 // Create a map for O(1) lookups of the cached links
cachedLinksMap := make(map[string]*repo.DavFile) cachedLinksMap := make(map[string]*repo.DavFile)
for _, u := range davFiles.Files { for _, cached := range davFiles.Files {
cachedLinksMap[u.Link] = u cachedLinksMap[cached.Link] = cached
} }
for _, link := range torrent.Links { for _, link := range torrent.Links {
if u, exists := cachedLinksMap[link]; exists { if unrestrict, exists := cachedLinksMap[link]; exists {
if u.Filesize == 0 { if unrestrict.Filesize == 0 {
// This link is cached but the filesize is 0 // This link is cached but the filesize is 0
// This means that the link is dead // This means that the link is dead
continue continue
} }
path := filepath.Join(currentPath, u.Filename) filenameV2 := davextra.InsertLinkFragment(unrestrict.Filename, davextra.GetLinkFragment(unrestrict.Link))
path := filepath.Join(currentPath, filenameV2)
response := dav.File( response := dav.File(
path, path,
int(u.Filesize), int(unrestrict.Filesize),
torrent.Added, // Assuming you want to use the torrent added time here torrent.Added, // Assuming you want to use the torrent added time here
link,
) )
responses = append(responses, response) responses = append(responses, response)
} else { } else {
@@ -67,8 +70,8 @@ func createSingleTorrentResponse(torrent realdebrid.Torrent, db *repo.Database)
unrestrictFn := func() (realdebrid.UnrestrictResponse, error) { unrestrictFn := func() (realdebrid.UnrestrictResponse, error) {
return realdebrid.UnrestrictCheck(os.Getenv("RD_TOKEN"), link) return realdebrid.UnrestrictCheck(os.Getenv("RD_TOKEN"), link)
} }
unrestrictResponse := realdebrid.RetryUntilOk(unrestrictFn) unrestrict := realdebrid.RetryUntilOk(unrestrictFn)
if unrestrictResponse == nil { if unrestrict == nil {
db.Insert(torrent.Hash, torrent.Filename, realdebrid.UnrestrictResponse{ db.Insert(torrent.Hash, torrent.Filename, realdebrid.UnrestrictResponse{
Filename: "", Filename: "",
Filesize: 0, Filesize: 0,
@@ -77,21 +80,20 @@ func createSingleTorrentResponse(torrent realdebrid.Torrent, db *repo.Database)
}) })
continue continue
} else { } else {
db.Insert(torrent.Hash, torrent.Filename, *unrestrictResponse) db.Insert(torrent.Hash, torrent.Filename, *unrestrict)
} }
filenameV2 := davextra.InsertLinkFragment(unrestrict.Filename, davextra.GetLinkFragment(unrestrict.Link))
path := filepath.Join(currentPath, unrestrictResponse.Filename) path := filepath.Join(currentPath, filenameV2)
response := dav.File( response := dav.File(
path, path,
int(unrestrictResponse.Filesize), int(unrestrict.Filesize),
torrent.Added, torrent.Added,
link,
) )
responses = append(responses, response) responses = append(responses, response)
} }
} }
// TODO: dedupe the links in the response
return &dav.MultiStatus{ return &dav.MultiStatus{
XMLNS: "DAV:", XMLNS: "DAV:",
Response: responses, Response: responses,

View File

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

View File

@@ -12,7 +12,7 @@ func Directory(path string) Response {
} }
} }
func File(path string, fileSize int, added string) Response { func File(path string, fileSize int, added string, link string) Response {
return Response{ return Response{
Href: customPathEscape(path), Href: customPathEscape(path),
Propstat: PropStat{ Propstat: PropStat{
@@ -21,6 +21,7 @@ func File(path string, fileSize int, added string) Response {
IsHidden: 0, IsHidden: 0,
CreationDate: added, CreationDate: added,
LastModified: added, LastModified: added,
Link: link,
}, },
Status: "HTTP/1.1 200 OK", Status: "HTTP/1.1 200 OK",
}, },

View File

@@ -24,6 +24,8 @@ type Prop struct {
CreationDate string `xml:"d:creationdate"` CreationDate string `xml:"d:creationdate"`
LastModified string `xml:"d:getlastmodified"` LastModified string `xml:"d:getlastmodified"`
IsHidden int `xml:"d:ishidden"` IsHidden int `xml:"d:ishidden"`
Filename string `xml:"-"`
Link string `xml:"-"`
} }
type ResourceType struct { type ResourceType struct {

26
pkg/davextra/util.go Normal file
View File

@@ -0,0 +1,26 @@
package davextra
import (
"fmt"
"path/filepath"
"regexp"
"strings"
)
func GetLinkFragment(link string) string {
re := regexp.MustCompile(`\w+`)
matches := re.FindAllString(link, -1) // Returns all matches
var longestMatch string
for _, match := range matches {
if len(match) > len(longestMatch) {
longestMatch = match
}
}
return longestMatch[:4]
}
func InsertLinkFragment(filename, dupeID string) string {
ext := filepath.Ext(filename)
name := strings.TrimSuffix(filename, ext)
return fmt.Sprintf("%s DMM%s%s", name, dupeID, ext)
}

View File

@@ -88,7 +88,7 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
baseURL := "https://api.real-debrid.com/rest/1.0/torrents" baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
var allTorrents []Torrent var allTorrents []Torrent
page := 1 page := 1
limit := 10 limit := 2500
for { for {
params := url.Values{} params := url.Values{}
@@ -124,7 +124,7 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
allTorrents = append(allTorrents, torrents...) allTorrents = append(allTorrents, torrents...)
totalCountHeader := "10" // resp.Header.Get("x-total-count") totalCountHeader := resp.Header.Get("x-total-count")
totalCount, err := strconv.Atoi(totalCountHeader) totalCount, err := strconv.Atoi(totalCountHeader)
if err != nil { if err != nil {
break break
@@ -152,9 +152,9 @@ func deduplicateTorrents(torrents []Torrent) []Torrent {
} else { } else {
// If hash is different, delete old entry and create two new entries // If hash is different, delete old entry and create two new entries
delete(mappedTorrents, t.Filename) delete(mappedTorrents, t.Filename)
newKey1 := t.Filename + " - " + t.Hash[:4] newKey1 := fmt.Sprintf("%s - %s", t.Filename, t.Hash[:4])
newKey2 := existing.Filename + " - " + existing.Hash[:4]
mappedTorrents[newKey1] = t mappedTorrents[newKey1] = t
newKey2 := fmt.Sprintf("%s - %s", existing.Filename, existing.Hash[:4])
mappedTorrents[newKey2] = existing mappedTorrents[newKey2] = existing
} }
} else { } else {

View File

@@ -6,12 +6,11 @@ type FileJSON struct {
} }
type UnrestrictResponse struct { type UnrestrictResponse struct {
Filename string `json:"filename"` Filename string `json:"filename"`
Filesize int64 `json:"filesize"` Filesize int64 `json:"filesize"`
Link string `json:"link"` Link string `json:"link"`
Host string `json:"host"` Host string `json:"host"`
Download string `json:"download,omitempty"` Download string `json:"download,omitempty"`
Streamable int `json:"streamable,omitempty"`
} }
type Torrent struct { type Torrent struct {

View File

@@ -6,8 +6,13 @@ import (
"encoding/gob" "encoding/gob"
"fmt" "fmt"
"log" "log"
"net/url"
"path" "path"
"path/filepath"
"regexp"
"strings"
"github.com/debridmediamanager.com/zurg/pkg/davextra"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid" "github.com/debridmediamanager.com/zurg/pkg/realdebrid"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/qianbin/directcache" "github.com/qianbin/directcache"
@@ -19,8 +24,8 @@ type Database struct {
Cache *directcache.Cache Cache *directcache.Cache
} }
func GenerateID(directory, filename string) string { func GenerateID(segment1, segment2, segment3 string) string {
fullPath := path.Join(directory, filename) fullPath := path.Join(segment1, segment2, segment3)
hash := xxh3.HashString(fullPath) hash := xxh3.HashString(fullPath)
return fmt.Sprintf("%016x", hash) return fmt.Sprintf("%016x", hash)
} }
@@ -36,13 +41,14 @@ func NewDatabase(dsn string) (*Database, error) {
return &Database{Connection: db, Cache: cache}, nil return &Database{Connection: db, Cache: cache}, nil
} }
func (db *Database) Insert(parentHash, directory string, resp realdebrid.UnrestrictResponse) { func (db *Database) Insert(parentHash, torrentName string, resp realdebrid.UnrestrictResponse) {
// Generate the ID for the link // Generate the ID for the link
var id string var id string
if resp.Filename == "" { if resp.Filename == "" {
id = GenerateID(directory, resp.Link) // alternative ID for 404 links
id = GenerateID(parentHash, resp.Link, "")
} else { } else {
id = GenerateID(directory, resp.Filename) id = GenerateID(parentHash, resp.Filename, davextra.GetLinkFragment(resp.Link))
} }
// Check if the link already exists in the database // Check if the link already exists in the database
var exists int var exists int
@@ -58,7 +64,7 @@ func (db *Database) Insert(parentHash, directory string, resp realdebrid.Unrestr
VALUES (?, ?, ?, ?, ?, ?, ?)`, VALUES (?, ?, ?, ?, ?, ?, ?)`,
id, id,
parentHash, parentHash,
directory, torrentName,
resp.Filename, resp.Filename,
resp.Filesize, resp.Filesize,
resp.Link, resp.Link,
@@ -73,11 +79,12 @@ func (db *Database) Insert(parentHash, directory string, resp realdebrid.Unrestr
} }
} }
func (db *Database) Get(directory, filename string) (*DavFile, error) { func (db *Database) Get(parentHash, filename string) (*DavFile, error) {
id := GenerateID(directory, filename) filenameV2, linkFragment := extractIDFromFilename(filename)
id := GenerateID(parentHash, filenameV2, linkFragment)
data, ok := db.Cache.Get([]byte(id)) data, ok := db.Cache.Get([]byte(id))
if !ok { if !ok {
resp, err := fetchFromDatabaseByID(db.Connection, id) resp, err := fetchFromDatabaseByID(db.Connection, id, linkFragment)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -101,6 +108,26 @@ func (db *Database) Get(directory, filename string) (*DavFile, error) {
return &resp, nil return &resp, nil
} }
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]
}
func (db *Database) GetMultiple(parentHash string) (*DavFiles, error) { func (db *Database) GetMultiple(parentHash string) (*DavFiles, error) {
key := []byte(parentHash) key := []byte(parentHash)
data, ok := db.Cache.Get(key) data, ok := db.Cache.Get(key)
@@ -130,24 +157,22 @@ func (db *Database) GetMultiple(parentHash string) (*DavFiles, error) {
return &resps, nil return &resps, nil
} }
func fetchFromDatabaseByID(conn *sql.DB, id string) (*DavFile, error) { func fetchFromDatabaseByID(conn *sql.DB, id, linkFragment string) (*DavFile, error) {
log.Printf("fetching from database: %s", id) log.Printf("fetching from database: %s", id)
var resp DavFile var resp DavFile
err := conn.QueryRow(` query := `
SELECT Filename, Filesize, Link SELECT Filename, Filesize, Link
FROM Links WHERE ID = ?`, FROM Links WHERE ID = ? AND Link LIKE ?`
id, row := conn.QueryRow(query, id, "https://real-debrid.com/d/"+linkFragment+"%")
).Scan(
&resp.Filename, err := row.Scan(&resp.Filename, &resp.Filesize, &resp.Link)
&resp.Filesize,
&resp.Link,
)
if err != nil { if err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return &resp, nil return nil, nil
} }
log.Printf("failed to fetch record: %v", err) log.Printf("failed to fetch record: %v", err)
return nil, err
} }
return &resp, nil return &resp, nil