github.com/gobitfly/go-ethereum@v1.8.12/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  import (
    20  	"path"
    21  
    22  	"github.com/ethereum/go-ethereum/swarm/storage"
    23  )
    24  
    25  type Response struct {
    26  	MimeType string
    27  	Status   int
    28  	Size     int64
    29  	// Content  []byte
    30  	Content string
    31  }
    32  
    33  // implements a service
    34  //
    35  // DEPRECATED: Use the HTTP API instead
    36  type Storage struct {
    37  	api *API
    38  }
    39  
    40  func NewStorage(api *API) *Storage {
    41  	return &Storage{api}
    42  }
    43  
    44  // Put uploads the content to the swarm with a simple manifest speficying
    45  // its content type
    46  //
    47  // DEPRECATED: Use the HTTP API instead
    48  func (s *Storage) Put(content, contentType string, toEncrypt bool) (storage.Address, func(), error) {
    49  	return s.api.Put(content, contentType, toEncrypt)
    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 (s *Storage) Get(bzzpath string) (*Response, error) {
    61  	uri, err := Parse(path.Join("bzz:/", bzzpath))
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	addr, err := s.api.Resolve(uri)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  	reader, mimeType, status, _, err := s.api.Get(addr, 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 (s *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  	addr, err := s.api.Resolve(uri)
    96  	if err != nil {
    97  		return "", err
    98  	}
    99  	addr, err = s.api.Modify(addr, path, contentHash, contentType)
   100  	if err != nil {
   101  		return "", err
   102  	}
   103  	return addr.Hex(), nil
   104  }