diff --git a/core/commands/cat.go b/core/commands/cat.go index f44f41a85..187432dca 100644 --- a/core/commands/cat.go +++ b/core/commands/cat.go @@ -147,6 +147,10 @@ func cat(ctx context.Context, api iface.CoreAPI, paths []string, offset int64, m return nil, 0, err } + if f.IsDirectory() { + return nil, 0, iface.ErrIsDir + } + file := f.(catFile) fsize, err := file.Size() diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 99f4f9c3c..615c34a77 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -179,11 +179,13 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr } dr, err := i.api.Unixfs().Get(ctx, resolvedPath) - dir := dr.IsDirectory() - switch err { - case nil: - // Cat() worked - defer dr.Close() + dir := false + switch { + case err == nil: + dir = dr.IsDirectory() + if !dir { + defer dr.Close() + } default: webError(w, "ipfs cat "+escapedURLPath, err, http.StatusNotFound) return @@ -370,7 +372,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr } type sizeReadSeeker interface { - Size() uint64 + Size() (int64, error) io.ReadSeeker } @@ -381,7 +383,7 @@ type sizeSeeker struct { func (s *sizeSeeker) Seek(offset int64, whence int) (int64, error) { if whence == io.SeekEnd && offset == 0 { - return int64(s.Size()), nil + return s.Size() } return s.sizeReadSeeker.Seek(offset, whence)