Skip to content

Commit

Permalink
Merge pull request ipfs/kubo#4320 from ipfs/fix/gateway-seeker
Browse files Browse the repository at this point in the history
gateway: fix seeker can't seek on specific files

This commit was moved from ipfs/kubo@005d243
  • Loading branch information
whyrusleeping authored Oct 20, 2017
2 parents fcb3702 + 81b07f5 commit 63329a3
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion gateway/core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr

if !dir {
name := gopath.Base(urlPath)
http.ServeContent(w, r, name, modtime, dr)
i.serveFile(w, r, name, modtime, dr)
return
}

Expand Down Expand Up @@ -372,6 +372,34 @@ 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) serveFile(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)
}

func (i *gatewayHandler) postHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
p, err := i.api.Unixfs().Add(ctx, r.Body)
if err != nil {
Expand Down

0 comments on commit 63329a3

Please sign in to comment.