apifile: Implement Seek
This commit was moved from ipfs/go-ipfs-http-client@69cc3e8106552605ecfdfa5c6f352996dc6f9497
Łukasz Magiera committed
Feb 4, 2019 at 19:43 UTC
75cf2be1026a7f206e61d164342dae4807d36bf1
1 file changed
+25
-6
client/httpapi/apifile.go
+25
-6
@@ -6,13 +6,15 @@ import (
6
"fmt"
7
"github.com/ipfs/go-cid"
8
"io"
9
+ "io/ioutil"
10
11
"github.com/ipfs/go-ipfs/core/coreapi/interface"
12
13
"github.com/ipfs/go-ipfs-files"
13
- unixfspb "github.com/ipfs/go-unixfs/pb"
14
)
15
16
+const forwardSeekLimit = 1 << 14 //16k
17
+
18
func (api *UnixfsAPI) Get(ctx context.Context, p iface.Path) (files.Node, error) {
19
if p.Mutable() { // use resolved path in case we are dealing with IPNS / MFS
20
var err error
@@ -80,7 +82,24 @@ func (f *apiFile) Read(p []byte) (int, error) {
82
}
83
84
func (f *apiFile) Seek(offset int64, whence int) (int64, error) {
83
- panic("implement me") //TODO
85
+ switch whence {
86
+ case io.SeekEnd:
87
+ offset = f.size + offset
88
+ case io.SeekCurrent:
89
+ offset = f.at + offset
90
+ }
91
+ if f.at == offset { //noop
92
+ return offset, nil
93
+ }
94
+
95
+ if f.at < offset && offset - f.at < forwardSeekLimit { //forward skip
96
+ r, err := io.CopyN(ioutil.Discard, f.r, offset - f.at)
97
+
98
+ f.at += r
99
+ return f.at, err
100
+ }
101
+ f.at = offset
102
+ return f.at, f.reset()
103
}
104
105
func (f *apiFile) Close() error {
@@ -156,17 +175,17 @@ func (it *apiIter) Next() bool {
175
}
176
177
switch it.cur.Type {
159
- case unixfspb.Data_HAMTShard:
178
+ case iface.THAMTShard:
179
fallthrough
161
- case unixfspb.Data_Metadata:
180
+ case iface.TMetadata:
181
fallthrough
163
- case unixfspb.Data_Directory:
182
+ case iface.TDirectory:
183
it.curFile, err = it.core.getDir(it.ctx, iface.IpfsPath(c), int64(it.cur.Size))
184
if err != nil {
185
it.err = err
186
return false
187
}
169
- case unixfspb.Data_File:
188
+ case iface.TFile:
189
it.curFile, err = it.core.getFile(it.ctx, iface.IpfsPath(c), int64(it.cur.Size))
190
if err != nil {
191
it.err = err