Skip to content
This repository was archived by the owner on Jul 11, 2019. It is now read-only.

Commit b533eb1

Browse files
committed
add skipreader , fix GetFile can use offset
1 parent 6c68eb3 commit b533eb1

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

driver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (driver *QiniuDriver) MakeDir(path string) error {
175175
return err
176176
}
177177

178-
func (driver *QiniuDriver) GetFile(key string, start int64) (int64, io.ReadCloser, error) {
178+
func (driver *QiniuDriver) GetFile(key string, offset int64) (int64, io.ReadCloser, error) {
179179
stat, err := driver.Stat(key)
180180
if err != nil {
181181
return 0, nil, err
@@ -193,7 +193,7 @@ func (driver *QiniuDriver) GetFile(key string, start int64) (int64, io.ReadClose
193193
return 0, nil, err
194194
}
195195

196-
return stat.Size(), resp.Body, nil
196+
return stat.Size(), NewSkipReadCloser(resp.Body, offset), nil
197197
}
198198

199199
func (driver *QiniuDriver) PutFile(key string, data io.Reader, appendData bool) (int64, error) {

skipreader.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package qiniudriver
2+
3+
import (
4+
"io"
5+
"io/ioutil"
6+
)
7+
8+
func NewSkipReadCloser(rd io.ReadCloser, count int64) io.ReadCloser {
9+
return &SkipReadCloser{
10+
ReadCloser: rd,
11+
count: count,
12+
}
13+
}
14+
15+
type SkipReadCloser struct {
16+
io.ReadCloser
17+
count int64
18+
skipped bool
19+
}
20+
21+
func (s *SkipReadCloser) Read(data []byte) (int, error) {
22+
if !s.skipped {
23+
if s.count > 0 {
24+
_, err := io.CopyN(ioutil.Discard, s.ReadCloser, s.count)
25+
if err != nil {
26+
return 0, err
27+
}
28+
}
29+
s.skipped = true
30+
}
31+
return s.ReadCloser.Read(data)
32+
}

0 commit comments

Comments
 (0)