storage.go 3.1 KB

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