You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
873 B

  1. package data
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. shell "github.com/ipfs/go-ipfs-api"
  8. )
  9. type Storage interface {
  10. Publish(o []byte) string
  11. Retrieve(id string) []byte
  12. }
  13. func Publish(object []byte) string {
  14. sh := shell.NewShell("localhost:5001")
  15. cid, err := sh.Add(bytes.NewBuffer(object))
  16. if err != nil {
  17. fmt.Fprintf(os.Stderr, "error: %s", err)
  18. os.Exit(1)
  19. }
  20. return cid
  21. }
  22. func Pin(path string) {
  23. sh := shell.NewShell("localhost:5001")
  24. err := sh.Pin(path)
  25. if err != nil {
  26. fmt.Fprintf(os.Stderr, "error: %s", err)
  27. os.Exit(1)
  28. }
  29. }
  30. func Retrieve(hash string) []byte {
  31. sh := shell.NewShell("localhost:5001")
  32. reader, err := sh.Cat(hash)
  33. if err != nil {
  34. fmt.Fprintf(os.Stderr, "error: %s", err)
  35. os.Exit(1)
  36. }
  37. content, err := ioutil.ReadAll(reader)
  38. if err != nil {
  39. fmt.Fprintf(os.Stderr, "error: %s", err)
  40. os.Exit(1)
  41. }
  42. return content
  43. }