Fix error videos

This commit is contained in:
Ben Sarmiento
2023-11-07 01:17:23 +01:00
parent 6be49d8843
commit 568a9a9b70
5 changed files with 36 additions and 32 deletions

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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 {

View File

@@ -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)
}

View File

@@ -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 {