Chunk manager init

This commit is contained in:
Ben Sarmiento
2023-11-06 01:22:56 +01:00
parent 90aaceec77
commit a39c72523d
5 changed files with 31 additions and 74 deletions

View File

@@ -3,8 +3,6 @@ package zfs
import (
"context"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
@@ -13,7 +11,7 @@ import (
"bazil.org/fuse"
"bazil.org/fuse/fs"
"github.com/debridmediamanager.com/zurg/pkg/realdebrid"
"github.com/debridmediamanager.com/zurg/internal/torrent"
)
// define variable as rootObject id
@@ -29,7 +27,7 @@ type Object struct {
objType int
parentName string
name string
link string
file *torrent.File
size uint64
mtime time.Time
}
@@ -141,7 +139,7 @@ func (o Object) Lookup(ctx context.Context, name string) (fs.Node, error) {
objType: FILE,
parentName: o.name,
name: name,
link: file.Link,
file: &file,
size: uint64(file.Bytes),
mtime: convertRFC3339toTime(item.Added),
}, nil
@@ -160,69 +158,13 @@ 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("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
data, err := o.fs.chunk.GetChunk(o.file, req.Offset, int64(req.Size))
if nil != err {
o.fs.log.Warnf("%v", err)
return fuse.EIO
}
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))
resp.Data = data
return nil
}