storage.go 3.0 KB

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