Fix repair :)

This commit is contained in:
Ben Sarmiento
2023-11-18 23:39:42 +01:00
parent 34b1a19478
commit ff93baa6c1
7 changed files with 264 additions and 348 deletions

View File

@@ -1,7 +1,6 @@
package universal
import (
"fmt"
"io"
"net/http"
"path"
@@ -67,7 +66,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentM
}
if data, exists := cache.Get(requestPath); exists {
streamFileToResponse(data, w, r, t, c, log)
streamFileToResponse(file, data, w, r, t, c, log)
return
}
@@ -81,8 +80,9 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentM
resp := t.UnrestrictUntilOk(link)
if resp == nil {
// go t.Repair(torrent.AccessKey)
log.Warnf("File %s is no longer available, torrent is marked for repair", file.Path)
file.Link = ""
t.SetChecksum("") // force a recheck
log.Warnf("File %s is no longer available", file.Path)
streamErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, t, c, log)
return
} else if resp.Filename != filename {
@@ -99,14 +99,16 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentM
}
}
cache.Add(requestPath, resp.Download)
streamFileToResponse(resp.Download, w, r, t, c, log)
streamFileToResponse(file, resp.Download, w, r, t, c, log)
}
func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) {
func streamFileToResponse(file *intTor.File, url string, w http.ResponseWriter, r *http.Request, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) {
// Create a new request for the file download.
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Errorf("Error creating new request: %v", err)
if file != nil {
log.Errorf("Error creating new request for file %s: %v", file.Path, err)
}
streamErrorVideo("https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, torMgr, cfg, log)
return
}
@@ -121,20 +123,22 @@ func streamFileToResponse(url string, w http.ResponseWriter, r *http.Request, to
resp, err := client.Do(req)
if err != nil {
log.Warnf("Cannot download file %v ; torrent is marked for repair", err)
// if torrent != nil {
// go t.Repair(torrent.AccessKey)
// }
if file != nil {
log.Warnf("Cannot download file %s: %v", file.Path, err)
file.Link = ""
torMgr.SetChecksum("") // force a recheck
}
streamErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, torMgr, cfg, log)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
log.Warnf("Received a %s status code ; torrent is marked for repair", resp.Status)
// if torrent != nil {
// go t.Repair(torrent.AccessKey)
// }
if file != nil {
log.Warnf("Received a %s status code for file %s", resp.Status, file.Path)
file.Link = ""
torMgr.SetChecksum("") // force a recheck
}
streamErrorVideo("https://www.youtube.com/watch?v=BcseUxviVqE", w, r, torMgr, cfg, log)
return
}
@@ -155,83 +159,5 @@ func streamErrorVideo(link string, w http.ResponseWriter, r *http.Request, t *in
http.Error(w, "REAL-DEBRID IS DOWN", http.StatusInternalServerError)
return
}
streamFileToResponse(resp.Download, w, r, t, c, log)
}
func createErrorFile(path, link string) *intTor.File {
ret := intTor.File{
Link: link,
}
ret.Path = path
return &ret
}
func GetFileReader(file *intTor.File, offset int64, size int, torMgr *intTor.TorrentManager, cfg config.ConfigInterface, log *zap.SugaredLogger) []byte {
unres := torMgr.UnrestrictUntilOk(file.Link)
if unres == nil {
if strings.Contains(file.Link, "www.youtube.com") {
log.Errorf("Even the error page is broken! Sorry!")
return nil
}
log.Warnf("File %s is no longer available, torrent is marked for repair", file.Path)
// if torrent != nil {
// go torMgr.Repair(torrent.AccessKey)
// }
errFile := createErrorFile("unavailable.mp4", "https://www.youtube.com/watch?v=gea_FJrtFVA")
return GetFileReader(errFile, 0, 0, torMgr, cfg, log)
}
req, err := http.NewRequest(http.MethodGet, unres.Download, nil)
if err != nil {
if strings.Contains(file.Link, "www.youtube.com") {
log.Errorf("Even the error page is broken! Sorry!")
return nil
}
log.Errorf("Error creating new request: %v", err)
errFile := createErrorFile("new_request.mp4", "https://www.youtube.com/watch?v=H3NSrObyAxM")
return GetFileReader(errFile, 0, 0, torMgr, cfg, log)
}
if size == 0 {
size = int(file.Bytes)
}
req.Header.Add("Range", fmt.Sprintf("bytes=%v-%v", offset, offset+int64(size)-1))
client := zurghttp.NewHTTPClient(cfg.GetToken(), 10, cfg)
resp, err := client.Do(req)
if err != nil {
if strings.Contains(file.Link, "www.youtube.com") {
log.Errorf("Even the error page is broken! Sorry!")
return nil
}
log.Warnf("Cannot download file %v ; torrent is marked for repair", err)
// if torrent != nil {
// go torMgr.Repair(torrent.AccessKey)
// }
errFile := createErrorFile("cannot_download.mp4", "https://www.youtube.com/watch?v=FSSd8cponAA")
return GetFileReader(errFile, 0, 0, torMgr, cfg, log)
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusPartialContent {
if strings.Contains(file.Link, "www.youtube.com") {
log.Errorf("Even the error page is broken! Sorry!")
return nil
}
log.Warnf("Received a %s status code ; torrent is marked for repair", resp.Status)
// if torrent != nil {
// go torMgr.Repair(torrent.AccessKey)
// }
errFile := createErrorFile("not_ok_status.mp4", "https://www.youtube.com/watch?v=BcseUxviVqE")
return GetFileReader(errFile, 0, 0, torMgr, cfg, log)
}
defer resp.Body.Close()
requestedBytes, err := io.ReadAll(resp.Body)
if err != nil {
if err != io.EOF {
log.Errorf("Error reading bytes: %v", err)
errFile := createErrorFile("read_error.mp4", "https://www.youtube.com/watch?v=t9VgOriBHwE")
return GetFileReader(errFile, 0, 0, torMgr, cfg, log)
}
}
return requestedBytes
streamFileToResponse(nil, resp.Download, w, r, t, c, log)
}