/
client.go
191 lines (160 loc) · 4.06 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package file
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"log"
"os"
"github.com/micro/go-micro/v2/client"
proto "github.com/micro/go-micro/v2/util/file/proto"
)
// Client is the client interface to access files
type File interface {
Open(filename string, truncate bool) (int64, error)
Stat(filename string) (*proto.StatResponse, error)
GetBlock(sessionId, blockId int64) ([]byte, error)
ReadAt(sessionId, offset, size int64) ([]byte, error)
Read(sessionId int64, buf []byte) (int, error)
Write(sessionId, offset int64, data []byte) error
Close(sessionId int64) error
Download(filename, saveFile string) error
Upload(filename, localFile string) error
DownloadAt(filename, saveFile string, blockId int) error
}
// NewClient returns a new Client which uses a micro Client
func New(service string, c client.Client) File {
return &fc{proto.NewFileService(service, c)}
}
const (
blockSize = 512 * 1024
)
type fc struct {
c proto.FileService
}
func (c *fc) Open(filename string, truncate bool) (int64, error) {
rsp, err := c.c.Open(context.TODO(), &proto.OpenRequest{
Filename: filename,
Truncate: truncate,
})
if err != nil {
return 0, err
}
return rsp.Id, nil
}
func (c *fc) Stat(filename string) (*proto.StatResponse, error) {
return c.c.Stat(context.TODO(), &proto.StatRequest{Filename: filename})
}
func (c *fc) GetBlock(sessionId, blockId int64) ([]byte, error) {
return c.ReadAt(sessionId, blockId*blockSize, blockSize)
}
func (c *fc) ReadAt(sessionId, offset, size int64) ([]byte, error) {
rsp, err := c.c.Read(context.TODO(), &proto.ReadRequest{Id: sessionId, Size: size, Offset: offset})
if err != nil {
return nil, err
}
if rsp.Eof {
err = io.EOF
}
if rsp.Data == nil {
rsp.Data = make([]byte, size)
}
if size != rsp.Size {
return rsp.Data[:rsp.Size], err
}
return rsp.Data, nil
}
func (c *fc) Read(sessionId int64, buf []byte) (int, error) {
b, err := c.ReadAt(sessionId, 0, int64(cap(buf)))
if err != nil {
return 0, err
}
copy(buf, b)
return len(b), nil
}
func (c *fc) Write(sessionId, offset int64, data []byte) error {
_, err := c.c.Write(context.TODO(), &proto.WriteRequest{
Id: sessionId,
Offset: offset,
Data: data})
return err
}
func (c *fc) Close(sessionId int64) error {
_, err := c.c.Close(context.TODO(), &proto.CloseRequest{Id: sessionId})
return err
}
func (c *fc) Download(filename, saveFile string) error {
return c.DownloadAt(filename, saveFile, 0)
}
func (c *fc) Upload(filename, localFile string) error {
file, err := os.Open(localFile)
if err != nil {
return err
}
defer file.Close()
offset := 0
sessionId, err := c.Open(filename, true)
defer c.Close(sessionId)
if err != nil {
return err
}
reader := bufio.NewReader(file)
part := make([]byte, blockSize)
for {
count, err := reader.Read(part)
if err != nil {
break
}
err = c.Write(sessionId, int64(offset), part)
if err != nil {
return err
}
offset += count
}
if err != nil && err != io.EOF {
return fmt.Errorf("Error reading %v: %v", localFile, err)
}
return nil
}
func (c *fc) DownloadAt(filename, saveFile string, blockId int) error {
stat, err := c.Stat(filename)
if err != nil {
return err
}
if stat.Type == "Directory" {
return errors.New(fmt.Sprintf("%s is directory.", filename))
}
blocks := int(stat.Size / blockSize)
if stat.Size%blockSize != 0 {
blocks += 1
}
log.Printf("Download %s in %d blocks\n", filename, blocks-blockId)
file, err := os.OpenFile(saveFile, os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return err
}
defer file.Close()
sessionId, err := c.Open(filename, false)
if err != nil {
return err
}
for i := blockId; i < blocks; i++ {
buf, rerr := c.GetBlock(sessionId, int64(i))
if rerr != nil && rerr != io.EOF {
return rerr
}
if _, werr := file.WriteAt(buf, int64(i)*blockSize); werr != nil {
return werr
}
if i%((blocks-blockId)/100+1) == 0 {
log.Printf("Downloading %s [%d/%d] blocks", filename, i-blockId+1, blocks-blockId)
}
if rerr == io.EOF {
break
}
}
log.Printf("Download %s completed", filename)
c.Close(sessionId)
return nil
}