Skip to content

Commit

Permalink
gateway: custom seeker for files
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed Oct 18, 2017
1 parent e71dce5 commit 3fbb771
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
24 changes: 24 additions & 0 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,31 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
}
}

type sizeReadSeeker interface {
Size() uint64

io.ReadSeeker
}

type sizeSeeker struct {
sizeReadSeeker
}

func (s *sizeSeeker) Seek(offset int64, whence int) (int64, error) {
if whence == io.SeekEnd && offset == 0 {
return int64(s.Size()), nil
}

return s.sizeReadSeeker.Seek(offset, whence)
}

func (i *gatewayHandler) serverFile(w http.ResponseWriter, req *http.Request, name string, modtime time.Time, content io.ReadSeeker) {
if sp, ok := content.(sizeReadSeeker); ok {
content = &sizeSeeker{
sizeReadSeeker: sp,
}
}

http.ServeContent(w, req, name, modtime, content)
//TODO: check for errors in ServeContent.. somehow

Expand Down
7 changes: 7 additions & 0 deletions unixfs/io/pbdagreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ func (dr *pbDagReader) Seek(offset int64, whence int) (int64, error) {
if offset < 0 {
return -1, errors.New("Invalid offset")
}
if offset == dr.offset {
return offset, nil
}

// Grab cached protobuf object (solely to make code look cleaner)
pb := dr.pbdata
Expand Down Expand Up @@ -239,6 +242,10 @@ func (dr *pbDagReader) Seek(offset int64, whence int) (int64, error) {
return offset, nil
case io.SeekCurrent:
// TODO: be smarter here
if offset == 0 {
return dr.offset, nil
}

noffset := dr.offset + offset
return dr.Seek(noffset, io.SeekStart)
case io.SeekEnd:
Expand Down

0 comments on commit 3fbb771

Please sign in to comment.