read token from config

This commit is contained in:
Ben Sarmiento
2023-10-20 01:49:06 +02:00
parent 464f522fea
commit 0e442fc9be
9 changed files with 23 additions and 22 deletions

View File

@@ -3,7 +3,6 @@ package main
import ( import (
"log" "log"
"net/http" "net/http"
"os"
"github.com/debridmediamanager.com/zurg/internal/config" "github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/dav" "github.com/debridmediamanager.com/zurg/internal/dav"
@@ -16,7 +15,7 @@ func main() {
log.Panicf("Config failed to load: %v", cErr) log.Panicf("Config failed to load: %v", cErr)
} }
t := torrent.NewTorrentManager(os.Getenv("RD_TOKEN"), c) t := torrent.NewTorrentManager(c)
// app := aero.New() // app := aero.New()
// dav.Setup(app, c, t) // dav.Setup(app, c, t)

View File

@@ -9,6 +9,7 @@ import (
type ConfigInterface interface { type ConfigInterface interface {
GetVersion() string GetVersion() string
GetToken() string
GetDirectories() []string GetDirectories() []string
MeetsConditions(directory, fileID, fileName string) bool MeetsConditions(directory, fileID, fileName string) bool
} }

View File

@@ -2,4 +2,5 @@ package config
type ZurgConfig struct { type ZurgConfig struct {
Version string `yaml:"zurg"` Version string `yaml:"zurg"`
Token string `yaml:"token"`
} }

View File

@@ -20,6 +20,10 @@ func (z *ZurgConfigV1) GetVersion() string {
return "v1" return "v1"
} }
func (z *ZurgConfigV1) GetToken() string {
return z.Token
}
func (z *ZurgConfigV1) GetDirectories() []string { func (z *ZurgConfigV1) GetDirectories() []string {
var rootDirectories []string var rootDirectories []string
for directory := range z.Directories { for directory := range z.Directories {

View File

@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os"
"strings" "strings"
"github.com/aerogo/aero" "github.com/aerogo/aero"
@@ -87,7 +86,7 @@ func Setup(app *aero.Application, c config.ConfigInterface, t *torrent.TorrentMa
} }
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) { unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
return realdebrid.UnrestrictLink(os.Getenv("RD_TOKEN"), link) return realdebrid.UnrestrictLink(c.GetToken(), link)
} }
resp := realdebrid.RetryUntilOk(unrestrictFn) resp := realdebrid.RetryUntilOk(unrestrictFn)
if resp == nil { if resp == nil {

View File

@@ -4,18 +4,18 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"os"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/debridmediamanager.com/zurg/internal/config"
"github.com/debridmediamanager.com/zurg/internal/torrent" "github.com/debridmediamanager.com/zurg/internal/torrent"
"github.com/debridmediamanager.com/zurg/pkg/davextra" "github.com/debridmediamanager.com/zurg/pkg/davextra"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid" "github.com/debridmediamanager.com/zurg/pkg/realdebrid"
) )
// HandleGetRequest handles a GET request to a file // HandleGetRequest handles a GET request to a file
func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager) { func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface) {
requestPath := path.Clean(r.URL.Path) requestPath := path.Clean(r.URL.Path)
segments := strings.Split(requestPath, "/") segments := strings.Split(requestPath, "/")
@@ -48,13 +48,15 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
} }
unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) { unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) {
return realdebrid.UnrestrictLink(os.Getenv("RD_TOKEN"), link) return realdebrid.UnrestrictLink(c.GetToken(), link)
} }
resp := realdebrid.RetryUntilOk(unrestrictFn) resp := realdebrid.RetryUntilOk(unrestrictFn)
if resp == nil { if resp == nil {
// TODO: Readd the file // TODO: Readd the file
// when unrestricting fails, it means the file is not available anymore, but still in their database // when unrestricting fails, it means the file is not available anymore, but still in their database
// if it's the only file, tough luck // if it's the only file, tough luck
// if it's the only file, try to readd it
// delete the old one, add a new one
log.Println("Cannot unrestrict link", link, filenameV2) log.Println("Cannot unrestrict link", link, filenameV2)
http.Error(w, "Cannot find file", http.StatusNotFound) http.Error(w, "Cannot find file", http.StatusNotFound)
return return

View File

@@ -16,7 +16,7 @@ func Router(mux *http.ServeMux, c config.ConfigInterface, t *torrent.TorrentMana
HandlePropfindRequest(w, r, t, c) HandlePropfindRequest(w, r, t, c)
case http.MethodGet: case http.MethodGet:
HandleGetRequest(w, r, t) HandleGetRequest(w, r, t, c)
case http.MethodOptions: case http.MethodOptions:
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View File

@@ -45,9 +45,9 @@ func (handler *TorrentManager) refreshTorrents() {
// NewTorrentManager creates a new torrent manager // NewTorrentManager creates a new torrent manager
// it will fetch all torrents and their info in the background // it will fetch all torrents and their info in the background
// and store them in-memory // and store them in-memory
func NewTorrentManager(token string, config config.ConfigInterface) *TorrentManager { func NewTorrentManager(config config.ConfigInterface) *TorrentManager {
handler := &TorrentManager{ handler := &TorrentManager{
token: token, token: config.GetToken(),
workerPool: make(chan bool, 10), workerPool: make(chan bool, 10),
config: config, config: config,
} }

View File

@@ -81,10 +81,10 @@ func UnrestrictLink(accessToken, link string) (*UnrestrictResponse, error) {
return nil, err return nil, err
} }
if canFetchFirstByte(response.Download) { if !canFetchFirstByte(response.Download) {
return &response, nil
}
return nil, fmt.Errorf("can't fetch first byte") return nil, fmt.Errorf("can't fetch first byte")
}
return &response, nil
} }
func canFetchFirstByte(url string) bool { func canFetchFirstByte(url string) bool {
@@ -108,19 +108,14 @@ func canFetchFirstByte(url string) bool {
if resp.StatusCode == http.StatusPartialContent { if resp.StatusCode == http.StatusPartialContent {
buffer := make([]byte, 1) buffer := make([]byte, 1)
_, err := resp.Body.Read(buffer) _, err := resp.Body.Read(buffer)
if err != nil { return err == nil
return false
}
return true
} }
// If server doesn't support partial content, try reading the first byte and immediately close // If server doesn't support partial content, try reading the first byte and immediately close
buffer := make([]byte, 1) buffer := make([]byte, 1)
_, err = io.ReadFull(resp.Body, buffer) _, err = resp.Body.Read(buffer)
if err != nil { resp.Body.Close() // Close immediately after reading
return false return err == nil
}
return true
} }
// GetTorrents returns all torrents, paginated // GetTorrents returns all torrents, paginated