Initial commit 🌈

This commit is contained in:
Ben Sarmiento
2023-10-16 21:31:51 +02:00
commit da2c53bf86
15 changed files with 802 additions and 0 deletions

28
pkg/dav/response.go Normal file
View File

@@ -0,0 +1,28 @@
package dav
func Directory(path string) Response {
return Response{
Href: customPathEscape(path),
Propstat: PropStat{
Prop: Prop{
ResourceType: ResourceType{Collection: &struct{}{}},
},
Status: "HTTP/1.1 200 OK",
},
}
}
func File(path string, fileSize int, added string) Response {
return Response{
Href: customPathEscape(path),
Propstat: PropStat{
Prop: Prop{
ContentLength: fileSize,
IsHidden: 0,
CreationDate: added,
LastModified: added,
},
Status: "HTTP/1.1 200 OK",
},
}
}

31
pkg/dav/types.go Normal file
View File

@@ -0,0 +1,31 @@
package dav
import "encoding/xml"
type MultiStatus struct {
XMLName xml.Name `xml:"d:multistatus"`
XMLNS string `xml:"xmlns:d,attr"`
Response []Response `xml:"d:response"`
}
type Response struct {
Href string `xml:"d:href"`
Propstat PropStat `xml:"d:propstat"`
}
type PropStat struct {
Prop Prop `xml:"d:prop"`
Status string `xml:"d:status"`
}
type Prop struct {
ResourceType ResourceType `xml:"d:resourcetype"`
ContentLength int `xml:"d:getcontentlength"`
CreationDate string `xml:"d:creationdate"`
LastModified string `xml:"d:getlastmodified"`
IsHidden int `xml:"d:ishidden"`
}
type ResourceType struct {
Collection *struct{} `xml:"d:collection,omitempty"`
}

14
pkg/dav/util.go Normal file
View File

@@ -0,0 +1,14 @@
package dav
import (
"net/url"
"strings"
)
func customPathEscape(input string) string {
segments := strings.Split(input, "/")
for i, segment := range segments {
segments[i] = url.PathEscape(segment)
}
return strings.Join(segments, "/")
}

172
pkg/realdebrid/api.go Normal file
View File

@@ -0,0 +1,172 @@
package realdebrid
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
)
func UnrestrictCheck(accessToken, link string) (UnrestrictResponse, error) {
data := url.Values{}
data.Set("link", link)
req, err := http.NewRequest("POST", "https://api.real-debrid.com/rest/1.0/unrestrict/check", bytes.NewBufferString(data.Encode()))
if err != nil {
return UnrestrictResponse{}, err
}
req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return UnrestrictResponse{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return UnrestrictResponse{}, err
}
if resp.StatusCode != http.StatusOK {
return UnrestrictResponse{}, fmt.Errorf("HTTP error: %s", resp.Status)
}
var response UnrestrictResponse
err = json.Unmarshal(body, &response)
if err != nil {
return UnrestrictResponse{}, err
}
return response, nil
}
func UnrestrictLink(accessToken, link string) (UnrestrictResponse, error) {
data := url.Values{}
data.Set("link", link)
req, err := http.NewRequest("POST", "https://api.real-debrid.com/rest/1.0/unrestrict/link", bytes.NewBufferString(data.Encode()))
if err != nil {
return UnrestrictResponse{}, err
}
req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return UnrestrictResponse{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return UnrestrictResponse{}, err
}
if resp.StatusCode != http.StatusOK {
return UnrestrictResponse{}, fmt.Errorf("HTTP error: %s", resp.Status)
}
var response UnrestrictResponse
err = json.Unmarshal(body, &response)
if err != nil {
return UnrestrictResponse{}, err
}
return response, nil
}
func GetTorrents(accessToken string) ([]Torrent, error) {
baseURL := "https://api.real-debrid.com/rest/1.0/torrents"
var allTorrents []Torrent
page := 1
limit := 10
for {
params := url.Values{}
params.Set("page", fmt.Sprintf("%d", page))
params.Set("limit", fmt.Sprintf("%d", limit))
reqURL := baseURL + "?" + params.Encode()
req, err := http.NewRequest("GET", reqURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+accessToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP error: %s", resp.Status)
}
var torrents []Torrent
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&torrents)
if err != nil {
return nil, err
}
allTorrents = append(allTorrents, torrents...)
totalCountHeader := "10" // resp.Header.Get("x-total-count")
totalCount, err := strconv.Atoi(totalCountHeader)
if err != nil {
break
}
if len(torrents) < limit || len(allTorrents) >= totalCount {
break
}
page++
}
return deduplicateTorrents(allTorrents), nil
}
func deduplicateTorrents(torrents []Torrent) []Torrent {
mappedTorrents := make(map[string]Torrent)
for _, t := range torrents {
if existing, ok := mappedTorrents[t.Filename]; ok {
if existing.Hash == t.Hash {
// If hash is the same, combine the links
existing.Links = append(existing.Links, t.Links...)
mappedTorrents[t.Filename] = existing
} 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]
mappedTorrents[newKey1] = t
mappedTorrents[newKey2] = existing
}
} else {
mappedTorrents[t.Filename] = t
}
}
// Convert the map back to a slice
deduplicated := make([]Torrent, 0, len(mappedTorrents))
for _, value := range mappedTorrents {
deduplicated = append(deduplicated, value)
}
return deduplicated
}

23
pkg/realdebrid/types.go Normal file
View File

