diff --git a/main.go b/main.go index 3d22d61..8aff7dd 100644 --- a/main.go +++ b/main.go @@ -5,10 +5,7 @@ import ( "flag" "github.com/sirrobot01/decypharr/cmd/decypharr" "github.com/sirrobot01/decypharr/internal/config" - "github.com/sirrobot01/decypharr/pkg/version" "log" - "net/http" - _ "net/http/pprof" // registers pprof handlers "os" "os/signal" "runtime/debug" @@ -22,22 +19,13 @@ func main() { debug.PrintStack() } }() - - if version.GetInfo().Channel == "dev" { - log.Println("Running in dev mode") - go func() { - if err := http.ListenAndServe(":6060", nil); err != nil { - log.Fatalf("pprof server failed: %v", err) - } - }() - } var configPath string flag.StringVar(&configPath, "config", "/data", "path to the data folder") flag.Parse() config.SetConfigPath(configPath) config.Get() - // Create a context that's cancelled on SIGINT/SIGTERM + // Create a context canceled on SIGINT/SIGTERM ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM) defer stop() diff --git a/pkg/qbit/downloader.go b/pkg/qbit/downloader.go index 7aa1295..0071865 100644 --- a/pkg/qbit/downloader.go +++ b/pkg/qbit/downloader.go @@ -3,10 +3,10 @@ package qbit import ( "fmt" "github.com/cavaliergopher/grab/v3" - "github.com/sirrobot01/decypharr/internal/request" "github.com/sirrobot01/decypharr/internal/utils" debridTypes "github.com/sirrobot01/decypharr/pkg/debrid/types" "io" + "net/http" "os" "path/filepath" "sync" @@ -20,7 +20,7 @@ func Download(client *grab.Client, url, filename string, progressCallback func(i } resp := client.Do(req) - t := time.NewTicker(time.Second) + t := time.NewTicker(time.Second * 2) defer t.Stop() var lastReported int64 @@ -92,9 +92,14 @@ func (q *QBit) downloadFiles(torrent *Torrent, parent string) { q.UpdateTorrentMin(torrent, debridTorrent) } client := &grab.Client{ - UserAgent: "Decypharr[QBitTorrent]", - HTTPClient: request.New(request.WithTimeout(0)), + UserAgent: "Decypharr[QBitTorrent]", + HTTPClient: &http.Client{ + Transport: &http.Transport{ + Proxy: http.ProxyFromEnvironment, + }, + }, } + errChan := make(chan error, len(debridTorrent.Files)) for _, file := range debridTorrent.Files { if file.DownloadLink == nil { q.logger.Info().Msgf("No download link found for %s", file.Name) @@ -116,12 +121,25 @@ func (q *QBit) downloadFiles(torrent *Torrent, parent string) { if err != nil { q.logger.Error().Msgf("Failed to download %s: %v", filename, err) + errChan <- err } else { q.logger.Info().Msgf("Downloaded %s", filename) } }(file) } wg.Wait() + + close(errChan) + var errors []error + for err := range errChan { + if err != nil { + errors = append(errors, err) + } + } + if len(errors) > 0 { + q.logger.Error().Msgf("Errors occurred during download: %v", errors) + return + } q.logger.Info().Msgf("Downloaded all files for %s", debridTorrent.Name) } diff --git a/pkg/web/api.go b/pkg/web/api.go index dbc3bbd..b3ce47b 100644 --- a/pkg/web/api.go +++ b/pkg/web/api.go @@ -151,7 +151,7 @@ func (ui *Handler) handleGetTorrents(w http.ResponseWriter, r *http.Request) { func (ui *Handler) handleDeleteTorrent(w http.ResponseWriter, r *http.Request) { hash := chi.URLParam(r, "hash") - category := r.URL.Query().Get("category") + category := chi.URLParam(r, "category") removeFromDebrid := r.URL.Query().Get("removeFromDebrid") == "true" if hash == "" { http.Error(w, "No hash provided", http.StatusBadRequest) diff --git a/pkg/web/templates/layout.html b/pkg/web/templates/layout.html index 1da7133..a4abe63 100644 --- a/pkg/web/templates/layout.html +++ b/pkg/web/templates/layout.html @@ -200,8 +200,15 @@ window.urlBase = "{{.URLBase}}"; - function joinURL(...segments) { - return segments.join('/').replace(/[/]+/g, '/').replace(/^(.+):\//, '$1://').replace(/^file:/, 'file:/').replace(/\/(?=[?&#])/g, '').replace(/\?/g, '&'); + function joinURL(base, path) { + if (!base.endsWith('/')) { + base += '/'; + } + if (path.startsWith('/')) { + path = path.substring(1); + } + return base + path; + } function fetcher(endpoint, options = {}) { diff --git a/pkg/webdav/misc.go b/pkg/webdav/misc.go index 71aacb0..3521c74 100644 --- a/pkg/webdav/misc.go +++ b/pkg/webdav/misc.go @@ -1,8 +1,6 @@ package webdav import ( - "github.com/go-chi/chi/v5" - "github.com/sirrobot01/decypharr/internal/utils" "github.com/stanNthe5/stringbuf" "net/http" "net/url" @@ -146,19 +144,3 @@ func writeXml(w http.ResponseWriter, status int, buf stringbuf.StringBuf) { w.WriteHeader(status) _, _ = w.Write(buf.Bytes()) } - -func getParam(r *http.Request, key string) string { - if r.URL == nil || r.URL.Query() == nil { - return "" - } - if v := chi.URLParam(r, key); v != "" { - return utils.PathUnescape(v) - } - if v := r.URL.Query().Get(key); v != "" { - return utils.PathUnescape(v) - } - if v := r.FormValue(key); v != "" { - return utils.PathUnescape(v) - } - return "" -}