Initial commit 🌈
This commit is contained in:
99
internal/dav/response.go
Normal file
99
internal/dav/response.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package dav
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/debridmediamanager.com/zurg/pkg/dav"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/repo"
|
||||
)
|
||||
|
||||
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
|
||||
for _, item := range torrents {
|
||||
if item.Progress != 100 {
|
||||
continue
|
||||
}
|
||||
|
||||
path := filepath.Join("/torrents", item.Filename)
|
||||
responses = append(responses, dav.Directory(path))
|
||||
}
|
||||
|
||||
return &dav.MultiStatus{
|
||||
XMLNS: "DAV:",
|
||||
Response: responses,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func createSingleTorrentResponse(torrent realdebrid.Torrent, db *repo.Database) (*dav.MultiStatus, error) {
|
||||
var responses []dav.Response
|
||||
|
||||
// initial response is the directory itself
|
||||
currentPath := filepath.Join("/torrents", torrent.Filename)
|
||||
responses = append(responses, dav.Directory(currentPath))
|
||||
|
||||
davFiles, err := db.GetMultiple(torrent.Hash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create a map for O(1) lookups of the cached links
|
||||
cachedLinksMap := make(map[string]*repo.DavFile)
|
||||
for _, u := range davFiles.Files {
|
||||
cachedLinksMap[u.Link] = u
|
||||
}
|
||||
for _, link := range torrent.Links {
|
||||
if u, exists := cachedLinksMap[link]; exists {
|
||||
if u.Filesize == 0 {
|
||||
// This link is cached but the filesize is 0
|
||||
// This means that the link is dead
|
||||
continue
|
||||
}
|
||||
path := filepath.Join(currentPath, u.Filename)
|
||||
response := dav.File(
|
||||
path,
|
||||
int(u.Filesize),
|
||||
torrent.Added, // Assuming you want to use the torrent added time here
|
||||
)
|
||||
responses = append(responses, response)
|
||||
} else {
|
||||
// This link is not cached yet
|
||||
unrestrictFn := func() (realdebrid.UnrestrictResponse, error) {
|
||||
return realdebrid.UnrestrictCheck(os.Getenv("RD_TOKEN"), link)
|
||||
}
|
||||
unrestrictResponse := realdebrid.RetryUntilOk(unrestrictFn)
|
||||
if unrestrictResponse == nil {
|
||||
db.Insert(torrent.Hash, torrent.Filename, realdebrid.UnrestrictResponse{
|
||||
Filename: "",
|
||||
Filesize: 0,
|
||||
Link: link,
|
||||
Host: "",
|
||||
})
|
||||
continue
|
||||
} else {
|
||||
db.Insert(torrent.Hash, torrent.Filename, *unrestrictResponse)
|
||||
}
|
||||
|
||||
path := filepath.Join(currentPath, unrestrictResponse.Filename)
|
||||
response := dav.File(
|
||||
path,
|
||||
int(unrestrictResponse.Filesize),
|
||||
torrent.Added,
|
||||
)
|
||||
responses = append(responses, response)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: dedupe the links in the response
|
||||
|
||||
return &dav.MultiStatus{
|
||||
XMLNS: "DAV:",
|
||||
Response: responses,
|
||||
}, nil
|
||||
}
|
||||
127
internal/dav/router.go
Normal file
127
internal/dav/router.go
Normal file
@@ -0,0 +1,127 @@
|
||||
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"
|
||||
)
|
||||
|
||||
func findTorrentByFilename(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"),
|
||||
},
|
||||
}
|
||||
|
||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
requestPath := path.Clean(r.URL.Path)
|
||||
|
||||
switch r.Method {
|
||||
case "PROPFIND":
|
||||
log.Println("PROPFIND", requestPath)
|
||||
var output []byte
|
||||
var err error
|
||||
|
||||
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 {
|
||||
lastSegment := path.Base(requestPath)
|
||||
torrent := findTorrentByFilename(torrents, lastSegment)
|
||||
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.MethodOptions:
|
||||
log.Println("OPTIONS", requestPath)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
case http.MethodGet:
|
||||
log.Println("GET", requestPath)
|
||||
segments := strings.Split(requestPath, "/")
|
||||
|
||||
// If there are less than 2 segments, return an error or adjust as needed
|
||||
if len(segments) < 2 {
|
||||
log.Println("Cannot find file")
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
}
|
||||
|
||||
// Get the last two segments
|
||||
secondLast := segments[len(segments)-2]
|
||||
last := segments[len(segments)-1]
|
||||
unrestrict, dbErr := db.Get(secondLast, last)
|
||||
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
|
||||
}
|
||||
|
||||
resp, err := realdebrid.UnrestrictLink(os.Getenv("RD_TOKEN"), unrestrict.Link)
|
||||
if err != nil {
|
||||
log.Printf("Cannot unrestrict link: %v", err.Error())
|
||||
http.Error(w, fmt.Sprintf("Cannot unrestrict link: %v", err.Error()), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, resp.Download, http.StatusFound)
|
||||
|
||||
default:
|
||||
log.Println("Method not implemented")
|
||||
http.Error(w, "Method not implemented", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user