Skip to content

Commit

Permalink
Export unixfs chunk function.
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Jan 29, 2021
1 parent 9148e8a commit b176dd8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
47 changes: 11 additions & 36 deletions unixfs/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@ package unixfs
import (
"context"
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/ipfs/go-ipfs-chunker"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
ufs "github.com/ipfs/go-unixfs"
"github.com/ipfs/go-unixfs/importer/balanced"
"github.com/ipfs/go-unixfs/importer/helpers"
ufsio "github.com/ipfs/go-unixfs/io"
)

// DefaultChunker is the name of the default chunker algorithm.
const DefaultChunker = "buzhash"

// Ignore is used to filter files.
type Ignore []string

Expand Down Expand Up @@ -58,32 +51,6 @@ func Add(ctx context.Context, dag ipld.DAGService, path string, ignore Ignore) (
}
}

// addReader splits the given reader into chunks and arranges them into a dag node.
func addReader(ctx context.Context, dag ipld.DAGService, reader io.Reader) (ipld.Node, error) {
chunker, err := chunk.FromString(reader, DefaultChunker)
if err != nil {
return nil, err
}

params := helpers.DagBuilderParams{
Dagserv: dag,
CidBuilder: merkledag.V1CidPrefix(),
Maxlinks: helpers.DefaultLinksPerBlock,
}

helper, err := params.New(chunker)
if err != nil {
return nil, err
}

node, err := balanced.Layout(helper)
if err != nil {
return nil, err
}

return node, dag.Add(ctx, node)
}

// addFile creates a dag node from the file at the given path.
func addFile(ctx context.Context, dag ipld.DAGService, path string) (ipld.Node, error) {
file, err := os.Open(path)
Expand All @@ -92,7 +59,7 @@ func addFile(ctx context.Context, dag ipld.DAGService, path string) (ipld.Node,
}
defer file.Close()

return addReader(ctx, dag, file)
return Chunk(ctx, dag, file)
}

// addSymlink creates a dag node from the symlink at the given path.
Expand All @@ -108,7 +75,11 @@ func addSymlink(ctx context.Context, dag ipld.DAGService, path string) (ipld.Nod
}

node := merkledag.NodeWithData(data)
return node, dag.Add(ctx, node)
if err := dag.Add(ctx, node); err != nil {
return nil, err
}

return node, nil
}

// addDir creates a dag node from the directory entries at the given path.
Expand Down Expand Up @@ -140,5 +111,9 @@ func addDir(ctx context.Context, dag ipld.DAGService, path string, ignore Ignore
return nil, err
}

return node, dag.Add(ctx, node)
if err := dag.Add(ctx, node); err != nil {
return nil, err
}

return node, nil
}
45 changes: 45 additions & 0 deletions unixfs/chunk.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package unixfs

import (
"context"
"io"

chunk "github.com/ipfs/go-ipfs-chunker"
ipld "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-unixfs/importer/balanced"
"github.com/ipfs/go-unixfs/importer/helpers"
)

// DefaultChunker is the name of the default chunker algorithm.
const DefaultChunker = "buzhash"

// Chunk splits the given reader into chunks and arranges them into a dag node.
func Chunk(ctx context.Context, dag ipld.DAGService, reader io.Reader) (ipld.Node, error) {
chunker, err := chunk.FromString(reader, DefaultChunker)
if err != nil {
return nil, err
}

params := helpers.DagBuilderParams{
Dagserv: dag,
CidBuilder: merkledag.V1CidPrefix(),
Maxlinks: helpers.DefaultLinksPerBlock,
}

helper, err := params.New(chunker)
if err != nil {
return nil, err
}

node, err := balanced.Layout(helper)
if err != nil {
return nil, err
}

if err := dag.Add(ctx, node); err != nil {
return nil, err
}

return node, nil
}
2 changes: 1 addition & 1 deletion unixfs/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func Merge(ctx context.Context, dag ipld.DAGService, o, a, b cid.Cid) (ipld.Node
merged := diff3.Merge(textO, textA, textB)
reader := strings.NewReader(merged)

return addReader(ctx, dag, reader)
return Chunk(ctx, dag, reader)
}

0 comments on commit b176dd8

Please sign in to comment.