@@ -0,0 +1,23 @@
package realdebrid
type FileJSON struct {
FileSize int `json:"filesize"`
Link string `json:"link"`
}
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"`
}
type Torrent struct {
Filename string `json:"filename"`
Hash string `json:"hash"`
Progress int `json:"progress"`
Added string `json:"added"`
Links []string `json:"links"`
}

23
pkg/realdebrid/util.go Normal file
View File

@@ -0,0 +1,23 @@
package realdebrid
import (
"math"
"strings"
"time"
)
func RetryUntilOk[T any](fn func() (T, error)) *T {
const initialDelay = 2 * time.Second
const maxDelay = 128 * time.Second
for i := 0; ; i++ {
result, err := fn()
if err == nil {
return &result
}
if strings.Contains(err.Error(), "404") {
return nil
}
delay := time.Duration(math.Min(float64(initialDelay*time.Duration(math.Pow(2, float64(i)))), float64(maxDelay)))
time.Sleep(delay)
}
}

186
pkg/repo/mysql.go Normal file
View File

@@ -0,0 +1,186 @@
package repo
import (
"bytes"
"database/sql"
"encoding/gob"
"fmt"
"log"
"path"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
_ "github.com/go-sql-driver/mysql"
"github.com/qianbin/directcache"
"github.com/zeebo/xxh3"
)
type Database struct {
Connection *sql.DB
Cache *directcache.Cache
}
func GenerateID(directory, filename string) string {
fullPath := path.Join(directory, filename)
hash := xxh3.HashString(fullPath)
return fmt.Sprintf("%016x", hash)
}
func NewDatabase(dsn string) (*Database, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}
cache := directcache.New(10 << 20) // This initializes a cache with 10 MB
return &Database{Connection: db, Cache: cache}, nil
}
func (db *Database) Insert(parentHash, directory string, resp realdebrid.UnrestrictResponse) {
// Generate the ID for the link
var id string
if resp.Filename == "" {
id = GenerateID(directory, resp.Link)
} else {
id = GenerateID(directory, resp.Filename)
}
// Check if the link already exists in the database
var exists int
err := db.Connection.QueryRow("SELECT COUNT(*) FROM Links WHERE ID = ?", id).Scan(&exists)
if err != nil {
log.Printf("failed to check existence: %v", err)
}
// If link does not exist in the database, insert the new record
if exists == 0 {
_, err = db.Connection.Exec(`
INSERT INTO Links (ID, ParentHash, Directory, Filename, Filesize, Link, Host)
VALUES (?, ?, ?, ?, ?, ?, ?)`,
id,
parentHash,
directory,
resp.Filename,
resp.Filesize,
resp.Link,
resp.Host,
)
if err != nil {
log.Printf("failed to insert record: %v", err)
}
// Clear cache for parentHash
db.Cache.Del([]byte(parentHash))
}
}
func (db *Database) Get(directory, filename string) (*DavFile, error) {
id := GenerateID(directory, filename)
data, ok := db.Cache.Get([]byte(id))
if !ok {
resp, err := fetchFromDatabaseByID(db.Connection, id)
if err != nil {
return nil, err
}
buffer := &bytes.Buffer{}
encoder := gob.NewEncoder(buffer)
if err := encoder.Encode(resp); err != nil {
return nil, err
}
db.Cache.Set([]byte(id), buffer.Bytes())
return resp, nil
}
buffer := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buffer)
var resp DavFile
if err := decoder.Decode(&resp); err != nil {
return nil, err
}
return &resp, nil
}
func (db *Database) GetMultiple(parentHash string) (*DavFiles, error) {
key := []byte(parentHash)
data, ok := db.Cache.Get(key)
if !ok {
resps, err := fetchMultipleFromDatabase(db.Connection, parentHash)
if err != nil {
return nil, err
}
buffer := &bytes.Buffer{}
encoder := gob.NewEncoder(buffer)
if err := encoder.Encode(resps); err != nil {
return nil, err
}
db.Cache.Set(key, buffer.Bytes())
return resps, nil
}
buffer := bytes.NewBuffer(data)
decoder := gob.NewDecoder(buffer)
var resps DavFiles
if err := decoder.Decode(&resps); err != nil {
return nil, err
}
return &resps, nil
}
func fetchFromDatabaseByID(conn *sql.DB, id 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,
)
if err != nil {
if err == sql.ErrNoRows {
return &resp, nil
}
log.Printf("failed to fetch record: %v", err)
}
return &resp, nil
}
func fetchMultipleFromDatabase(conn *sql.DB, parentHash string) (*DavFiles, error) {
log.Printf("fetching multiple from database: %s", parentHash)
rows, err := conn.Query(`
SELECT Filename, Filesize, Link
FROM Links WHERE ParentHash = ?`,
parentHash,
)
if err != nil {
return nil, fmt.Errorf("failed to fetch records: %v", err)
}
defer rows.Close()
var responses []*DavFile
for rows.Next() {
resp := &DavFile{}
if err := rows.Scan(&resp.Filename, &resp.Filesize, &resp.Link); err != nil {
return nil, fmt.Errorf("failed to scan row: %v", err)
}
responses = append(responses, resp)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error while iterating over rows: %v", err)
}
result := &DavFiles{
Files: responses,
}
return result, nil
}

11
pkg/repo/types.go Normal file
View File

@@ -0,0 +1,11 @@
package repo
type DavFile struct {
Filename string
Filesize int64
Link string
}
type DavFiles struct {
Files []*DavFile
}