@@ -46,14 +46,16 @@ const (
4646 healthUrl = "/health"
4747 chunkUploadDownloadUrl = "/chunks"
4848 bytesUploadDownloadUrl = "/bytes"
49+ bzzUrl = "/bzz"
4950 tagsUrl = "/tags"
5051 pinsUrl = "/pins/"
5152 _ = pinsUrl
5253 swarmPinHeader = "Swarm-Pin"
5354 swarmEncryptHeader = "Swarm-Encrypt"
5455 swarmPostageBatchId = "Swarm-Postage-Batch-Id"
5556 //swarmDeferredUploadHeader = "Swarm-Deferred-Upload"
56- swarmTagHeader = "Swarm-Tag"
57+ swarmTagHeader = "Swarm-Tag"
58+ contentTypeHeader = "Content-Type"
5759)
5860
5961// Client is a bee http client that satisfies blockstore.Client
@@ -462,6 +464,110 @@ func (s *Client) DownloadBlob(address []byte) ([]byte, int, error) {
462464 return respData , response .StatusCode , nil
463465}
464466
467+ // UploadBzz uploads a file through bzz api
468+ func (s * Client ) UploadBzz (data []byte , fileName string ) (address []byte , err error ) {
469+ to := time .Now ()
470+
471+ fullUrl := s .url + bzzUrl + "?name=" + fileName
472+ req , err := http .NewRequest (http .MethodPost , fullUrl , bytes .NewBuffer (data ))
473+ if err != nil {
474+ return nil , err
475+ }
476+
477+ req .Header .Set (swarmPostageBatchId , s .postageBlockId )
478+ req .Header .Set (contentTypeHeader , "application/json" )
479+
480+ response , err := s .client .Do (req )
481+ if err != nil {
482+ return nil , err
483+ }
484+ defer response .Body .Close ()
485+
486+ req .Close = true
487+
488+ respData , err := io .ReadAll (response .Body )
489+ if err != nil {
490+ return nil , errors .New ("error downloading bzz" )
491+ }
492+
493+ if response .StatusCode != http .StatusOK && response .StatusCode != http .StatusCreated {
494+ var beeErr * beeError
495+ err = json .Unmarshal (respData , & beeErr )
496+ if err != nil {
497+ return nil , errors .New (string (respData ))
498+ }
499+ return nil , errors .New (beeErr .Message )
500+ }
501+
502+ var resp bytesPostResponse
503+ err = json .Unmarshal (respData , & resp )
504+
505+ fields := logrus.Fields {
506+ "reference" : resp .Reference .String (),
507+ "size" : len (respData ),
508+ "duration" : time .Since (to ).String (),
509+ }
510+ s .logger .WithFields (fields ).Log (logrus .DebugLevel , "upload bzz: " )
511+
512+ // add the data and ref if it is not in cache
513+ if ! s .inBlockCache (s .downloadBlockCache , resp .Reference .String ()) {
514+ s .addToBlockCache (s .downloadBlockCache , resp .Reference .String (), data )
515+ }
516+ return resp .Reference .Bytes (), nil
517+ }
518+
519+ // DownloadBzz downloads bzz data from the Swarm network.
520+ func (s * Client ) DownloadBzz (address []byte ) ([]byte , int , error ) {
521+ to := time .Now ()
522+
523+ // return the data if this address is already in cache
524+ addrString := swarm .NewAddress (address ).String ()
525+ if s .inBlockCache (s .downloadBlockCache , addrString ) {
526+ return s .getFromBlockCache (s .downloadBlockCache , addrString ), 200 , nil
527+ }
528+
529+ fullUrl := s .url + bzzUrl + "/" + addrString
530+ req , err := http .NewRequest (http .MethodGet , fullUrl , http .NoBody )
531+ if err != nil {
532+ return nil , http .StatusNotFound , err
533+ }
534+
535+ response , err := s .client .Do (req )
536+ if err != nil {
537+ return nil , http .StatusNotFound , err
538+ }
539+ defer response .Body .Close ()
540+
541+ req .Close = true
542+
543+ respData , err := io .ReadAll (response .Body )
544+ if err != nil {
545+ return nil , response .StatusCode , errors .New ("error downloading bzz" )
546+ }
547+
548+ if response .StatusCode != http .StatusOK {
549+ var beeErr * beeError
550+ err = json .Unmarshal (respData , & beeErr )
551+ if err != nil {
552+ return nil , response .StatusCode , errors .New (string (respData ))
553+ }
554+ return nil , response .StatusCode , errors .New (beeErr .Message )
555+ }
556+
557+ fields := logrus.Fields {
558+ "reference" : addrString ,
559+ "size" : len (respData ),
560+ "duration" : time .Since (to ).String (),
561+ }
562+ s .logger .WithFields (fields ).Log (logrus .DebugLevel , "download bzz: " )
563+
564+ // add the data and ref if it is not in cache
565+ if ! s .inBlockCache (s .downloadBlockCache , addrString ) {
566+ s .addToBlockCache (s .downloadBlockCache , addrString , respData )
567+ }
568+ return respData , response .StatusCode , nil
569+ }
570+
465571// DeleteReference unpins a reference so that it will be garbage collected by the Swarm network.
466572func (s * Client ) DeleteReference (address []byte ) error {
467573 // TODO uncomment after unpinning is fixed
0 commit comments