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

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