-
Notifications
You must be signed in to change notification settings - Fork 0
/
resumable_put.go
80 lines (71 loc) · 1.7 KB
/
resumable_put.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
package rs
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"lib/objectstream"
"lib/utils"
"log"
"net/http"
)
type resumableToken struct {
Name string
Size int64
Hash string
Servers []string
Uuids []string
}
type RSResumablePutStream struct {
*RSPutStream
*resumableToken
}
func NewRSResumablePutStream(dataServers []string, name, hash string, size int64) (*RSResumablePutStream, error) {
putStream, e := NewRSPutStream(dataServers, hash, size)
if e != nil {
return nil, e
}
uuids := make([]string, ALL_SHARDS)
for i := range uuids {
uuids[i] = putStream.writers[i].(*objectstream.TempPutStream).Uuid
}
token := &resumableToken{name, size, hash, dataServers, uuids}
return &RSResumablePutStream{putStream, token}, nil
}
func NewRSResumablePutStreamFromToken(token string) (*RSResumablePutStream, error) {
b, e := base64.StdEncoding.DecodeString(token)
if e != nil {
return nil, e
}
var t resumableToken
e = json.Unmarshal(b, &t)
if e != nil {
return nil, e
}
writers := make([]io.Writer, ALL_SHARDS)
for i := range writers {
writers[i] = &objectstream.TempPutStream{t.Servers[i], t.Uuids[i]}
}
enc := NewEncoder(writers)
return &RSResumablePutStream{&RSPutStream{enc}, &t}, nil
}
func (s *RSResumablePutStream) ToToken() string {
b, _ := json.Marshal(s)
return base64.StdEncoding.EncodeToString(b)
}
func (s *RSResumablePutStream) CurrentSize() int64 {
r, e := http.Head(fmt.Sprintf("http://%s/temp/%s", s.Servers[0], s.Uuids[0]))
if e != nil {
log.Println(e)
return -1
}
if r.StatusCode != http.StatusOK {
log.Println(r.StatusCode)
return -1
}
size := utils.GetSizeFromHeader(r.Header) * DATA_SHARDS
if size > s.Size {
size = s.Size
}
return size
}