github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/swarm/api/storage.go (about) 1 // Copyright 2016 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import "path" 20 21 type Response struct { 22 MimeType string 23 Status int 24 Size int64 25 // Content []byte 26 Content string 27 } 28 29 // implements a service 30 // 31 // DEPRECATED: Use the HTTP API instead 32 type Storage struct { 33 api *Api 34 } 35 36 func NewStorage(api *Api) *Storage { 37 return &Storage{api} 38 } 39 40 // Put uploads the content to the swarm with a simple manifest speficying 41 // its content type 42 // 43 // DEPRECATED: Use the HTTP API instead 44 func (self *Storage) Put(content, contentType string) (string, error) { 45 key, err := self.api.Put(content, contentType) 46 if err != nil { 47 return "", err 48 } 49 return key.String(), err 50 } 51 52 // Get retrieves the content from bzzpath and reads the response in full 53 // It returns the Response object, which serialises containing the 54 // response body as the value of the Content field 55 // NOTE: if error is non-nil, sResponse may still have partial content 56 // the actual size of which is given in len(resp.Content), while the expected 57 // size is resp.Size 58 // 59 // DEPRECATED: Use the HTTP API instead 60 func (self *Storage) Get(bzzpath string) (*Response, error) { 61 uri, err := Parse(path.Join("bzz:/", bzzpath)) 62 if err != nil { 63 return nil, err 64 } 65 key, err := self.api.Resolve(uri) 66 if err != nil { 67 return nil, err 68 } 69 reader, mimeType, status, err := self.api.Get(key, uri.Path) 70 if err != nil { 71 return nil, err 72 } 73 quitC := make(chan bool) 74 expsize, err := reader.Size(quitC) 75 if err != nil { 76 return nil, err 77 } 78 body := make([]byte, expsize) 79 size, err := reader.Read(body) 80 if int64(size) == expsize { 81 err = nil 82 } 83 return &Response{mimeType, status, expsize, string(body[:size])}, err 84 } 85 86 // Modify(rootHash, basePath, contentHash, contentType) takes th e manifest trie rooted in rootHash, 87 // and merge on to it. creating an entry w conentType (mime) 88 // 89 // DEPRECATED: Use the HTTP API instead 90 func (self *Storage) Modify(rootHash, path, contentHash, contentType string) (newRootHash string, err error) { 91 uri, err := Parse("bzz:/" + rootHash) 92 if err != nil { 93 return "", err 94 } 95 key, err := self.api.Resolve(uri) 96 if err != nil { 97 return "", err 98 } 99 key, err = self.api.Modify(key, path, contentHash, contentType) 100 if err != nil { 101 return "", err 102 } 103 return key.String(), nil 104 }