Implement file reads
This commit is contained in:
@@ -3,6 +3,8 @@ package zfs
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -11,6 +13,7 @@ import (
|
|||||||
|
|
||||||
"bazil.org/fuse"
|
"bazil.org/fuse"
|
||||||
"bazil.org/fuse/fs"
|
"bazil.org/fuse/fs"
|
||||||
|
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// define variable as rootObject id
|
// define variable as rootObject id
|
||||||
@@ -26,6 +29,7 @@ type Object struct {
|
|||||||
objType int
|
objType int
|
||||||
parentName string
|
parentName string
|
||||||
name string
|
name string
|
||||||
|
link string
|
||||||
size uint64
|
size uint64
|
||||||
mtime time.Time
|
mtime time.Time
|
||||||
}
|
}
|
||||||
@@ -137,6 +141,7 @@ func (o Object) Lookup(ctx context.Context, name string) (fs.Node, error) {
|
|||||||
objType: FILE,
|
objType: FILE,
|
||||||
parentName: o.name,
|
parentName: o.name,
|
||||||
name: name,
|
name: name,
|
||||||
|
link: file.Link,
|
||||||
size: uint64(file.Bytes),
|
size: uint64(file.Bytes),
|
||||||
mtime: convertRFC3339toTime(item.Added),
|
mtime: convertRFC3339toTime(item.Added),
|
||||||
}, nil
|
}, 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
|
// Read reads some bytes or the whole file
|
||||||
func (o Object) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
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
|
// Remove deletes an element
|
||||||
|
|||||||
Reference in New Issue
Block a user