github.com/0chain/gosdk@v1.17.11/zboxcore/sdk/common.go (about) 1 package sdk 2 3 import ( 4 "context" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 "path" 10 "strconv" 11 "sync" 12 "time" 13 14 "github.com/0chain/errors" 15 "github.com/0chain/gosdk/constants" 16 "github.com/0chain/gosdk/zboxcore/blockchain" 17 "github.com/0chain/gosdk/zboxcore/fileref" 18 l "github.com/0chain/gosdk/zboxcore/logger" 19 "github.com/0chain/gosdk/zboxcore/zboxutil" 20 ) 21 22 func getObjectTreeFromBlobber(ctx context.Context, allocationID, allocationTx, sig string, remoteFilePath string, blobber *blockchain.StorageNode) (fileref.RefEntity, error) { 23 httpreq, err := zboxutil.NewObjectTreeRequest(blobber.Baseurl, allocationID, allocationTx, sig, remoteFilePath) 24 if err != nil { 25 l.Logger.Error(blobber.Baseurl, "Error creating object tree request", err) 26 return nil, err 27 } 28 var lR ReferencePathResult 29 ctx, cncl := context.WithTimeout(ctx, (time.Second * 60)) 30 err = zboxutil.HttpDo(ctx, cncl, httpreq, func(resp *http.Response, err error) error { 31 if err != nil { 32 l.Logger.Error("Object tree:", err) 33 return err 34 } 35 defer resp.Body.Close() 36 if resp.StatusCode != http.StatusOK { 37 l.Logger.Error("Object tree response : ", resp.StatusCode) 38 } 39 resp_body, err := ioutil.ReadAll(resp.Body) 40 if err != nil { 41 l.Logger.Error("Object tree: Resp", err) 42 return err 43 } 44 if resp.StatusCode != http.StatusOK { 45 if resp.StatusCode == http.StatusNotFound { 46 return errors.Throw(constants.ErrNotFound, remoteFilePath) 47 } 48 return errors.New(strconv.Itoa(resp.StatusCode), fmt.Sprintf("Object tree error response: Body: %s ", string(resp_body))) 49 } else { 50 err = json.Unmarshal(resp_body, &lR) 51 if err != nil { 52 l.Logger.Error("Object tree json decode error: ", err) 53 return err 54 } 55 } 56 return nil 57 }) 58 if err != nil { 59 return nil, err 60 } 61 62 return lR.GetRefFromObjectTree(allocationID) 63 } 64 65 func getAllocationDataFromBlobber(blobber *blockchain.StorageNode, allocationId string, allocationTx string, respCh chan<- *BlobberAllocationStats, wg *sync.WaitGroup) { 66 defer wg.Done() 67 httpreq, err := zboxutil.NewAllocationRequest(blobber.Baseurl, allocationId, allocationTx) 68 if err != nil { 69 l.Logger.Error(blobber.Baseurl, "Error creating allocation request", err) 70 return 71 } 72 73 var result BlobberAllocationStats 74 ctx, cncl := context.WithTimeout(context.Background(), (time.Second * 30)) 75 err = zboxutil.HttpDo(ctx, cncl, httpreq, func(resp *http.Response, err error) error { 76 if err != nil { 77 l.Logger.Error("Get allocation :", err) 78 return err 79 } 80 defer resp.Body.Close() 81 if resp.StatusCode != http.StatusOK { 82 l.Logger.Error("Get allocation response : ", resp.StatusCode) 83 } 84 resp_body, err := ioutil.ReadAll(resp.Body) 85 if err != nil { 86 l.Logger.Error("Get allocation: Resp", err) 87 return err 88 } 89 90 err = json.Unmarshal(resp_body, &result) 91 if err != nil { 92 l.Logger.Error("Object tree json decode error: ", err) 93 return err 94 } 95 return nil 96 }) 97 if err != nil { 98 return 99 } 100 101 result.BlobberID = blobber.ID 102 result.BlobberURL = blobber.Baseurl 103 respCh <- &result 104 } 105 106 type ProcessResult struct { 107 BlobberIndex int 108 FileRef fileref.RefEntity 109 Succeed bool 110 } 111 112 var ErrFileNameTooLong = errors.New("invalid_parameter", "filename is longer than 150 characters") 113 114 func ValidateRemoteFileName(remotePath string) error { 115 _, fileName := path.Split(remotePath) 116 117 if len(fileName) > 150 { 118 return ErrFileNameTooLong 119 } 120 121 return nil 122 }