github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/swarm/api/storage.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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 go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 type Response struct { 20 MimeType string 21 Status int 22 Size int64 23 // Content []byte 24 Content string 25 } 26 27 // implements a service 28 type Storage struct { 29 api *Api 30 } 31 32 func NewStorage(api *Api) *Storage { 33 return &Storage{api} 34 } 35 36 // Put uploads the content to the swarm with a simple manifest speficying 37 // its content type 38 func (self *Storage) Put(content, contentType string) (string, error) { 39 return self.api.Put(content, contentType) 40 } 41 42 // Get retrieves the content from bzzpath and reads the response in full 43 // It returns the Response object, which serialises containing the 44 // response body as the value of the Content field 45 // NOTE: if error is non-nil, sResponse may still have partial content 46 // the actual size of which is given in len(resp.Content), while the expected 47 // size is resp.Size 48 func (self *Storage) Get(bzzpath string) (*Response, error) { 49 reader, mimeType, status, err := self.api.Get(bzzpath, true) 50 if err != nil { 51 return nil, err 52 } 53 quitC := make(chan bool) 54 expsize, err := reader.Size(quitC) 55 if err != nil { 56 return nil, err 57 } 58 body := make([]byte, expsize) 59 size, err := reader.Read(body) 60 if int64(size) == expsize { 61 err = nil 62 } 63 return &Response{mimeType, status, expsize, string(body[:size])}, err 64 } 65 66 // Modify(rootHash, path, contentHash, contentType) takes th e manifest trie rooted in rootHash, 67 // and merge on to it. creating an entry w conentType (mime) 68 func (self *Storage) Modify(rootHash, path, contentHash, contentType string) (newRootHash string, err error) { 69 return self.api.Modify(rootHash+"/"+path, contentHash, contentType, true) 70 }