-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_file.go
51 lines (46 loc) · 1.27 KB
/
client_file.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
package crpc
import (
"errors"
"io"
"path/filepath"
"github.com/ndsky1003/crpc/coder"
"github.com/ndsky1003/crpc/compressor"
"github.com/ndsky1003/crpc/dto"
"github.com/ndsky1003/crpc/header/headertype"
"github.com/ndsky1003/crpc/options"
)
func (this *Client) SendFile(server string, moduleFunc string, filename string, reader io.Reader, opts ...*options.SendOptions) error {
if filename == "" {
return errors.New("filename not empty")
}
if filepath.IsAbs(filename) {
return errors.New("filename must relative path")
}
data := make([]byte, this.chunksSize)
var chunkIndex uint16 = 0
filebody := &dto.FileBody{
Filename: filename,
}
opt := options.Send().Merge(opts...)
opt.SetCoderType(coder.FilePack).SetCompressorType(compressor.Raw)
if opt.Timeout == nil {
opt.SetTimeout(60 * 60 * 2)
}
for {
n, err := reader.Read(data)
if err != nil {
return err
}
filebody.ChunksIndex = chunkIndex
filebody.Data = data[:n]
//if err := this._call(headertype.Chunks, coder.FilePack, compressor.Raw, 60*60*2, server, moduleFunc, filebody, nil); err != nil {
if err := this._call(headertype.Chunks, server, moduleFunc, filebody, nil, opt); err != nil {
return err
}
if n < this.chunksSize {
return nil
}
filebody.Offset += uint64(n)
chunkIndex++
}
}