4
4
"bytes"
5
5
"io"
6
6
"io/ioutil"
7
- "strconv "
7
+ "net/http "
8
8
9
9
"github.com/juruen/rmapi/config"
10
10
"github.com/juruen/rmapi/log"
@@ -16,26 +16,40 @@ type BlobStorage struct {
16
16
http * transport.HttpClientCtx
17
17
}
18
18
19
- func (b * BlobStorage ) PutUrl (hash string , gen int64 ) (string , error ) {
19
+ const ROOT_NAME = "root"
20
+
21
+ func (b * BlobStorage ) PutRootUrl (hash string , gen int64 ) (string , int64 , error ) {
22
+ log .Trace .Println ("fetching ROOT url for: " + hash )
23
+ req := model.BlobRootStorageRequest {
24
+ Method : http .MethodPut ,
25
+ RelativePath : ROOT_NAME ,
26
+ RootSchema : hash ,
27
+ Generation : gen ,
28
+ }
29
+ var res model.BlobStorageResponse
30
+
31
+ if err := b .http .Post (transport .UserBearer , config .UploadBlob , req , & res ); err != nil {
32
+ return "" , 0 , err
33
+ }
34
+ return res .Url , res .MaxUploadSizeBytes , nil
35
+ }
36
+ func (b * BlobStorage ) PutUrl (hash string ) (string , int64 , error ) {
20
37
log .Trace .Println ("fetching PUT blob url for: " + hash )
21
38
var req model.BlobStorageRequest
22
39
var res model.BlobStorageResponse
23
- req .Method = "PUT"
40
+ req .Method = http . MethodPut
24
41
req .RelativePath = hash
25
- if gen > 0 {
26
- req .Generation = strconv .FormatInt (gen , 10 )
27
- }
28
42
if err := b .http .Post (transport .UserBearer , config .UploadBlob , req , & res ); err != nil {
29
- return "" , err
43
+ return "" , 0 , err
30
44
}
31
- return res .Url , nil
45
+ return res .Url , res . MaxUploadSizeBytes , nil
32
46
}
33
47
34
48
func (b * BlobStorage ) GetUrl (hash string ) (string , error ) {
35
49
log .Trace .Println ("fetching GET blob url for: " + hash )
36
50
var req model.BlobStorageRequest
37
51
var res model.BlobStorageResponse
38
- req .Method = "GET"
52
+ req .Method = http . MethodGet
39
53
req .RelativePath = hash
40
54
if err := b .http .Post (transport .UserBearer , config .DownloadBlob , req , & res ); err != nil {
41
55
return "" , err
@@ -55,37 +69,36 @@ func (b *BlobStorage) GetReader(hash string) (io.ReadCloser, error) {
55
69
}
56
70
57
71
func (b * BlobStorage ) UploadBlob (hash string , reader io.Reader ) error {
58
- url , err := b .PutUrl (hash , - 1 )
72
+ url , size , err := b .PutUrl (hash )
59
73
if err != nil {
60
74
return err
61
75
}
62
76
log .Trace .Println ("put url: " + url )
63
77
64
- _ , err = b .http .PutBlobStream (url , - 1 , reader )
65
- return err
78
+ return b .http .PutBlobStream (url , reader , size )
66
79
}
67
80
68
81
// SyncComplete notifies that the sync is done
69
- func (b * BlobStorage ) SyncComplete () error {
70
- return b .http .Post (transport .UserBearer , config .SyncComplete , nil , nil )
82
+ func (b * BlobStorage ) SyncComplete (gen int64 ) error {
83
+ req := model.SyncCompletedRequest {
84
+ Generation : gen ,
85
+ }
86
+ return b .http .Post (transport .UserBearer , config .SyncComplete , req , nil )
71
87
}
72
88
73
89
func (b * BlobStorage ) WriteRootIndex (roothash string , gen int64 ) (int64 , error ) {
74
90
log .Info .Println ("writing root with gen: " , gen )
75
- url , err := b .PutUrl ( "root" , gen )
91
+ url , maxRequestSize , err := b .PutRootUrl ( roothash , gen )
76
92
if err != nil {
77
93
return 0 , err
78
94
}
79
95
log .Trace .Println ("got root url:" , url )
80
96
reader := bytes .NewBufferString (roothash )
81
97
82
- gen , err = b .http .PutBlobStream (url , gen , reader )
83
- return gen , err
84
-
98
+ return b .http .PutRootBlobStream (url , gen , maxRequestSize , reader )
85
99
}
86
100
func (b * BlobStorage ) GetRootIndex () (string , int64 , error ) {
87
-
88
- url , err := b .GetUrl ("root" )
101
+ url , err := b .GetUrl (ROOT_NAME )
89
102
if err != nil {
90
103
return "" , 0 , err
91
104
}
0 commit comments