Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve rewind errors in ipfs #103

Merged
merged 14 commits into from
Mar 10, 2023
20 changes: 10 additions & 10 deletions ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ import (
"fmt"

shell "github.com/ipfs/go-ipfs-api"
log "github.com/sirupsen/logrus"
)

type IPFS struct {
sh *shell.Shell
url string
}

// NewIPFS generates an IPFS node with IPFS url.
func NewIPFS(url string) (*IPFS, error) {
newShell := shell.NewShell(url)

if !newShell.IsUp() {
// As with the previous specification, when an IPFS object is initially created, it checks to see if IPFS can connect well.
if !shell.NewShell(url).IsUp() {
return nil, fmt.Errorf("IPFS is not connected")
}

log.Info("successfully connect to IPFS node")

return &IPFS{
sh: newShell,
url: url,
}, nil
}

func (i *IPFS) createShell() *shell.Shell {
return shell.NewShell(i.url)
}

// Add method adds a data and returns a CID.
func (i *IPFS) Add(data []byte) (string, error) {
reader := bytes.NewReader(data)

cid, err := i.sh.Add(reader)
cid, err := i.createShell().Add(reader)
if err != nil {
return "", err
}
Expand All @@ -41,7 +41,7 @@ func (i *IPFS) Add(data []byte) (string, error) {

// Get method gets a data and returns a bytes of Deal.
func (i *IPFS) Get(cid string) ([]byte, error) {
data, err := i.sh.Cat(cid)
data, err := i.createShell().Cat(cid)
if err != nil {
return nil, err
}
Expand Down