feat: add support for a range offset in GetFile

This commit is contained in:
Derrick Hammer 2024-01-24 19:05:54 -05:00
parent 95b57cffc0
commit dcf05974e2
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 19 additions and 8 deletions

View File

@ -18,7 +18,7 @@ type StorageService interface {
FileExists(hash []byte) (bool, models.Upload) FileExists(hash []byte) (bool, models.Upload)
GetHashSmall(file io.ReadSeeker) ([]byte, error) GetHashSmall(file io.ReadSeeker) ([]byte, error)
GetHash(file io.Reader) ([]byte, int64, error) GetHash(file io.Reader) ([]byte, int64, error)
GetFile(hash []byte) (io.ReadCloser, uint64, error) GetFile(hash []byte, start int64) (io.ReadCloser, int64, error)
CreateUpload(hash []byte, uploaderID uint, uploaderIP string, size uint64, protocol string) (*models.Upload, error) CreateUpload(hash []byte, uploaderID uint, uploaderIP string, size uint64, protocol string) (*models.Upload, error)
TusUploadExists(hash []byte) (bool, models.TusUpload) TusUploadExists(hash []byte) (bool, models.TusUpload)
CreateTusUpload(hash []byte, uploadID string, uploaderID uint, uploaderIP string, protocol string) (*models.TusUpload, error) CreateTusUpload(hash []byte, uploadID string, uploaderID uint, uploaderIP string, protocol string) (*models.TusUpload, error)

View File

@ -5,6 +5,7 @@ import (
"context" "context"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt"
"git.lumeweb.com/LumeWeb/libs5-go/encoding" "git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/portal/api/middleware" "git.lumeweb.com/LumeWeb/portal/api/middleware"
@ -23,6 +24,7 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
"io" "io"
"lukechampine.com/blake3" "lukechampine.com/blake3"
"net/http"
"strings" "strings"
"time" "time"
) )
@ -598,7 +600,7 @@ func splitS3Ids(id string) (objectId, multipartId string) {
return return
} }
func (s *StorageServiceImpl) GetFile(hash []byte) (io.ReadCloser, uint64, error) { func (s *StorageServiceImpl) GetFile(hash []byte, start int64) (io.ReadCloser, int64, error) {
if exists, tusUpload := s.TusUploadExists(hash); exists { if exists, tusUpload := s.TusUploadExists(hash); exists {
if tusUpload.Completed { if tusUpload.Completed {
upload, err := s.tusStore.GetUpload(context.Background(), tusUpload.UploadID) upload, err := s.tusStore.GetUpload(context.Background(), tusUpload.UploadID)
@ -607,10 +609,9 @@ func (s *StorageServiceImpl) GetFile(hash []byte) (io.ReadCloser, uint64, error)
} }
info, _ := upload.GetInfo(context.Background()) info, _ := upload.GetInfo(context.Background())
reader, err := upload.GetReader(context.Background()) reader, err := upload.GetReader(context.Background())
return reader, uint64(info.Size), err return reader, info.Size, err
} }
} }
@ -625,16 +626,26 @@ func (s *StorageServiceImpl) GetFile(hash []byte) (io.ReadCloser, uint64, error)
return nil, 0, err return nil, 0, err
} }
resp, err := s.httpApi.R(). request := s.httpApi.R().
SetPathParam("path", hashStr). SetPathParam("path", hashStr).
SetQueryParam("bucket", upload.Protocol). SetQueryParam("bucket", upload.Protocol).
DisableAutoReadResponse(). DisableAutoReadResponse()
Get("/api/worker/objects/{path}")
if start > 0 {
rangeHeader := fmt.Sprintf("bytes=%d-", start)
request.SetHeader("Range", rangeHeader)
}
resp, err := request.Get("/api/worker/objects/{path}")
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err
} }
if start > 0 && resp.StatusCode != http.StatusPartialContent {
return nil, 0, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
if resp.IsError() { if resp.IsError() {
if resp.Error() != nil { if resp.Error() != nil {
return nil, 0, resp.Error().(error) return nil, 0, resp.Error().(error)
@ -644,5 +655,5 @@ func (s *StorageServiceImpl) GetFile(hash []byte) (io.ReadCloser, uint64, error)
} }
return resp.Body, upload.Size, nil return resp.Body, int64(upload.Size), nil
} }