Skip to content

Commit

Permalink
refactor: reuse seekToRangeStart
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel authored and hacdias committed Sep 25, 2023
1 parent b2e3ea7 commit 1f0672f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
16 changes: 7 additions & 9 deletions gateway/blocks_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ func (bb *BlocksBackend) Get(ctx context.Context, path ImmutablePath, ranges ...
return md, nil, err
}

// Only a single range is supported
// Only a single range is supported in responses to HTTP Range Requests.
// When more than one is passed in the Range header, this library will
// return a response for the first one and ignores remaining ones.
var ra *ByteRange
if len(ranges) > 0 {
ra = &ranges[0]
Expand All @@ -170,10 +172,8 @@ func (bb *BlocksBackend) Get(ctx context.Context, path ImmutablePath, ranges ...
return ContentPathMetadata{}, nil, err
}

if ra != nil && ra.From != 0 {
if _, err := f.Seek(int64(ra.From), io.SeekStart); err != nil {
return ContentPathMetadata{}, nil, err
}
if err := seekToRangeStart(f, ra); err != nil {
return ContentPathMetadata{}, nil, err
}

return md, NewGetResponseFromReader(f, fileSize), nil
Expand Down Expand Up @@ -206,10 +206,8 @@ func (bb *BlocksBackend) Get(ctx context.Context, path ImmutablePath, ranges ...
return ContentPathMetadata{}, nil, err
}

if ra != nil && ra.From != 0 {
if _, err := file.Seek(int64(ra.From), io.SeekStart); err != nil {
return ContentPathMetadata{}, nil, err
}
if err := seekToRangeStart(file, ra); err != nil {
return ContentPathMetadata{}, nil, err
}

if s, ok := f.(*files.Symlink); ok {
Expand Down
14 changes: 11 additions & 3 deletions gateway/serve_http_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,8 @@ func (i *handler) seekToStartOfFirstRange(w http.ResponseWriter, r *http.Request
return false
}
if len(ranges) > 0 {
ra := ranges[0]
// Seek to start of first range
_, err := data.Seek(int64(ra.From), io.SeekStart)
ra := &ranges[0]
err = seekToRangeStart(data, ra)
if err != nil {
i.webError(w, r, fmt.Errorf("could not seek to location in range request: %w", err), http.StatusBadRequest)
return false
Expand All @@ -462,3 +461,12 @@ func (i *handler) seekToStartOfFirstRange(w http.ResponseWriter, r *http.Request
}
return true
}

func seekToRangeStart(data io.Seeker, ra *ByteRange) error {
if ra != nil && ra.From != 0 {
if _, err := data.Seek(int64(ra.From), io.SeekStart); err != nil {
return err
}
}
return nil
}

0 comments on commit 1f0672f

Please sign in to comment.