ipfs

package
v0.0.0-...-42a4098 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2019 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AddResult

type AddResult struct {
	Hash string `json:"Hash"`
	Size uint64 `json:"Size,string"`
}

AddResult contains result of AddResult command

func (*AddResult) String

func (a *AddResult) String() string

String implements fmt.Stringer interface

func (a *AddResult) ToLink(name string) Link

ToLink creates link from AddResult

type Cid

type Cid string

Cid represents a self-describing content adressed

func (Cid) Defined

func (c Cid) Defined() bool

Defined returns true if a Cid is defined Calling any other methods on an undefined Cid will result in undefined behavior.

func (Cid) MarshalJSON

func (c Cid) MarshalJSON() ([]byte, error)

MarshalJSON procudes a JSON representation of a Cid, which looks as follows:

{ "/": "<cid-string>" }

Note that this formatting comes from the IPLD specification (https://github.com/ipld/specs/tree/master/ipld)

func (Cid) String

func (c Cid) String() string

String returns the default string representation of a Cid.

func (c *Cid) ToLink(name string, size uint64) Link

ToLink creates link from Cid

func (*Cid) UnmarshalJSON

func (c *Cid) UnmarshalJSON(b []byte) error

UnmarshalJSON parses the JSON representation of a Cid.

type IPFS

type IPFS struct {
	// contains filtered or unexported fields
}

IPFS provides limited functionality to interact with the IPFS (https://ipfs.io)

Example
package main

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"strings"

	"github.com/dimchansky/ipfs-add/ipfs"
	"github.com/dnaeon/go-vcr/recorder"
)

func main() {
	// start http recorder
	r, err := recorder.New("fixtures/dag-links")
	if err != nil {
		panic(err)
	}
	defer func() { _ = r.Stop() }() // Make sure recorder is stopped once done with it

	c, err := ipfs.NewWithClient("https://ipfs.infura.io:5001", &http.Client{Transport: r})
	if err != nil {
		panic(err)
	}

	ctx := context.Background()
	file1, err := c.Add(ctx, strings.NewReader("file 1 content"))
	if err != nil {
		panic(err)
	}
	fmt.Printf("file1.txt: %v\n", file1)

	file2, err := c.Add(ctx, strings.NewReader("file 2 content"))
	if err != nil {
		panic(err)
	}
	fmt.Printf("file2.txt: %v\n", file2)

	subDir, err := c.DagPutLinks(ctx, []ipfs.Link{
		file1.ToLink("file1.txt"),
		file2.ToLink("file2.txt"),
	})
	if err != nil {
		panic(err)
	}
	fmt.Printf("subdir: Hash: %v\n", subDir.String())

	subDirStat, err := c.ObjectStat(ctx, subDir.String())
	if err != nil {
		panic(err)
	}
	fmt.Printf("subdir: Size: %v\n", subDirStat.CumulativeSize)

	readmeFile, err := c.Add(ctx, strings.NewReader("README blah blah blah..."))
	if err != nil {
		panic(err)
	}
	fmt.Printf("readme.txt: %v\n", readmeFile)

	rootDir, err := c.DagPutLinks(ctx, []ipfs.Link{
		subDir.ToLink("subdir", subDirStat.CumulativeSize),
		readmeFile.ToLink("README.txt"),
	})
	if err != nil {
		panic(err)
	}
	fmt.Printf("rootdir: Hash: %v\n", rootDir.String())

	rc, err := c.Cat(ctx, rootDir.String()+"/subdir/file1.txt")
	if err != nil {
		panic(err)
	}

	file1Content, err := copyToString(rc)
	if err != nil {
		panic(err)
	}
	fmt.Println(file1Content)

}

func copyToString(r io.Reader) (res string, err error) {
	if c, ok := r.(io.Closer); ok {
		defer func() { _ = c.Close() }()
	}

	var sb strings.Builder
	if _, err = io.Copy(&sb, r); err == nil {
		res = sb.String()
	}
	return
}
Output:

file1.txt: Hash: QmSFEbC6Y17cdti7damkjoqESWftkyfSXjdKDQqnf4ECV7 Size: 22
file2.txt: Hash: QmVssUfKob8KkUyUiwzoGqNTKqyaEXfqxeGiUJ7ZGyfPLV Size: 22
subdir: Hash: QmaRt7pb5LE7991M94XzVCcZgUKPLoihD941GyFSuYBQ9Y
subdir: Size: 150
readme.txt: Hash: QmeQVyGZQbArEEzukCAXbNbLBBwiBwD4E1WinEeAVw1dZ8 Size: 32
rootdir: Hash: QmZgrVVH6Dp4MR4FoF5npDTtJzLneH2KvxoPHixAiucVJH
file 1 content

func New

func New(url string) (*IPFS, error)

New creates new instance of IPFS from the provided url

func NewWithClient

func NewWithClient(uri string, c *http.Client) (*IPFS, error)

NewWithClient creates new instance of IPFS from the provided url and http.Client

func (*IPFS) Add

func (f *IPFS) Add(ctx context.Context, r io.Reader) (*AddResult, error)

Add a file to ipfs from the given reader, returns the hash of the added file

func (*IPFS) Cat

func (f *IPFS) Cat(ctx context.Context, path string) (io.ReadCloser, error)

Cat the content at the given path. Callers need to drain and close the returned reader after usage.

func (f *IPFS) DagGetLinks(ctx context.Context, cid Cid) ([]Link, error)

DagGetLinks gets directory links.

func (f *IPFS) DagPutLinks(ctx context.Context, links []Link) (Cid, error)

DagPutLinks puts directory containing links and returns Cid of directory.

func (*IPFS) ObjectStat

func (f *IPFS) ObjectStat(ctx context.Context, path string) (*ObjectStat, error)

ObjectStat returns information about the dag node

type Link struct {
	// multihash of the target object
	Cid Cid `json:"Cid"`
	// utf string name. should be unique per object
	Name string `json:"Name"`
	// cumulative size of target object
	Size uint64 `json:"Size"`
}

Link represents an IPFS Merkle DAG Link between Nodes.

type ObjectStat

type ObjectStat struct {
	Hash           string `json:"Hash"`
	NumLinks       uint64 `json:"NumLinks"`
	BlockSize      uint64 `json:"BlockSize"`
	LinksSize      uint64 `json:"LinksSize"`
	DataSize       uint64 `json:"DataSize"`
	CumulativeSize uint64 `json:"CumulativeSize"`
}

ObjectStat provides information about dag nodes

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL