6 "github.com/aliyun/aliyun-oss-go-sdk/oss"
8 "github.com/bytom/vapor/errors"
9 "github.com/bytom/vapor/toolbar/osssync/util"
12 // PutObjByteArr upload Byte Array object
13 func (u *UploadKeeper) PutObjByteArr(objectName string, objectValue []byte) error {
14 objectAcl := oss.ObjectACL(oss.ACLPublicRead)
15 return u.OssBucket.PutObject(objectName, bytes.NewReader(objectValue), objectAcl)
18 // GetInfoJson Download info.json
19 func (u *UploadKeeper) GetInfoJson() (*util.Info, error) {
20 body, err := u.OssBucket.GetObject("info.json")
25 return util.GetInfoJson(body)
29 func (u *UploadKeeper) PutInfoJson(infoData *util.Info) error {
30 jsonData, err := util.Struct2Json(infoData)
36 return u.PutObjByteArr("info.json", jsonData)
39 // SetLatestBlockHeight set new latest blockHeight on OSS
40 func (u *UploadKeeper) SetLatestBlockHeight(newLatestBlockHeight uint64) error {
41 info, err := u.GetInfoJson()
46 info.LatestBlockHeight = newLatestBlockHeight
47 return u.PutInfoJson(info)
50 // AddInterval if "info.json" exists on OSS, add Interval to the end; if not exist, create "info.json" with Interval
51 func (u *UploadKeeper) AddInterval(end, gzSize uint64) error {
52 isJsonExist, err := u.OssBucket.IsObjectExist("info.json")
60 info, err = u.GetInfoJson()
66 prevInvl := info.Interval[len(info.Interval)-1]
67 if prevInvl.EndBlockHeight >= end {
68 return errors.New("New interval is included in previous intervals.")
71 if (end-prevInvl.EndBlockHeight)%gzSize != 0 {
72 return errors.New("New interval is invalid.")
75 newInvl := util.NewInterval(prevInvl.EndBlockHeight+1, end, gzSize)
76 info.Interval = append(info.Interval, newInvl)
78 info = util.NewInfo(end, gzSize)
80 return u.PutInfoJson(info)