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