fixes here and there
This commit is contained in:
@@ -23,6 +23,7 @@ type ConfigInterface interface {
|
||||
GetNetworkBufferSize() int
|
||||
GetMountPoint() string
|
||||
EnableRetainFolderNameExtension() bool
|
||||
GetRandomPreferredHost() string
|
||||
}
|
||||
|
||||
func LoadZurgConfig(filename string) (ConfigInterface, error) {
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
package config
|
||||
|
||||
import "math/rand"
|
||||
|
||||
type ZurgConfig struct {
|
||||
Version string `yaml:"zurg"`
|
||||
Token string `yaml:"token"`
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
NumOfWorkers int `yaml:"concurrent_workers"`
|
||||
RefreshEverySeconds int `yaml:"check_for_changes_every_secs"`
|
||||
CacheTimeHours int `yaml:"info_cache_time_hours"`
|
||||
CanRepair bool `yaml:"enable_repair"`
|
||||
OnLibraryUpdate string `yaml:"on_library_update"`
|
||||
NetworkBufferSize int `yaml:"network_buffer_size"`
|
||||
MountPoint string `yaml:"mount_point"`
|
||||
RetainFolderNameExtension bool `yaml:"retain_folder_name_extension"`
|
||||
Version string `yaml:"zurg"`
|
||||
Token string `yaml:"token"`
|
||||
Host string `yaml:"host"`
|
||||
Port string `yaml:"port"`
|
||||
NumOfWorkers int `yaml:"concurrent_workers"`
|
||||
RefreshEverySeconds int `yaml:"check_for_changes_every_secs"`
|
||||
CacheTimeHours int `yaml:"info_cache_time_hours"`
|
||||
CanRepair bool `yaml:"enable_repair"`
|
||||
OnLibraryUpdate string `yaml:"on_library_update"`
|
||||
NetworkBufferSize int `yaml:"network_buffer_size"`
|
||||
MountPoint string `yaml:"mount_point"`
|
||||
RetainFolderNameExtension bool `yaml:"retain_folder_name_extension"`
|
||||
PreferredHosts []string `yaml:"preferred_hosts"`
|
||||
}
|
||||
|
||||
func (z *ZurgConfig) GetToken() string {
|
||||
@@ -73,3 +76,11 @@ func (z *ZurgConfig) GetMountPoint() string {
|
||||
func (z *ZurgConfig) EnableRetainFolderNameExtension() bool {
|
||||
return z.RetainFolderNameExtension
|
||||
}
|
||||
|
||||
func (z *ZurgConfig) GetRandomPreferredHost() string {
|
||||
if len(z.PreferredHosts) == 0 {
|
||||
return ""
|
||||
}
|
||||
randomIndex := rand.Intn(len(z.PreferredHosts))
|
||||
return z.PreferredHosts[randomIndex]
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
|
||||
case len(filteredSegments) == 2:
|
||||
output, err = handleSingleTorrent(requestPath, w, r, t)
|
||||
default:
|
||||
log.Errorf("Request %s %s not found", r.Method, requestPath)
|
||||
log.Warnf("Request %s %s not found", r.Method, requestPath)
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
@@ -54,9 +54,9 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To
|
||||
|
||||
func handleRoot(w http.ResponseWriter, r *http.Request, c config.ConfigInterface) ([]byte, error) {
|
||||
var responses []dav.Response
|
||||
responses = append(responses, dav.Directory("/"))
|
||||
responses = append(responses, dav.Directory(""))
|
||||
for _, directory := range c.GetDirectories() {
|
||||
responses = append(responses, dav.Directory("/"+directory))
|
||||
responses = append(responses, dav.Directory(directory))
|
||||
}
|
||||
rootResponse := dav.MultiStatus{
|
||||
XMLNS: "DAV:",
|
||||
@@ -104,7 +104,7 @@ func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Requ
|
||||
accessKey := path.Base(requestPath)
|
||||
torrent, exists := t.TorrentMap.Get(accessKey)
|
||||
if !exists {
|
||||
return nil, fmt.Errorf("cannot find torrent %s", requestPath)
|
||||
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
|
||||
}
|
||||
|
||||
var responses []dav.Response
|
||||
|
||||
@@ -30,7 +30,7 @@ func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.T
|
||||
case len(filteredSegments) == 3:
|
||||
output, err = handleSingleTorrent(requestPath, w, r, t)
|
||||
default:
|
||||
log.Errorf("Request %s %s not found", r.Method, requestPath)
|
||||
log.Warnf("Request %s %s not found", r.Method, requestPath)
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Requ
|
||||
accessKey := path.Base(requestPath)
|
||||
torrent, _ := t.TorrentMap.Get(accessKey)
|
||||
if torrent == nil {
|
||||
return nil, fmt.Errorf("cannot find torrent %s", requestPath)
|
||||
return nil, fmt.Errorf("cannot find torrent %s", accessKey)
|
||||
}
|
||||
|
||||
htmlDoc := "<ol>"
|
||||
|
||||
@@ -14,17 +14,16 @@ import (
|
||||
"github.com/debridmediamanager.com/zurg/pkg/logutil"
|
||||
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||
"github.com/elliotchance/orderedmap/v2"
|
||||
"github.com/nutsdb/nutsdb"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type TorrentManager struct {
|
||||
TorrentMap *orderedmap.OrderedMap[string, *Torrent] // accessKey -> Torrent
|
||||
repairMap *orderedmap.OrderedMap[string, bool] // accessKey -> bool
|
||||
requiredVersion string
|
||||
rd *realdebrid.RealDebrid
|
||||
checksum string
|
||||
config config.ConfigInterface
|
||||
db *nutsdb.DB
|
||||
workerPool chan bool
|
||||
mu *sync.Mutex
|
||||
log *zap.SugaredLogger
|
||||
@@ -33,19 +32,18 @@ type TorrentManager struct {
|
||||
// NewTorrentManager creates a new torrent manager
|
||||
// it will fetch all torrents and their info in the background
|
||||
// and store them in-memory and cached in files
|
||||
func NewTorrentManager(config config.ConfigInterface, db *nutsdb.DB) *TorrentManager {
|
||||
func NewTorrentManager(config config.ConfigInterface, rd *realdebrid.RealDebrid) *TorrentManager {
|
||||
t := &TorrentManager{
|
||||
TorrentMap: orderedmap.NewOrderedMap[string, *Torrent](),
|
||||
repairMap: orderedmap.NewOrderedMap[string, bool](),
|
||||
requiredVersion: "10.11.2023",
|
||||
rd: realdebrid.NewRealDebrid(config.GetToken(), logutil.NewLogger().Named("realdebrid")),
|
||||
rd: rd,
|
||||
config: config,
|
||||
db: db,
|
||||
workerPool: make(chan bool, config.GetNumOfWorkers()),
|
||||
mu: &sync.Mutex{},
|
||||
log: logutil.NewLogger().Named("manager"),
|
||||
}
|
||||
|
||||
// start with a clean slate
|
||||
t.mu.Lock()
|
||||
|
||||
newTorrents, _, err := t.rd.GetTorrents(0)
|
||||
@@ -170,7 +168,7 @@ func (t *TorrentManager) getChecksum() string {
|
||||
totalCount = torrentsResp.totalCount
|
||||
case count = <-countChan:
|
||||
case err := <-errChan:
|
||||
t.log.Errorf("Checksum API Error: %v\n", err)
|
||||
t.log.Warnf("Checksum API Error: %v\n", err)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
@@ -199,7 +197,7 @@ func (t *TorrentManager) startRefreshJob() {
|
||||
|
||||
newTorrents, _, err := t.rd.GetTorrents(0)
|
||||
if err != nil {
|
||||
t.log.Errorf("Cannot get torrents: %v\n", err)
|
||||
t.log.Warnf("Cannot get torrents: %v\n", err)
|
||||
continue
|
||||
}
|
||||
t.log.Infof("Detected changes! Refreshing %d torrents", len(newTorrents))
|
||||
@@ -271,7 +269,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
|
||||
if info == nil {
|
||||
info, err = t.rd.GetTorrentInfo(rdTorrent.ID)
|
||||
if err != nil {
|
||||
t.log.Errorf("Cannot get info for id=%s: %v\n", rdTorrent.ID, err)
|
||||
t.log.Warnf("Cannot get info for id=%s: %v\n", rdTorrent.ID, err)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -296,14 +294,13 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
|
||||
})
|
||||
}
|
||||
if selectedFiles.Len() > len(info.Links) && info.Progress == 100 {
|
||||
t.log.Debugf("%d links has expired for %s %s", selectedFiles.Len()-len(info.Links), info.ID, info.Name)
|
||||
// chaotic file means RD will not output the desired file selection
|
||||
// e.g. even if we select just a single mkv, it will output a rar
|
||||
var isChaotic bool
|
||||
selectedFiles, isChaotic = t.organizeChaos(info.Links, selectedFiles)
|
||||
if isChaotic {
|
||||
t.log.Errorf("Torrent id=%s %s is unfixable, it is always returning an unstreamable link (it is no longer shown in your directories)", info.ID, info.Name)
|
||||
t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
|
||||
// t.log.Warnf("Torrent id=%s %s is unfixable, it is always returning an unstreamable link (it is no longer shown in your directories)", info.ID, info.Name)
|
||||
// t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
|
||||
return nil
|
||||
} else {
|
||||
if streamableCount > 1 {
|
||||
@@ -314,8 +311,8 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
|
||||
t.log.Infof("Torrent id=%s %s marked for repair", info.ID, info.Name)
|
||||
forRepair = true
|
||||
} else {
|
||||
t.log.Errorf("Torrent id=%s %s is unfixable, the lone streamable link has expired (it is no longer shown in your directories)", info.ID, info.Name)
|
||||
t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
|
||||
// t.log.Warnf("Torrent id=%s %s is unfixable, the lone streamable link has expired (it is no longer shown in your directories)", info.ID, info.Name)
|
||||
// t.log.Debugf("You can try fixing it yourself magnet:?xt=urn:btih:%s", info.Hash)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -525,9 +522,19 @@ func (t *TorrentManager) repairAll() {
|
||||
}
|
||||
|
||||
func (t *TorrentManager) Repair(accessKey string) {
|
||||
if _, exists := t.repairMap.Get(accessKey); exists {
|
||||
return
|
||||
}
|
||||
t.repairMap.Set(accessKey, true)
|
||||
|
||||
if !t.config.EnableRepair() {
|
||||
t.log.Warn("Repair is disabled; if you do not have other zurg instances running, you should enable repair")
|
||||
return
|
||||
}
|
||||
|
||||
torrent, _ := t.TorrentMap.Get(accessKey)
|
||||
if torrent == nil {
|
||||
t.log.Errorf("Cannot find torrent %s anymore to repair it", accessKey)
|
||||
t.log.Warnf("Cannot find torrent %s anymore to repair it", accessKey)
|
||||
return
|
||||
}
|
||||
if torrent.InProgress {
|
||||
@@ -602,7 +609,7 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
|
||||
// redownload torrent
|
||||
resp, err := t.rd.AddMagnetHash(torrent.Instances[0].Hash)
|
||||
if err != nil {
|
||||
t.log.Errorf("Cannot redownload torrent: %v", err)
|
||||
t.log.Warnf("Cannot redownload torrent: %v", err)
|
||||
return false
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
@@ -611,7 +618,7 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
|
||||
newTorrentID := resp.ID
|
||||
err = t.rd.SelectTorrentFiles(newTorrentID, missingFiles)
|
||||
if err != nil {
|
||||
t.log.Errorf("Cannot start redownloading: %v", err)
|
||||
t.log.Warnf("Cannot start redownloading: %v", err)
|
||||
t.rd.DeleteTorrent(newTorrentID)
|
||||
return false
|
||||
}
|
||||
@@ -620,13 +627,13 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
|
||||
// see if the torrent is ready
|
||||
info, err := t.rd.GetTorrentInfo(newTorrentID)
|
||||
if err != nil {
|
||||
t.log.Errorf("Cannot get info on redownloaded torrent id=%s : %v", newTorrentID, err)
|
||||
t.log.Warnf("Cannot get info on redownloaded torrent id=%s : %v", newTorrentID, err)
|
||||
t.rd.DeleteTorrent(newTorrentID)
|
||||
return false
|
||||
}
|
||||
|
||||
if info.Status == "magnet_error" || info.Status == "error" || info.Status == "virus" || info.Status == "dead" {
|
||||
t.log.Errorf("Redownloaded torrent id=%s is in error state: %s", newTorrentID, info.Status)
|
||||
t.log.Warnf("Redownloaded torrent id=%s is in error state: %s", newTorrentID, info.Status)
|
||||
t.rd.DeleteTorrent(newTorrentID)
|
||||
return false
|
||||
}
|
||||
@@ -656,7 +663,7 @@ func (t *TorrentManager) canCapacityHandle() bool {
|
||||
for {
|
||||
count, err := t.rd.GetActiveTorrentCount()
|
||||
if err != nil {
|
||||
t.log.Errorf("Cannot get active downloads count: %v", err)
|
||||
t.log.Warnf("Cannot get active downloads count: %v", err)
|
||||
if retryCount >= maxRetries {
|
||||
t.log.Error("Max retries reached. Exiting.")
|
||||
return false
|
||||
|
||||
@@ -47,14 +47,14 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
||||
|
||||
torrent, _ := t.TorrentMap.Get(accessKey)
|
||||
if torrent == nil {
|
||||
log.Errorf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
|
||||
log.Warnf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
file, _ := torrent.SelectedFiles.Get(filename)
|
||||
if file == nil {
|
||||
log.Errorf("Cannot find file from path %s", requestPath)
|
||||
log.Warnf("Cannot find file from path %s", requestPath)
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
@@ -66,7 +66,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
||||
|
||||
if file.Link == "" {
|
||||
// This is a dead file, serve an alternate file
|
||||
log.Errorf("File %s is not yet available, zurg is repairing the torrent", filename)
|
||||
log.Warnf("File %s is not yet available, zurg is repairing the torrent", filename)
|
||||
streamErrorVideo("https://www.youtube.com/watch?v=bGTqwt6vdcY", w, r, t, c, log)
|
||||
return
|
||||
}
|
||||
@@ -75,18 +75,18 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent
|
||||
resp := t.UnrestrictUntilOk(link)
|
||||
if resp == nil {
|
||||
go t.Repair(torrent.AccessKey)
|
||||
log.Errorf("File %s is no longer available, torrent is marked for repair", file.Path)
|
||||
log.Warnf("File %s is no longer available, torrent is marked for repair", file.Path)
|
||||
streamErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, t, c, log)
|
||||
return
|
||||
} else if resp.Filename != filename {
|
||||
actualExt := filepath.Ext(resp.Filename)
|
||||
expectedExt := filepath.Ext(filename)
|
||||
if actualExt != expectedExt && resp.Streamable != 1 {
|
||||
log.Errorf("File extension mismatch: %s and %s", filename, resp.Filename)
|
||||
log.Warnf("File was changed and is not streamable: %s and %s", filename, resp.Filename)
|
||||
streamErrorVideo("https://www.youtube.com/watch?v=t9VgOriBHwE", w, r, t, c, log)
|
||||
return
|
||||
} else {
|
||||
log.Errorf("Filename mismatch: %s and %s", filename, resp.Filename)
|
||||
log.Warnf("Filename mismatch: %s and %s", filename, resp.Filename)
|
||||
}
|
||||
}
|
||||
cache.Add(requestPath, resp.Download)
|
||||
@@ -108,11 +108,11 @@ func streamFileToResponse(torrent *torrent.Torrent, url string, w http.ResponseW
|
||||
}
|
||||
|
||||
// Create a custom HTTP client
|
||||
client := zurghttp.NewHTTPClient(c.GetToken(), 10)
|
||||
client := zurghttp.NewHTTPClient(c.GetToken(), 10, c)
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Errorf("Error downloading file %v ; torrent is marked for repair", err)
|
||||
log.Warnf("Cannot download file %v ; torrent is marked for repair", err)
|
||||
if torrent != nil {
|
||||
go t.Repair(torrent.AccessKey)
|
||||
}
|
||||
@@ -122,7 +122,7 @@ func streamFileToResponse(torrent *torrent.Torrent, url string, w http.ResponseW
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
|
||||
log.Errorf("Received a %s status code ; torrent is marked for repair", resp.Status)
|
||||
log.Warnf("Received a %s status code ; torrent is marked for repair", resp.Status)
|
||||
if torrent != nil {
|
||||
go t.Repair(torrent.AccessKey)
|
||||
}
|
||||
|
||||
@@ -46,20 +46,20 @@ func HandleHeadRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torren
|
||||
|
||||
torrent, _ := t.TorrentMap.Get(accessKey)
|
||||
if torrent == nil {
|
||||
log.Errorf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
|
||||
log.Warnf("Cannot find torrent %s in the directory %s", accessKey, baseDirectory)
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
file, _ := torrent.SelectedFiles.Get(filename)
|
||||
if file == nil {
|
||||
log.Errorf("Cannot find file from path %s", requestPath)
|
||||
log.Warnf("Cannot find file from path %s", requestPath)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if file.Link == "" {
|
||||
// This is a dead file, serve an alternate file
|
||||
log.Errorf("File %s is no longer available", filename)
|
||||
log.Warnf("File %s is no longer available", filename)
|
||||
http.Error(w, "Cannot find file", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user