44 "bytes"
55 "io"
66 "io/ioutil"
7- "strconv "
7+ "net/http "
88
99 "github.com/juruen/rmapi/config"
1010 "github.com/juruen/rmapi/log"
@@ -16,26 +16,40 @@ type BlobStorage struct {
1616 http * transport.HttpClientCtx
1717}
1818
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 ) {
2037 log .Trace .Println ("fetching PUT blob url for: " + hash )
2138 var req model.BlobStorageRequest
2239 var res model.BlobStorageResponse
23- req .Method = "PUT"
40+ req .Method = http . MethodPut
2441 req .RelativePath = hash
25- if gen > 0 {
26- req .Generation = strconv .FormatInt (gen , 10 )
27- }
2842 if err := b .http .Post (transport .UserBearer , config .UploadBlob , req , & res ); err != nil {
29- return "" , err
43+ return "" , 0 , err
3044 }
31- return res .Url , nil
45+ return res .Url , res . MaxUploadSizeBytes , nil
3246}
3347
3448func (b * BlobStorage ) GetUrl (hash string ) (string , error ) {
3549 log .Trace .Println ("fetching GET blob url for: " + hash )
3650 var req model.BlobStorageRequest
3751 var res model.BlobStorageResponse
38- req .Method = "GET"
52+ req .Method = http . MethodGet
3953 req .RelativePath = hash
4054 if err := b .http .Post (transport .UserBearer , config .DownloadBlob , req , & res ); err != nil {
4155 return "" , err
@@ -55,37 +69,36 @@ func (b *BlobStorage) GetReader(hash string) (io.ReadCloser, error) {
5569}
5670
5771func (b * BlobStorage ) UploadBlob (hash string , reader io.Reader ) error {
58- url , err := b .PutUrl (hash , - 1 )
72+ url , size , err := b .PutUrl (hash )
5973 if err != nil {
6074 return err
6175 }
6276 log .Trace .Println ("put url: " + url )
6377
64- _ , err = b .http .PutBlobStream (url , - 1 , reader )
65- return err
78+ return b .http .PutBlobStream (url , reader , size )
6679}
6780
6881// 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 )
7187}
7288
7389func (b * BlobStorage ) WriteRootIndex (roothash string , gen int64 ) (int64 , error ) {
7490 log .Info .Println ("writing root with gen: " , gen )
75- url , err := b .PutUrl ( "root" , gen )
91+ url , maxRequestSize , err := b .PutRootUrl ( roothash , gen )
7692 if err != nil {
7793 return 0 , err
7894 }
7995 log .Trace .Println ("got root url:" , url )
8096 reader := bytes .NewBufferString (roothash )
8197
82- gen , err = b .http .PutBlobStream (url , gen , reader )
83- return gen , err
84-
98+ return b .http .PutRootBlobStream (url , gen , maxRequestSize , reader )
8599}
86100func (b * BlobStorage ) GetRootIndex () (string , int64 , error ) {
87-
88- url , err := b .GetUrl ("root" )
101+ url , err := b .GetUrl (ROOT_NAME )
89102 if err != nil {
90103 return "" , 0 , err
91104 }
0 commit comments