A lot of rewrite here

This commit is contained in:
Ben Sarmiento
2023-10-18 00:17:07 +02:00
parent 44216343e2
commit 0886c93250
14 changed files with 399 additions and 482 deletions

91
internal/dav/getfile.go Normal file
View File

@@ -0,0 +1,91 @@
package dav
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/realdebrid"
)
// HandleGetRequest handles a GET request to a file
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) {
requestPath := path.Clean(r.URL.Path)
segments := strings.Split(requestPath, "/")
// If there are less than 3 segments, return an error or adjust as needed
if len(segments) < 3 {
log.Println("Invalid url", requestPath)
http.Error(w, "Cannot find file", http.StatusNotFound)
}
// Get the last two segments
torrentName := segments[len(segments)-2]
torrents := findAllTorrentsWithName(t, torrentName)
if torrents == nil {
log.Println("Cannot find directory", requestPath)
http.Error(w, "Cannot find file", http.StatusNotFound)
return
}
filename := segments[len(segments)-1]
filenameV2, linkFragment := extractIDFromFilename(filename)
link := findLinkByFragment(torrents, linkFragment)
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
return realdebrid.UnrestrictLink(os.Getenv("RD_TOKEN"), link)
}
resp := realdebrid.RetryUntilOk(unrestrictFn)
if resp == nil {
// TODO: Delete the link from the database
log.Println("Cannot unrestrict link")
http.Error(w, "Cannot find file", http.StatusNotFound)
return
}
if resp.Filename != filenameV2 {
// TODO: Redo the logic to handle mismatch
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 []realdebrid.Torrent, 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
}
}
}
return ""
}

82
internal/dav/propfind.go Normal file
View File

