From faf5230e69231c701795bf4591fc48327cd0d618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 15 Oct 2018 12:45:49 +0200 Subject: [PATCH] coreapi unixfs: Return seeker from get MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/commands/cat.go | 15 +++------------ core/coreapi/interface/unixfs.go | 8 +++++++- core/coreapi/unixfile.go | 14 ++++++++++++-- core/coreapi/unixfs.go | 2 +- core/corehttp/gateway_handler.go | 4 ++-- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/core/commands/cat.go b/core/commands/cat.go index 187432dca..8c04a434b 100644 --- a/core/commands/cat.go +++ b/core/commands/cat.go @@ -6,11 +6,10 @@ import ( "io" "os" - cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" + "github.com/ipfs/go-ipfs/core/commands/cmdenv" "github.com/ipfs/go-ipfs/core/coreapi/interface" cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds" - "gx/ipfs/QmZMWMvWMVKCbHetJ4RgndbuEF1io2UpUxwQwtNjtYPzSC/go-ipfs-files" "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) @@ -124,12 +123,6 @@ var CatCmd = &cmds.Command{ }, } -type catFile interface { - files.SizeFile - - io.Seeker -} - func cat(ctx context.Context, api iface.CoreAPI, paths []string, offset int64, max int64) ([]io.Reader, uint64, error) { readers := make([]io.Reader, 0, len(paths)) length := uint64(0) @@ -142,17 +135,15 @@ func cat(ctx context.Context, api iface.CoreAPI, paths []string, offset int64, m return nil, 0, err } - f, err := api.Unixfs().Get(ctx, fpath) + file, err := api.Unixfs().Get(ctx, fpath) if err != nil { return nil, 0, err } - if f.IsDirectory() { + if file.IsDirectory() { return nil, 0, iface.ErrIsDir } - file := f.(catFile) - fsize, err := file.Size() if err != nil { return nil, 0, err diff --git a/core/coreapi/interface/unixfs.go b/core/coreapi/interface/unixfs.go index 4a6c956a0..dd7e5a392 100644 --- a/core/coreapi/interface/unixfs.go +++ b/core/coreapi/interface/unixfs.go @@ -2,6 +2,7 @@ package iface import ( "context" + "io" options "github.com/ipfs/go-ipfs/core/coreapi/interface/options" @@ -17,6 +18,11 @@ type AddEvent struct { Size string `json:",omitempty"` } +type UnixfsFile interface { + files.SizeFile + io.Seeker +} + // UnixfsAPI is the basic interface to immutable files in IPFS // NOTE: This API is heavily WIP, things are guaranteed to break frequently type UnixfsAPI interface { @@ -29,7 +35,7 @@ type UnixfsAPI interface { // // Note that some implementations of this API may apply the specified context // to operations performed on the returned file - Get(context.Context, Path) (files.File, error) + Get(context.Context, Path) (UnixfsFile, error) // Ls returns the list of links in a directory Ls(context.Context, Path) ([]*ipld.Link, error) diff --git a/core/coreapi/unixfile.go b/core/coreapi/unixfile.go index b7c3ae2bf..844863bd4 100644 --- a/core/coreapi/unixfile.go +++ b/core/coreapi/unixfile.go @@ -8,6 +8,8 @@ import ( gopath "path" "time" + "github.com/ipfs/go-ipfs/core/coreapi/interface" + dag "gx/ipfs/QmVvNkTCx8V9Zei8xuTYTBdUXmbnDRS4iNuw1SztYyhQwQ/go-merkledag" ft "gx/ipfs/QmWE6Ftsk98cG2MTVgH4wJT8VP2nL9TuBkYTrz9GSqcsh5/go-unixfs" uio "gx/ipfs/QmWE6Ftsk98cG2MTVgH4wJT8VP2nL9TuBkYTrz9GSqcsh5/go-unixfs/io" @@ -95,6 +97,14 @@ func (d *ufsDirectory) NextFile() (files.File, error) { return newUnixfsFile(d.ctx, d.dserv, nd, l.Name, d) } +func (d *ufsDirectory) Size() (int64, error) { + return 0, files.ErrNotReader +} + +func (d *ufsDirectory) Seek(offset int64, whence int) (int64, error) { + return 0, files.ErrNotReader +} + type ufsFile struct { uio.DagReader @@ -122,7 +132,7 @@ func (f *ufsFile) Size() (int64, error) { return int64(f.DagReader.Size()), nil } -func newUnixfsDir(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, name string, path string) (files.File, error) { +func newUnixfsDir(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, name string, path string) (iface.UnixfsFile, error) { dir, err := uio.NewDirectoryFromNode(dserv, nd) if err != nil { return nil, err @@ -153,7 +163,7 @@ func newUnixfsDir(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, name }, nil } -func newUnixfsFile(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, name string, parent files.File) (files.File, error) { +func newUnixfsFile(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, name string, parent files.File) (iface.UnixfsFile, error) { path := name if parent != nil { path = gopath.Join(parent.FullPath(), name) diff --git a/core/coreapi/unixfs.go b/core/coreapi/unixfs.go index d36955d34..0b725a591 100644 --- a/core/coreapi/unixfs.go +++ b/core/coreapi/unixfs.go @@ -133,7 +133,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options return coreiface.IpfsPath(nd.Cid()), nil } -func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (files.File, error) { +func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (coreiface.UnixfsFile, error) { nd, err := api.core().ResolveNode(ctx, p) if err != nil { return nil, err diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 615c34a77..29db4c98b 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -270,7 +270,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr } else { name = getFilename(urlPath) } - i.serveFile(w, r, name, modtime, dr.(io.ReadSeeker)) + i.serveFile(w, r, name, modtime, dr) return } @@ -305,7 +305,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr defer dr.Close() // write to request - http.ServeContent(w, r, "index.html", modtime, dr.(io.ReadSeeker)) + http.ServeContent(w, r, "index.html", modtime, dr) return default: internalWebError(w, err)