From 464f522fea9e708cd570a8350daab8a27660b409 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Fri, 20 Oct 2023 01:38:05 +0200 Subject: [PATCH] Proper logging --- internal/dav/aero.go | 4 ++-- internal/dav/getfile.go | 4 ++-- internal/dav/router.go | 2 +- internal/dav/util.go | 2 +- pkg/realdebrid/api.go | 41 ++++++++++++++++++++++++++++++++++++++++- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/internal/dav/aero.go b/internal/dav/aero.go index 6304578..6027877 100644 --- a/internal/dav/aero.go +++ b/internal/dav/aero.go @@ -82,7 +82,7 @@ func Setup(app *aero.Application, c config.ConfigInterface, t *torrent.TorrentMa filenameV2, linkFragment := davextra.ExtractLinkFragment(filename) link := getLink(torrents, filenameV2, linkFragment) if link == "" { - log.Println("Link not found") + log.Println("Link not found", filename) return ctx.Error(http.StatusNotFound, "Cannot find file") } @@ -94,7 +94,7 @@ func Setup(app *aero.Application, c config.ConfigInterface, t *torrent.TorrentMa // TODO: Readd the file // when unrestricting fails, it means the file is not available anymore // if it's the only file, tough luck - log.Println("Cannot unrestrict link") + log.Println("Cannot unrestrict link", link, filenameV2) return ctx.Error(http.StatusNotFound, "Cannot find file") } if resp.Filename != filenameV2 { diff --git a/internal/dav/getfile.go b/internal/dav/getfile.go index 9e395e0..7069c10 100644 --- a/internal/dav/getfile.go +++ b/internal/dav/getfile.go @@ -42,7 +42,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent filenameV2, linkFragment := davextra.ExtractLinkFragment(filename) link := getLink(torrents, filenameV2, linkFragment) if link == "" { - log.Println("Link not found") + log.Println("Link not found", filename) http.Error(w, "Cannot find file", http.StatusNotFound) return } @@ -55,7 +55,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent // TODO: Readd the file // when unrestricting fails, it means the file is not available anymore, but still in their database // if it's the only file, tough luck - log.Println("Cannot unrestrict link") + log.Println("Cannot unrestrict link", link, filenameV2) http.Error(w, "Cannot find file", http.StatusNotFound) return } diff --git a/internal/dav/router.go b/internal/dav/router.go index 12aed98..625616c 100644 --- a/internal/dav/router.go +++ b/internal/dav/router.go @@ -22,7 +22,7 @@ func Router(mux *http.ServeMux, c config.ConfigInterface, t *torrent.TorrentMana w.WriteHeader(http.StatusOK) default: - log.Println("Method not implemented") + log.Println("Method not implemented", r.Method) http.Error(w, "Method not implemented", http.StatusMethodNotAllowed) } }) diff --git a/internal/dav/util.go b/internal/dav/util.go index 8e0770b..294c3a8 100644 --- a/internal/dav/util.go +++ b/internal/dav/util.go @@ -12,7 +12,7 @@ import ( func convertRFC3339toRFC1123(input string) string { t, err := time.Parse(time.RFC3339, input) if err != nil { - log.Println("Error:", err) + log.Println("Conversion error", err) return "" } return t.Format("Mon, 02 Jan 2006 15:04:05 GMT") diff --git a/pkg/realdebrid/api.go b/pkg/realdebrid/api.go index b7078b5..913afc3 100644 --- a/pkg/realdebrid/api.go +++ b/pkg/realdebrid/api.go @@ -81,7 +81,46 @@ func UnrestrictLink(accessToken, link string) (*UnrestrictResponse, error) { return nil, err } - return &response, nil + if canFetchFirstByte(response.Download) { + return &response, nil + } + return nil, fmt.Errorf("can't fetch first byte") +} + +func canFetchFirstByte(url string) bool { + // Create a new HTTP request + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return false + } + + // Set the Range header to request only the first byte + req.Header.Set("Range", "bytes=0-0") + + // Execute the request + resp, err := http.DefaultClient.Do(req) + if err != nil { + return false + } + defer resp.Body.Close() + + // If server supports partial content + if resp.StatusCode == http.StatusPartialContent { + buffer := make([]byte, 1) + _, err := resp.Body.Read(buffer) + if err != nil { + return false + } + return true + } + + // If server doesn't support partial content, try reading the first byte and immediately close + buffer := make([]byte, 1) + _, err = io.ReadFull(resp.Body, buffer) + if err != nil { + return false + } + return true } // GetTorrents returns all torrents, paginated