coreapi unixfs: Return seeker from get
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Oct 15, 2018 at 12:45 UTC
faf5230e69231c701795bf4591fc48327cd0d618
5 files changed
+25
-18
core/commands/cat.go
+3
-12
@@ -6,11 +6,10 @@ import (
6
"io"
7
"os"
8
9
- cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
9
+ "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10
"github.com/ipfs/go-ipfs/core/coreapi/interface"
11
12
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
13
- "gx/ipfs/QmZMWMvWMVKCbHetJ4RgndbuEF1io2UpUxwQwtNjtYPzSC/go-ipfs-files"
13
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
14
)
15
@@ -124,12 +123,6 @@ var CatCmd = &cmds.Command{
123
},
124
}
125
127
-type catFile interface {
128
- files.SizeFile
129
-
130
- io.Seeker
131
-}
132
-
126
func cat(ctx context.Context, api iface.CoreAPI, paths []string, offset int64, max int64) ([]io.Reader, uint64, error) {
127
readers := make([]io.Reader, 0, len(paths))
128
length := uint64(0)
@@ -142,17 +135,15 @@ func cat(ctx context.Context, api iface.CoreAPI, paths []string, offset int64, m
135
return nil, 0, err
136
}
137
145
- f, err := api.Unixfs().Get(ctx, fpath)
138
+ file, err := api.Unixfs().Get(ctx, fpath)
139
if err != nil {
140
return nil, 0, err
141
}
142
150
- if f.IsDirectory() {
143
+ if file.IsDirectory() {
144
return nil, 0, iface.ErrIsDir
145
}
146
154
- file := f.(catFile)
155
-
147
fsize, err := file.Size()
148
if err != nil {
149
return nil, 0, err
core/coreapi/interface/unixfs.go
+7
-1
@@ -2,6 +2,7 @@ package iface
2
3
import (
4
"context"
5
+ "io"
6
7
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
8
@@ -17,6 +18,11 @@ type AddEvent struct {
18
Size string `json:",omitempty"`
19
}
20
21
+type UnixfsFile interface {
22
+ files.SizeFile
23
+ io.Seeker
24
+}
25
+
26
// UnixfsAPI is the basic interface to immutable files in IPFS
27
// NOTE: This API is heavily WIP, things are guaranteed to break frequently
28
type UnixfsAPI interface {
@@ -29,7 +35,7 @@ type UnixfsAPI interface {
35
//
36
// Note that some implementations of this API may apply the specified context
37
// to operations performed on the returned file
32
- Get(context.Context, Path) (files.File, error)
38
+ Get(context.Context, Path) (UnixfsFile, error)
39
40
// Ls returns the list of links in a directory
41
Ls(context.Context, Path) ([]*ipld.Link, error)
core/coreapi/unixfile.go
+12
-2
@@ -8,6 +8,8 @@ import (
8
gopath "path"
9
"time"
10
11
+ "github.com/ipfs/go-ipfs/core/coreapi/interface"
12
+
13
dag "gx/ipfs/QmVvNkTCx8V9Zei8xuTYTBdUXmbnDRS4iNuw1SztYyhQwQ/go-merkledag"
14
ft "gx/ipfs/QmWE6Ftsk98cG2MTVgH4wJT8VP2nL9TuBkYTrz9GSqcsh5/go-unixfs"
15
uio "gx/ipfs/QmWE6Ftsk98cG2MTVgH4wJT8VP2nL9TuBkYTrz9GSqcsh5/go-unixfs/io"
@@ -95,6 +97,14 @@ func (d *ufsDirectory) NextFile() (files.File, error) {
97
return newUnixfsFile(d.ctx, d.dserv, nd, l.Name, d)
98
}
99
100
+func (d *ufsDirectory) Size() (int64, error) {
101
+ return 0, files.ErrNotReader
102
+}
103
+
104
+func (d *ufsDirectory) Seek(offset int64, whence int) (int64, error) {
105
+ return 0, files.ErrNotReader
106
+}
107
+
108
type ufsFile struct {
109
uio.DagReader
110
@@ -122,7 +132,7 @@ func (f *ufsFile) Size() (int64, error) {
132
return int64(f.DagReader.Size()), nil
133
}
134
125
-func newUnixfsDir(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, name string, path string) (files.File, error) {
135
+func newUnixfsDir(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, name string, path string) (iface.UnixfsFile, error) {
136
dir, err := uio.NewDirectoryFromNode(dserv, nd)
137
if err != nil {
138
return nil, err
@@ -153,7 +163,7 @@ func newUnixfsDir(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, name
163
}, nil
164
}
165
156
-func newUnixfsFile(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, name string, parent files.File) (files.File, error) {
166
+func newUnixfsFile(ctx context.Context, dserv ipld.DAGService, nd ipld.Node, name string, parent files.File) (iface.UnixfsFile, error) {
167
path := name
168
if parent != nil {
169
path = gopath.Join(parent.FullPath(), name)
core/coreapi/unixfs.go
+1
-1
@@ -133,7 +133,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.File, opts ...options
133
return coreiface.IpfsPath(nd.Cid()), nil
134
}
135
136
-func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (files.File, error) {
136
+func (api *UnixfsAPI) Get(ctx context.Context, p coreiface.Path) (coreiface.UnixfsFile, error) {
137
nd, err := api.core().ResolveNode(ctx, p)
138
if err != nil {
139
return nil, err
core/corehttp/gateway_handler.go
+2
-2
@@ -270,7 +270,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
270
} else {
271
name = getFilename(urlPath)
272
}
273
- i.serveFile(w, r, name, modtime, dr.(io.ReadSeeker))
273
+ i.serveFile(w, r, name, modtime, dr)
274
return
275
}
276
@@ -305,7 +305,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
305
defer dr.Close()
306
307
// write to request
308
- http.ServeContent(w, r, "index.html", modtime, dr.(io.ReadSeeker))
308
+ http.ServeContent(w, r, "index.html", modtime, dr)
309
return
310
default:
311
internalWebError(w, err)