diff --git a/internal/universal/get.go b/internal/universal/get.go index 0947cf6..735d585 100644 --- a/internal/universal/get.go +++ b/internal/universal/get.go @@ -11,7 +11,7 @@ import ( "github.com/debridmediamanager.com/zurg/internal/config" "github.com/debridmediamanager.com/zurg/internal/dav" intHttp "github.com/debridmediamanager.com/zurg/internal/http" - "github.com/debridmediamanager.com/zurg/internal/torrent" + intTor "github.com/debridmediamanager.com/zurg/internal/torrent" zurghttp "github.com/debridmediamanager.com/zurg/pkg/http" "github.com/debridmediamanager.com/zurg/pkg/logutil" "github.com/hashicorp/golang-lru/v2/expirable" @@ -19,7 +19,7 @@ import ( ) // HandleGetRequest handles a GET request universally for both WebDAV and HTTP -func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) { +func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *intTor.TorrentManager, c config.ConfigInterface, cache *expirable.LRU[string, string]) { log := logutil.NewLogger().Named("uniget") requestPath := path.Clean(r.URL.Path) @@ -80,6 +80,8 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent streamErrorVideo("https://www.youtube.com/watch?v=gea_FJrtFVA", w, r, t, c, log) return } else if resp.Filename != filename { + // this is possible if there's only 1 streamable file in the torrent + // and then suddenly it's a rar file actualExt := filepath.Ext(resp.Filename) expectedExt := filepath.Ext(filename) if actualExt != expectedExt && resp.Streamable != 1 { @@ -94,7 +96,7 @@ func HandleGetRequest(w http.ResponseWriter, r *http.Request, t *torrent.Torrent streamFileToResponse(torrent, resp.Download, w, r, t, c, log) } -func streamFileToResponse(torrent *torrent.Torrent, url string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) { +func streamFileToResponse(torrent *intTor.Torrent, url string, w http.ResponseWriter, r *http.Request, t *intTor.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) { // Create a new request for the file download. req, err := http.NewRequest(http.MethodGet, url, nil) if err != nil { @@ -141,44 +143,7 @@ func streamFileToResponse(torrent *torrent.Torrent, url string, w http.ResponseW io.CopyBuffer(w, resp.Body, buf) } -func StreamFile(torrent *torrent.Torrent, url string, offset int64, size int, t *torrent.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) io.ReadCloser { - // 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) - // streamErrorVideo("https://www.youtube.com/watch?v=H3NSrObyAxM", w, r, t, c, log) - return nil - } - - // pass range header from offset and size - req.Header.Add("Range", "bytes="+fmt.Sprintf("%d-%d", offset, size-1)) - - // Create a custom HTTP client - client := zurghttp.NewHTTPClient(c.GetToken(), 10, c) - - 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) - } - // streamErrorVideo("https://www.youtube.com/watch?v=FSSd8cponAA", w, r, t, c, log) - return nil - } - - 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) - } - // streamErrorVideo("https://www.youtube.com/watch?v=BcseUxviVqE", w, r, t, c, log) - return nil - } - - return resp.Body -} - -func streamErrorVideo(link string, w http.ResponseWriter, r *http.Request, t *torrent.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) { +func streamErrorVideo(link string, w http.ResponseWriter, r *http.Request, t *intTor.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) { resp := t.UnrestrictUntilOk(link) if resp == nil { http.Error(w, "REAL-DEBRID IS DOWN", http.StatusInternalServerError) @@ -186,3 +151,73 @@ func streamErrorVideo(link string, w http.ResponseWriter, r *http.Request, t *to } streamFileToResponse(nil, 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(torrent *intTor.Torrent, file *intTor.File, offset int64, size int, t *intTor.TorrentManager, c config.ConfigInterface, log *zap.SugaredLogger) io.ReadCloser { + unres := t.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 t.Repair(torrent.AccessKey) + } + errFile := createErrorFile("unavailable.mp4", "https://www.youtube.com/watch?v=gea_FJrtFVA") + return GetFileReader(nil, errFile, 0, 0, t, c, 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(nil, errFile, 0, 0, t, c, log) + } + + if size == 0 { + size = int(file.Bytes) + } + req.Header.Add("Range", fmt.Sprintf("bytes=%v-%v", offset, size-1)) + + client := zurghttp.NewHTTPClient(c.GetToken(), 10, c) + 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 t.Repair(torrent.AccessKey) + } + errFile := createErrorFile("cannot_download.mp4", "https://www.youtube.com/watch?v=FSSd8cponAA") + return GetFileReader(nil, errFile, 0, 0, t, c, 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 t.Repair(torrent.AccessKey) + } + errFile := createErrorFile("not_ok_status.mp4", "https://www.youtube.com/watch?v=BcseUxviVqE") + return GetFileReader(nil, errFile, 0, 0, t, c, log) + } + + return resp.Body +} diff --git a/internal/zfs/object.go b/internal/zfs/object.go index 22a8a1e..b7f61ad 100644 --- a/internal/zfs/object.go +++ b/internal/zfs/object.go @@ -11,6 +11,7 @@ import ( "bazil.org/fuse" "bazil.org/fuse/fs" "github.com/debridmediamanager.com/zurg/internal/torrent" + "github.com/debridmediamanager.com/zurg/internal/universal" ) // define variable as rootObject id @@ -26,6 +27,7 @@ type Object struct { objType int parentName string name string + torrent *torrent.Torrent file *torrent.File size uint64 mtime time.Time @@ -136,6 +138,7 @@ func (o Object) Lookup(ctx context.Context, name string) (fs.Node, error) { objType: FILE, parentName: o.name, name: name, + torrent: torrent, file: file, size: uint64(file.Bytes), mtime: convertRFC3339toTime(file.Added), @@ -152,14 +155,17 @@ func (o Object) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.Open // Read reads some bytes or the whole file func (o Object) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { - o.fs.log.Debugf("Read %s (total size %d) req offset %d req size %d", o.name, o.size, req.Offset, req.Size) - data, err := o.fs.chunk.GetChunk(o.file, req.Offset, int64(req.Size)) - if nil != err { - o.fs.log.Warnf("%v", err) + reader := universal.GetFileReader(o.torrent, o.file, req.Offset, int(req.Size), o.fs.t, o.fs.c, o.fs.log) + if reader == nil { + return syscall.EIO + } + defer reader.Close() + resp.Data = make([]byte, req.Size) + _, err := reader.Read(resp.Data) + if err != nil && err.Error() != "EOF" { + o.fs.log.Errorf("Error reading bytes from Real-Debrid: %v", err) return syscall.EIO } - - resp.Data = data return nil } diff --git a/pkg/http/client.go b/pkg/http/client.go index 342fd91..148c48d 100644 --- a/pkg/http/client.go +++ b/pkg/http/client.go @@ -22,7 +22,8 @@ type HTTPClient struct { func (r *HTTPClient) Do(req *http.Request) (*http.Response, error) { if r.config != nil && strings.Contains(req.Host, "download.real-debrid.") { - if host := r.config.GetRandomPreferredHost(); host != "" { + prefHost := r.config.GetRandomPreferredHost() + if host := prefHost; host != "" { req.Host = r.config.GetRandomPreferredHost() } } @@ -56,7 +57,7 @@ func NewHTTPClient(token string, maxRetries int, c config.ConfigInterface) *HTTP if resp.StatusCode == 429 { return true } - // no need to retry + // no need to retry) return false }, log: logutil.NewLogger().Named("client"),