From 568a9a9b70481f69df4aada2feb49d5054dcc993 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Tue, 7 Nov 2023 01:17:23 +0100 Subject: [PATCH] Fix error videos --- internal/dav/listing.go | 8 ++------ internal/http/listing.go | 4 ++-- internal/http/util.go | 8 -------- internal/universal/get.go | 37 ++++++++++++++++++++++++++----------- pkg/realdebrid/types.go | 11 ++++++----- 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/internal/dav/listing.go b/internal/dav/listing.go index 3d18ef8..d2653e6 100644 --- a/internal/dav/listing.go +++ b/internal/dav/listing.go @@ -42,13 +42,13 @@ func HandlePropfindRequest(w http.ResponseWriter, r *http.Request, t *torrent.To output, err = handleSingleTorrent(requestPath, w, r, t) default: log.Errorf("Request %s %s not found", r.Method, requestPath) - writeHTTPError(w, "Not Found", http.StatusNotFound) + http.Error(w, "Not Found", http.StatusNotFound) return } if err != nil { log.Errorf("Error processing request: %v", err) - writeHTTPError(w, "Server error", http.StatusInternalServerError) + http.Error(w, "Server error", http.StatusInternalServerError) return } @@ -107,7 +107,3 @@ func handleSingleTorrent(requestPath string, w http.ResponseWriter, r *http.Requ } return xml.Marshal(resp) } - -func writeHTTPError(w http.ResponseWriter, errorMessage string, statusCode int) { - http.Error(w, errorMessage, statusCode) -} diff --git a/internal/http/listing.go b/internal/http/listing.go index cdd23f2..f641062 100644 --- a/internal/http/listing.go +++ b/internal/http/listing.go @@ -39,13 +39,13 @@ func HandleDirectoryListing(w http.ResponseWriter, r *http.Request, t *torrent.T output, err = handleSingleTorrent(requestPath, w, r, t) default: log.Errorf("Request %s %s not found", r.Method, requestPath) - writeHTTPError(w, "Not Found", http.StatusNotFound) + http.Error(w, "Not Found", http.StatusNotFound) return } if err != nil { log.Errorf("Error processing request: %v", err) - writeHTTPError(w, "Server error", http.StatusInternalServerError) + http.Error(w, "Server error", http.StatusInternalServerError) return } diff --git a/internal/http/util.go b/internal/http/util.go index 8e71035..e37f3b1 100644 --- a/internal/http/util.go +++ b/internal/http/util.go @@ -1,13 +1,5 @@ package http -import ( - "net/http" -) - -func writeHTTPError(w http.ResponseWriter, errorMessage string, statusCode int) { - http.Error(w, errorMessage, statusCode) -} - func removeEmptySegments(urlSegments []string) []string { var result []string for _, s := range urlSegments { diff --git a/internal/universal/get.go b/internal/universal/get.go index 775d113..736f09b 100644 --- a/internal/universal/get.go +++ b/internal/universal/get.go @@ -43,7 +43,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent return } if data, exists := cache.Get(requestPath); exists { - streamFileToResponse(data, w, r, c.GetNetworkBufferSize(), log) + streamFileToResponse(data, w, r, c, log) return } @@ -54,7 +54,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent torrents := t.FindAllTorrentsWithName(baseDirectory, torrentName) if torrents == nil { log.Errorf("Cannot find torrent %s in the directory %s", requestPath, baseDirectory) - http.Redirect(w, r, "https://send.nukes.wtf/tDeTd0", http.StatusFound) + http.Error(w, "File not found", http.StatusNotFound) return } @@ -62,12 +62,13 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent torrent, file := getFile(torrents, filenameV2, linkFragment) if file == nil { log.Errorf("Cannot find file from path %s", requestPath) - http.Error(w, "Cannot find file", http.StatusNotFound) + http.Error(w, "File not found", http.StatusNotFound) return } if file.Link == "" { // This is a dead file, serve an alternate file log.Errorf("File %s is no longer available", filename) + streamErrorVideo("https://www.youtube.com/watch?v=bGTqwt6vdcY", w, r, c, log) return } link := file.Link @@ -81,26 +82,28 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent log.Errorf("Cannot unrestrict file %s %s", filenameV2, link) t.HideTheFile(torrent, file) } - http.Redirect(w, r, "https://send.nukes.wtf/tDeTd0", http.StatusFound) + streamErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, c, log) return } else if resp.Filename != filenameV2 { actualExt := filepath.Ext(resp.Filename) expectedExt := filepath.Ext(filenameV2) - if actualExt != expectedExt { + if actualExt != expectedExt && resp.Streamable != 1 { log.Errorf("File extension mismatch: %s and %s", filenameV2, resp.Filename) + streamErrorVideo("https://www.youtube.com/watch?v=t9VgOriBHwE", w, r, c, log) + return } else { log.Errorf("Filename mismatch: %s and %s", filenameV2, resp.Filename) } } cache.Add(requestPath, resp.Download) - streamFileToResponse(resp.Download, w, r, c.GetNetworkBufferSize(), log) + streamFileToResponse(resp.Download, w, r, c, log) } -func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, bufferSize int, log *zap.SugaredLogger) { +func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, c config.ConfigInterface, log *zap.SugaredLogger) { req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { log.Errorf("Error creating new request %v", err) - http.Redirect(w, r, "https://send.nukes.wtf/ARjVWb", http.StatusFound) + streamErrorVideo("https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, c, log) return } @@ -113,14 +116,14 @@ func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, bu resp, err := http.DefaultClient.Do(req) if err != nil { log.Errorf("Error downloading file %v", err) - http.Redirect(w, r, "https://send.nukes.wtf/TB2u2n", http.StatusFound) + streamErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, c, log) return } defer resp.Body.Close() if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent { log.Errorf("Received a nonOK status code %d", resp.StatusCode) - http.Redirect(w, r, "https://send.nukes.wtf/b5AiON", http.StatusFound) + streamErrorVideo("https://www.youtube.com/watch?v=BcseUxviVqE", w, r, c, log) return } @@ -130,6 +133,18 @@ func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, bu } } - buf := make([]byte, bufferSize) + buf := make([]byte, c.GetNetworkBufferSize()) io.CopyBuffer(w, resp.Body, buf) } + +func streamErrorVideo(link string, w http.ResponseWriter, r *http.Request, c config.ConfigInterface, log *zap.SugaredLogger) { + unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) { + return realdebrid.UnrestrictLink(c.GetToken(), link) + } + resp := realdebrid.RetryUntilOk(unrestrictFn) + if resp == nil { + http.Error(w, "REAL-DEBRID IS DOWN", http.StatusInternalServerError) + return + } + streamFileToResponse(resp.Download, w, r, c, log) +} diff --git a/pkg/realdebrid/types.go b/pkg/realdebrid/types.go index e697eba..ca08b21 100644 --- a/pkg/realdebrid/types.go +++ b/pkg/realdebrid/types.go @@ -11,11 +11,12 @@ type FileJSON struct { } type UnrestrictResponse struct { - Filename string `json:"filename"` - Filesize int64 `json:"filesize"` - Link string `json:"link"` - Host string `json:"host"` - Download string `json:"download,omitempty"` + Filename string `json:"filename"` + Filesize int64 `json:"filesize"` + Link string `json:"link"` + Host string `json:"host"` + Download string `json:"download,omitempty"` + Streamable int `json:"streamable"` } type Torrent struct {