github.com/susy-go/susy-graviton@v0.0.0-20190614130430-36cddae42305/swarm/api/storage.go (about) 1 // Copyleft 2016 The susy-graviton Authors 2 // This file is part of the susy-graviton library. 3 // 4 // The susy-graviton library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The susy-graviton library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MSRCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the susy-graviton library. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import ( 20 "context" 21 "path" 22 23 "github.com/susy-go/susy-graviton/swarm/storage" 24 ) 25 26 type Response struct { 27 MimeType string 28 Status int 29 Size int64 30 // Content []byte 31 Content string 32 } 33 34 // implements a service 35 // 36 // DEPRECATED: Use the HTTP API instead 37 type Storage struct { 38 api *API 39 } 40 41 func NewStorage(api *API) *Storage { 42 return &Storage{api} 43 } 44 45 // Put uploads the content to the swarm with a simple manifest speficying 46 // its content type 47 // 48 // DEPRECATED: Use the HTTP API instead 49 func (s *Storage) Put(ctx context.Context, content string, contentType string, toEncrypt bool) (storage.Address, func(context.Context) error, error) { 50 return s.api.Put(ctx, content, contentType, toEncrypt) 51 } 52 53 // Get retrieves the content from bzzpath and reads the response in full 54 // It returns the Response object, which serialises containing the 55 // response body as the value of the Content field 56 // NOTE: if error is non-nil, sResponse may still have partial content 57 // the actual size of which is given in len(resp.Content), while the expected 58 // size is resp.Size 59 // 60 // DEPRECATED: Use the HTTP API instead 61 func (s *Storage) Get(ctx context.Context, bzzpath string) (*Response, error) { 62 uri, err := Parse(path.Join("bzz:/", bzzpath)) 63 if err != nil { 64 return nil, err 65 } 66 addr, err := s.api.Resolve(ctx, uri.Addr) 67 if err != nil { 68 return nil, err 69 } 70 reader, mimeType, status, _, err := s.api.Get(ctx, nil, addr, uri.Path) 71 if err != nil { 72 return nil, err 73 } 74 quitC := make(chan bool) 75 expsize, err := reader.Size(ctx, quitC) 76 if err != nil { 77 return nil, err 78 } 79 body := make([]byte, expsize) 80 size, err := reader.Read(body) 81 if int64(size) == expsize { 82 err = nil 83 } 84 return &Response{mimeType, status, expsize, string(body[:size])}, err 85 }