Allow accessing same filename differently
This commit is contained in:
@@ -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{
|
||||
Href: customPathEscape(path),
|
||||
Propstat: PropStat{
|
||||
@@ -21,6 +21,7 @@ func File(path string, fileSize int, added string) Response {
|
||||
IsHidden: 0,
|
||||
CreationDate: added,
|
||||
LastModified: added,
|
||||
Link: link,
|
||||
},
|
||||
Status: "HTTP/1.1 200 OK",
|
||||
},
|
||||
|
||||
@@ -24,6 +24,8 @@ type Prop struct {
|
||||
CreationDate string `xml:"d:creationdate"`
|
||||
LastModified string `xml:"d:getlastmodified"`
|
||||
IsHidden int `xml:"d:ishidden"`
|
||||
Filename string `xml:"-"`
|
||||
Link string `xml:"-"`
|
||||
}
|
||||
|
||||
type ResourceType struct {
|
||||
|
||||
26
pkg/davextra/util.go
Normal file
26
pkg/davextra/util.go
Normal 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)
|
||||
}
|
||||
@@ -88,7 +88,7 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
|
||||
baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
|
||||
var allTorrents []Torrent
|
||||
page := 1
|
||||
limit := 10
|
||||
limit := 2500
|
||||
|
||||
for {
|
||||
params := url.Values{}
|
||||
@@ -124,7 +124,7 @@ func GetTorrents(accessToken string) ([]Torrent, error) {
|
||||
|
||||
allTorrents = append(allTorrents, torrents...)
|
||||
|
||||
totalCountHeader := "10" // resp.Header.Get("x-total-count")
|
||||
totalCountHeader := resp.Header.Get("x-total-count")
|
||||
totalCount, err := strconv.Atoi(totalCountHeader)
|
||||
if err != nil {
|
||||
break
|
||||
@@ -152,9 +152,9 @@ func deduplicateTorrents(torrents []Torrent) []Torrent {
|
||||
} else {
|
||||
// If hash is different, delete old entry and create two new entries
|
||||
delete(mappedTorrents, t.Filename)
|
||||
newKey1 := t.Filename + " - " + t.Hash[:4]
|
||||
newKey2 := existing.Filename + " - " + existing.Hash[:4]
|
||||
newKey1 := fmt.Sprintf("%s - %s", t.Filename, t.Hash[:4])
|
||||
mappedTorrents[newKey1] = t
|
||||
newKey2 := fmt.Sprintf("%s - %s", existing.Filename, existing.Hash[:4])
|
||||
mappedTorrents[newKey2] = existing
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -6,12 +6,11 @@ type FileJSON struct {
|
||||
}
|
||||
|
||||
type UnrestrictResponse struct {
|
||||
Filename string `json:"filename"`
|
||||
Filesize int64 `json:"filesize"`
|
||||
Link string `json:"link"`
|
||||
Host string `json:"host"`
|
||||
Download string `json:"download,omitempty"`
|
||||
Streamable int `json:"streamable,omitempty"`
|
||||
Filename string `json:"filename"`
|
||||
Filesize int64 `json:"filesize"`
|
||||
Link string `json:"link"`
|
||||
Host string `json:"host"`
|
||||
Download string `json:"download,omitempty"`
|
||||
}
|
||||
|
||||
type Torrent struct {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user