github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/swarm/api/storage.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package api 13 14 import "path" 15 16 type Response struct { 17 MimeType string 18 Status int 19 Size int64 20 // Content []byte 21 Content string 22 } 23 24 // implements a service 25 // 26 // DEPRECATED: Use the HTTP API instead 27 type Storage struct { 28 api *Api 29 } 30 31 func NewStorage(api *Api) *Storage { 32 return &Storage{api} 33 } 34 35 // Put uploads the content to the swarm with a simple manifest speficying 36 // its content type 37 // 38 // DEPRECATED: Use the HTTP API instead 39 func (self *Storage) Put(content, contentType string) (string, error) { 40 key, err := self.api.Put(content, contentType) 41 if err != nil { 42 return "", err 43 } 44 return key.String(), err 45 } 46 47 // Get retrieves the content from bzzpath and reads the response in full 48 // It returns the Response object, which serialises containing the 49 // response body as the value of the Content field 50 // NOTE: if error is non-nil, sResponse may still have partial content 51 // the actual size of which is given in len(resp.Content), while the expected 52 // size is resp.Size 53 // 54 // DEPRECATED: Use the HTTP API instead 55 func (self *Storage) Get(bzzpath string) (*Response, error) { 56 uri, err := Parse(path.Join("bzz:/", bzzpath)) 57 if err != nil { 58 return nil, err 59 } 60 key, err := self.api.Resolve(uri) 61 if err != nil { 62 return nil, err 63 } 64 reader, mimeType, status, err := self.api.Get(key, uri.Path) 65 if err != nil { 66 return nil, err 67 } 68 quitC := make(chan bool) 69 expsize, err := reader.Size(quitC) 70 if err != nil { 71 return nil, err 72 } 73 body := make([]byte, expsize) 74 size, err := reader.Read(body) 75 if int64(size) == expsize { 76 err = nil 77 } 78 return &Response{mimeType, status, expsize, string(body[:size])}, err 79 } 80 81 // Modify(rootHash, basePath, contentHash, contentType) takes th e manifest trie rooted in rootHash, 82 // and merge on to it. creating an entry w conentType (mime) 83 // 84 // DEPRECATED: Use the HTTP API instead 85 func (self *Storage) Modify(rootHash, path, contentHash, contentType string) (newRootHash string, err error) { 86 uri, err := Parse("bzz:/" + rootHash) 87 if err != nil { 88 return "", err 89 } 90 key, err := self.api.Resolve(uri) 91 if err != nil { 92 return "", err 93 } 94 key, err = self.api.Modify(key, path, contentHash, contentType) 95 if err != nil { 96 return "", err 97 } 98 return key.String(), nil 99 }