storage.go 2.9 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 "path"
  18. type Response struct {
  19. MimeType string
  20. Status int
  21. Size int64
  22. // Content []byte
  23. Content string
  24. }
  25. // implements a service
  26. //
  27. // DEPRECATED: Use the HTTP API instead
  28. type Storage struct {
  29. api *Api
  30. }
  31. func NewStorage(api *Api) *Storage {
  32. return &Storage{api}
  33. }
  34. // Put uploads the content to the swarm with a simple manifest speficying
  35. // its content type
  36. //
  37. // DEPRECATED: Use the HTTP API instead
  38. func (self *Storage) Put(content, contentType string) (string, error) {
  39. key, err := self.api.Put(content, contentType)
  40. if err != nil {
  41. return "", err
  42. }
  43. return key.String(), err
  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 (self *Storage) Get(bzzpath string) (*Response, error) {
  54. uri, err := Parse(path.Join("bzz:/", bzzpath))
  55. if err != nil {
  56. return nil, err
  57. }
  58. key, err := self.api.Resolve(uri)
  59. if err != nil {
  60. return nil, err
  61. }
  62. reader, mimeType, status, err := self.api.Get(key, uri.Path)
  63. if err != nil {
  64. return nil, err
  65. }
  66. quitC := make(chan bool)
  67. expsize, err := reader.Size(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 (self *Storage) Modify(rootHash, path, contentHash, contentType string) (newRootHash string, err error) {
  83. uri, err := Parse("bzz:/" + rootHash)
  84. if err != nil {
  85. return "", err
  86. }
  87. key, err := self.api.Resolve(uri)
  88. if err != nil {
  89. return "", err
  90. }
  91. key, err = self.api.Modify(key, path, contentHash, contentType)
  92. if err != nil {
  93. return "", err
  94. }
  95. return key.String(), nil
  96. }