From 083a0ebd6551c81895ec06f029f5bb647e6f2aac Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Mon, 6 Nov 2023 00:06:26 +0100 Subject: [PATCH] Implement file reads --- internal/zfs/object.go | 70 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/internal/zfs/object.go b/internal/zfs/object.go index 469fb53..dc34dab 100644 --- a/internal/zfs/object.go +++ b/internal/zfs/object.go @@ -3,6 +3,8 @@ package zfs import ( "context" "fmt" + "io" + "net/http" "os" "path/filepath" "strings" @@ -11,6 +13,7 @@ import ( "bazil.org/fuse" "bazil.org/fuse/fs" + "github.com/debridmediamanager.com/zurg/pkg/realdebrid" ) // define variable as rootObject id @@ -26,6 +29,7 @@ type Object struct { objType int parentName string name string + link string size uint64 mtime time.Time } @@ -137,6 +141,7 @@ func (o Object) Lookup(ctx context.Context, name string) (fs.Node, error) { objType: FILE, parentName: o.name, name: name, + link: file.Link, size: uint64(file.Bytes), mtime: convertRFC3339toTime(item.Added), }, nil @@ -155,7 +160,70 @@ 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 { - return fmt.Errorf("Read not yet implemented") + o.fs.log.Debugf("Reading offset %d size %d", req.Offset, req.Size) + unrestrictFn := func() (*realdebrid.UnrestrictResponse, error) { + return realdebrid.UnrestrictLink(o.fs.c.GetToken(), o.link) + } + unrestrictResp := realdebrid.RetryUntilOk(unrestrictFn) + if unrestrictResp == nil { + o.fs.log.Errorf("Cannot unrestrict link %s", o.link) + return syscall.EIO + } else if unrestrictResp.Filename != o.name { + actualExt := filepath.Ext(unrestrictResp.Filename) + expectedExt := filepath.Ext(o.name) + if actualExt != expectedExt { + o.fs.log.Errorf("File extension mismatch: expected %s, got %s", expectedExt, actualExt) + } else { + o.fs.log.Errorf("Filename mismatch: expected %s, got %s", o.name, unrestrictResp.Filename) + } + } + o.fs.log.Debugf("Unrestricted link: %s", unrestrictResp.Download) + + httpClient := &http.Client{ + Timeout: 30 * time.Second, // Set a timeout to prevent hanging + } + + streamReq, err := http.NewRequest(http.MethodGet, unrestrictResp.Download, nil) + if err != nil { + o.fs.log.Errorf("Error creating new request %v", err) + return syscall.EIO + } + + // Ensure that the byte range is correctly specified + endByte := req.Offset + int64(req.Size) - 1 + streamReq.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", req.Offset, endByte)) + o.fs.log.Debugf("Requested bytes=%d-%d", req.Offset, endByte) + + streamResp, err := httpClient.Do(streamReq.WithContext(ctx)) + if err != nil { + o.fs.log.Errorf("Error downloading file %s %v", unrestrictResp.Download, err) + return syscall.EIO + } + defer streamResp.Body.Close() + + // Check for successful status codes + if streamResp.StatusCode != http.StatusOK && streamResp.StatusCode != http.StatusPartialContent { + o.fs.log.Errorf("Received a non-ok status code %d for %s", streamResp.StatusCode, unrestrictResp.Download) + return syscall.EIO + } + + // Optionally check if the server returned the expected byte range + contentRange := streamResp.Header.Get("Content-Range") + if contentRange == "" && streamResp.StatusCode == http.StatusPartialContent { + o.fs.log.Error("Content-Range header is missing in HTTP partial content response") + return syscall.EIO + } + + // Process the stream in chunks. + // Allocate a buffer to read into. + respData, readErr := io.ReadAll(streamResp.Body) + if readErr != nil { + o.fs.log.Errorf("Error reading bytes from download response %v", readErr) + return syscall.EIO + } + resp.Data = respData + o.fs.log.Debugf("Read %d bytes in total", len(resp.Data)) + return nil } // Remove deletes an element