github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/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  	"context"
    21  	"path"
    22  
    23  	"github.com/PlatONnetwork/PlatON-Go/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  }
    86  
    87  // Modify(rootHash, basePath, contentHash, contentType) takes th e manifest trie rooted in rootHash,
    88  // and merge on  to it. creating an entry w conentType (mime)
    89  //
    90  // DEPRECATED: Use the HTTP API instead
    91  func (s *Storage) Modify(ctx context.Context, rootHash, path, contentHash, contentType string) (newRootHash string, err error) {
    92  	uri, err := Parse("bzz:/" + rootHash)
    93  	if err != nil {
    94  		return "", err
    95  	}
    96  	addr, err := s.api.Resolve(ctx, uri.Addr)
    97  	if err != nil {
    98  		return "", err
    99  	}
   100  	addr, err = s.api.Modify(ctx, addr, path, contentHash, contentType)
   101  	if err != nil {
   102  		return "", err
   103  	}
   104  	return addr.Hex(), nil
   105  }