@@ -0,0 +1,82 @@
package dav
import (
"encoding/xml"
"fmt"
"log"
"net/http"
"path"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/dav"
)
// HandlePropfindRequest handles a PROPFIND request
func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) {
var output []byte
var err error
requestPath := path.Clean(r.URL.Path)
if requestPath == "/" {
output, err = handleRoot(w, r)
} else if requestPath == "/torrents" {
output, err = handleListOfTorrents(w, r, t)
} else {
output, err = handleSingleTorrent(w, r, t)
}
if err != nil {
log.Printf("Cannot marshal xml: %v\n", err.Error())
http.Error(w, fmt.Sprintf("Cannot marshal xml: %v", err.Error()), http.StatusInternalServerError)
return
}
if output != nil {
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
w.WriteHeader(http.StatusMultiStatus)
fmt.Fprintf(w, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n%s\n", output)
return
}
}
// handleRoot handles a PROPFIND request to the root directory
func handleRoot(w http.ResponseWriter, r *http.Request) ([]byte, error) {
rootResponse := dav.MultiStatus{
XMLNS: "DAV:",
Response: []dav.Response{
dav.Directory("/"),
dav.Directory("/torrents"),
},
}
return xml.MarshalIndent(rootResponse, "", " ")
}
// handleListOfTorrents handles a PROPFIND request to the /torrents directory
func handleListOfTorrents(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
allTorrents := t.GetAll()
allTorrentsResponse, err := createMultiTorrentResponse(allTorrents)
if err != nil {
log.Printf("Cannot read directory: %v\n", err.Error())
http.Error(w, fmt.Sprintf("Cannot read directory: %v", err.Error()), http.StatusInternalServerError)
return nil, nil
}
return xml.MarshalIndent(allTorrentsResponse, "", " ")
}
// handleSingleTorrent handles a PROPFIND request to a single torrent directory
func handleSingleTorrent(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) ([]byte, error) {
requestPath := path.Clean(r.URL.Path)
torrentName := path.Base(requestPath)
foundTorrents := findAllTorrentsWithName(t, torrentName)
if len(foundTorrents) == 0 {
log.Println("Cannot find directory")
http.Error(w, "Cannot find directory", http.StatusNotFound)
return nil, nil
}
var torrentResponse *dav.MultiStatus
torrentResponse, err := createCombinedTorrentResponse(foundTorrents, t)
if err != nil {
log.Printf("Cannot read directory: %v\n", err.Error())
http.Error(w, fmt.Sprintf("Cannot read directory: %v", err.Error()), http.StatusInternalServerError)
return nil, nil
}
return xml.MarshalIndent(torrentResponse, "", " ")
}

View File

@@ -1,26 +1,29 @@
package dav
import (
"os"
"fmt"
"path/filepath"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"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/repo"
)
// createMultiTorrentResponse creates a WebDAV response for a list of torrents
func createMultiTorrentResponse(torrents []realdebrid.Torrent) (*dav.MultiStatus, error) {
var responses []dav.Response
// initial response is the directory itself
responses = append(responses, dav.Directory("/torrents"))
// add all files and directories in the directory
seen := make(map[string]bool)
for _, item := range torrents {
if item.Progress != 100 {
continue
}
if _, exists := seen[item.Filename]; exists {
continue
}
seen[item.Filename] = true
path := filepath.Join("/torrents", item.Filename)
responses = append(responses, dav.Directory(path))
@@ -32,66 +35,76 @@ func createMultiTorrentResponse(torrents []realdebrid.Torrent) (*dav.MultiStatus
}, nil
}
func createSingleTorrentResponse(torrent realdebrid.Torrent, db *repo.Database) (*dav.MultiStatus, error) {
// createTorrentResponse creates a WebDAV response for torrents with the same name
func createCombinedTorrentResponse(torrents []realdebrid.Torrent, t *torrent.TorrentManager) (*dav.MultiStatus, error) {
var responses []dav.Response
// initial response is the directory itself
currentPath := filepath.Join("/torrents", torrent.Filename)
currentPath := filepath.Join("/torrents", torrents[0].Filename)
responses = append(responses, dav.Directory(currentPath))
davFiles, err := db.GetMultiple(torrent.Hash)
if err != nil {
return nil, err
}
seen := make(map[string]bool)
idx := 0
// Create a map for O(1) lookups of the cached links
cachedLinksMap := make(map[string]*repo.DavFile)
for _, cached := range davFiles.Files {
cachedLinksMap[cached.Link] = cached
}
for _, link := range torrent.Links {
if unrestrict, exists := cachedLinksMap[link]; exists {
if unrestrict.Filesize == 0 {
// This link is cached but the filesize is 0
// This means that the link is dead
var torrentResponses []dav.Response
for _, torrent := range torrents {
info := t.GetInfo(torrent.ID)
if info == nil {
continue
}
var selectedFiles []realdebrid.File
for _, file := range info.Files {
if file.Selected == 0 {
continue
}
filenameV2 := davextra.InsertLinkFragment(unrestrict.Filename, davextra.GetLinkFragment(unrestrict.Link))
path := filepath.Join(currentPath, filenameV2)
response := dav.File(
path,
unrestrict.Filesize,
convertDate(torrent.Added),
link,
)
responses = append(responses, response)
selectedFiles = append(selectedFiles, file)
}
if len(selectedFiles) != len(info.Links) {
fmt.Println("Links and files do not match", info.Filename)
// TODO: Add auto-healing for this
// for _, link := range info.Links {
// unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
// return realdebrid.UnrestrictCheck(os.Getenv("RD_TOKEN"), link)
// }
// resp := realdebrid.RetryUntilOk(unrestrictFn)
// if resp == nil {
// continue
// } else {
// if _, exists := seen[resp.Filename]; exists {
// continue
// }
// seen[resp.Filename] = true
// filePath := filepath.Join(currentPath, resp.Filename)
// torrentResponses = append(torrentResponses,
// dav.File(
// filePath,
// resp.Filesize,
// info.Added,
// resp.Link,
// ),
// )
// }
// }
} else {
// This link is not cached yet
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
return realdebrid.UnrestrictCheck(os.Getenv("RD_TOKEN"), link)
for _, file := range selectedFiles {
filename := filepath.Base(file.Path)
if _, exists := seen[filename]; exists {
continue
}
seen[filename] = true
filePath := filepath.Join(currentPath, filename)
torrentResponses = append(torrentResponses, dav.File(
filePath,
file.Bytes,
convertDate(info.Added),
info.Links[idx],
))
idx++
}
unrestrict := realdebrid.RetryUntilOk(unrestrictFn)
if unrestrict == nil {
db.Insert(torrent.Hash, torrent.Filename, realdebrid.UnrestrictResponse{
Filename: "",
Filesize: 0,
Link: link,
Host: "",
})
continue
}
db.Insert(torrent.Hash, torrent.Filename, *unrestrict)
filenameV2 := davextra.InsertLinkFragment(unrestrict.Filename, davextra.GetLinkFragment(unrestrict.Link))
path := filepath.Join(currentPath, filenameV2)
response := dav.File(
path,
unrestrict.Filesize,
convertDate(torrent.Added),
link,
)
responses = append(responses, response)
}
}
responses = append(responses, torrentResponses...)
return &dav.MultiStatus{
XMLNS: "DAV:",

View File

@@ -1,42 +1,17 @@
package dav
import (
"encoding/xml"
"fmt"
"log"
"net/http"
"os"
"path"
"strings"
"github.com/debridmediamanager.com/zurg/pkg/dav"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
"github.com/debridmediamanager.com/zurg/pkg/repo"
"github.com/debridmediamanager.com/zurg/internal/torrent"
)
func findTorrentByName(torrents []realdebrid.Torrent, filename string) *realdebrid.Torrent {
for _, torrent := range torrents {
if torrent.Filename == filename {
return &torrent
}
}
return nil
}
func Router(mux *http.ServeMux, db *repo.Database) {
torrents, err := realdebrid.GetTorrents(os.Getenv("RD_TOKEN"))
if err != nil {
log.Printf("Cannot get torrents: %v", err.Error())
return
}
rootResponse := dav.MultiStatus{
XMLNS: "DAV:",
Response: []dav.Response{
dav.Directory("/"),
dav.Directory("/torrents"),
},
}
// Router creates a WebDAV router
func Router(mux *http.ServeMux) {
t := torrent.NewTorrentManager(os.Getenv("RD_TOKEN"))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requestPath := path.Clean(r.URL.Path)
@@ -45,89 +20,14 @@ func Router(mux *http.ServeMux, db *repo.Database) {
switch r.Method {
case "PROPFIND":
var output []byte
var err error
HandlePropfindRequest(w, r, t)
if requestPath == "/" {
output, err = xml.MarshalIndent(rootResponse, "", " ")
} else if requestPath == "/torrents" {
var allTorrentsResponse *dav.MultiStatus
allTorrentsResponse, err = createMultiTorrentResponse(torrents)
if err != nil {
log.Printf("Cannot read directory: %v", err.Error())
http.Error(w, fmt.Sprintf("Cannot read directory: %v", err.Error()), http.StatusInternalServerError)
return
}
output, err = xml.MarshalIndent(allTorrentsResponse, "", " ")
} else {
torrentName := path.Base(requestPath)
torrent := findTorrentByName(torrents, torrentName)
if torrent == nil {
log.Println("Cannot find directory")
http.Error(w, "Cannot find directory", http.StatusNotFound)
return
}
var torrentResponse *dav.MultiStatus
torrentResponse, err = createSingleTorrentResponse(*torrent, db)
if err != nil {
log.Printf("Cannot read directory: %v", err.Error())
http.Error(w, fmt.Sprintf("Cannot read directory: %v", err.Error()), http.StatusInternalServerError)
return
}
output, err = xml.MarshalIndent(torrentResponse, "", " ")
}
if err != nil {
log.Printf("Cannot marshal xml: %v", err.Error())
http.Error(w, fmt.Sprintf("Cannot marshal xml: %v", err.Error()), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/xml; charset=\"utf-8\"")
w.WriteHeader(http.StatusMultiStatus)
fmt.Fprintf(w, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n%s\n", output)
case http.MethodGet:
HandleGetRequest(w, r, t)
case http.MethodOptions:
w.WriteHeader(http.StatusOK)
case http.MethodGet:
segments := strings.Split(requestPath, "/")
// If there are less than 3 segments, return an error or adjust as needed
if len(segments) < 3 {
log.Println("Cannot find file")
http.Error(w, "Cannot find file", http.StatusNotFound)
}
// Get the last two segments
torrentName := segments[len(segments)-2]
torrent := findTorrentByName(torrents, torrentName)
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 {
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)
return
}
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
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())
http.Error(w, fmt.Sprintf("Cannot unrestrict link: %v", err.Error()), http.StatusNotFound)
return
}
http.Redirect(w, r, resp.Download, http.StatusFound)
default:
log.Println("Method not implemented")
http.Error(w, "Method not implemented", http.StatusMethodNotAllowed)

View File

@@ -1,15 +1,33 @@
package dav
import (
"fmt"
"log"
"time"
"github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
)
// convertDate converts a date from RFC3339 to RFC1123
func convertDate(input string) string {
t, err := time.Parse(time.RFC3339, input)
if err != nil {
fmt.Println("Error:", err)
log.Println("Error:", err)
return ""
}
return t.Format("Mon, 02 Jan 2006 15:04:05 GMT")
}
// findAllTorrentsWithName finds all torrents with a given name
func findAllTorrentsWithName(t *torrent.TorrentManager, filename string) []realdebrid.Torrent {
var matchingTorrents []realdebrid.Torrent
torrents := t.GetAll()
for _, torrent := range torrents {
if torrent.Filename == filename {
matchingTorrents = append(matchingTorrents, torrent)
}
}
return matchingTorrents
}

View File

@@ -0,0 +1 @@
package torrent

View File

@@ -0,0 +1,93 @@
package torrent
import (
"log"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
"github.com/dgraph-io/ristretto"
)
type TorrentManager struct {
token string
cache *ristretto.Cache
workerPool chan bool
}
// NewTorrentManager creates a new torrent manager
// it will fetch all torrents and their info in the background
// and cache them
func NewTorrentManager(token string) *TorrentManager {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1e7, // number of keys to track frequency of (10M).
MaxCost: 1 << 30, // maximum cost of cache (1GB).
BufferItems: 64, // number of keys per Get buffer.
})
if err != nil {
panic(err)
}
handler := &TorrentManager{
token: token,
cache: cache,
workerPool: make(chan bool, 10),
}
torrents := handler.getAll()
for _, torrent := range torrents {
go func(id string) {
handler.workerPool <- true
handler.getInfo(id)
// sleep for 1 second to avoid rate limiting
<-handler.workerPool
}(torrent.ID)
}
return handler
}
func (t *TorrentManager) getAll() []realdebrid.Torrent {
cacheKey := "t:all"
torrents, err := realdebrid.GetTorrents(t.token)
if err != nil {
log.Printf("Cannot get torrents: %v\n", err.Error())
return nil
}
t.cache.Set(cacheKey, torrents, 0)
return torrents
}
func (t *TorrentManager) GetAll() []realdebrid.Torrent {
cacheKey := "t:all"
if data, found := t.cache.Get(cacheKey); found {
if cachedTorrents, ok := data.([]realdebrid.Torrent); ok {
return cachedTorrents
} else {
t.cache.Del(cacheKey)
}
}
return t.getAll()
}
func (t *TorrentManager) getInfo(torrentID string) *realdebrid.Torrent {
cacheKey := "t:" + torrentID
info, err := realdebrid.GetTorrentInfo(t.token, torrentID)
if err != nil {
log.Printf("Cannot get info: %v\n", err.Error())
return nil
}
t.cache.Set(cacheKey, info, 0)
return info
}
func (t *TorrentManager) GetInfo(torrentID string) *realdebrid.Torrent {
cacheKey := "t:" + torrentID
if data, found := t.cache.Get(cacheKey); found {
if torrent, ok := data.(*realdebrid.Torrent); ok {
return torrent
} else {
t.cache.Del(cacheKey)
}
}
return t.getInfo(torrentID)
}