storage.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. type Response struct {
  18. MimeType string
  19. Status int
  20. Size int64
  21. // Content []byte
  22. Content string
  23. }
  24. // implements a service
  25. type Storage struct {
  26. api *Api
  27. }
  28. func NewStorage(api *Api) *Storage {
  29. return &Storage{api}
  30. }
  31. // Put uploads the content to the swarm with a simple manifest speficying
  32. // its content type
  33. func (self *Storage) Put(content, contentType string) (string, error) {
  34. return self.api.Put(content, contentType)
  35. }
  36. // Get retrieves the content from bzzpath and reads the response in full
  37. // It returns the Response object, which serialises containing the
  38. // response body as the value of the Content field
  39. // NOTE: if error is non-nil, sResponse may still have partial content
  40. // the actual size of which is given in len(resp.Content), while the expected
  41. // size is resp.Size
  42. func (self *Storage) Get(bzzpath string) (*Response, error) {
  43. reader, mimeType, status, err := self.api.Get(bzzpath, true)
  44. if err != nil {
  45. return nil, err
  46. }
  47. quitC := make(chan bool)
  48. expsize, err := reader.Size(quitC)
  49. if err != nil {
  50. return nil, err
  51. }
  52. body := make([]byte, expsize)
  53. size, err := reader.Read(body)
  54. if int64(size) == expsize {
  55. err = nil
  56. }
  57. return &Response{mimeType, status, expsize, string(body[:size])}, err
  58. }
  59. // Modify(rootHash, path, contentHash, contentType) takes th e manifest trie rooted in rootHash,
  60. // and merge on to it. creating an entry w conentType (mime)
  61. func (self *Storage) Modify(rootHash, path, contentHash, contentType string) (newRootHash string, err error) {
  62. return self.api.Modify(rootHash+"/"+path, contentHash, contentType, true)
  63. }