coreapi unixfs: Return seeker from get

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
This commit is contained in:
Łukasz Magiera 2018-10-15 12:45:49 +02:00
parent 7915d26090
commit faf5230e69
5 changed files with 25 additions and 18 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)