mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-27 13:27:50 +08:00
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
// package importer implements utilities used to create ipfs DAGs from files
|
|
// and readers
|
|
package importer
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/ipfs/go-ipfs/commands/files"
|
|
bal "github.com/ipfs/go-ipfs/importer/balanced"
|
|
"github.com/ipfs/go-ipfs/importer/chunk"
|
|
h "github.com/ipfs/go-ipfs/importer/helpers"
|
|
trickle "github.com/ipfs/go-ipfs/importer/trickle"
|
|
dag "github.com/ipfs/go-ipfs/merkledag"
|
|
logging "gx/ipfs/Qmazh5oNUVsDZTs2g59rq8aYQqwpss8tcUWQzor5sCCEuH/go-log"
|
|
)
|
|
|
|
var log = logging.Logger("importer")
|
|
|
|
// Builds a DAG from the given file, writing created blocks to disk as they are
|
|
// created
|
|
func BuildDagFromFile(fpath string, ds dag.DAGService) (*dag.Node, error) {
|
|
stat, err := os.Lstat(fpath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if stat.IsDir() {
|
|
return nil, fmt.Errorf("`%s` is a directory", fpath)
|
|
}
|
|
|
|
f, err := files.NewSerialFile(fpath, fpath, false, stat)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
|
|
return BuildDagFromReader(ds, chunk.NewSizeSplitter(f, chunk.DefaultBlockSize))
|
|
}
|
|
|
|
func BuildDagFromReader(ds dag.DAGService, spl chunk.Splitter) (*dag.Node, error) {
|
|
dbp := h.DagBuilderParams{
|
|
Dagserv: ds,
|
|
Maxlinks: h.DefaultLinksPerBlock,
|
|
}
|
|
|
|
return bal.BalancedLayout(dbp.New(spl))
|
|
}
|
|
|
|
func BuildTrickleDagFromReader(ds dag.DAGService, spl chunk.Splitter) (*dag.Node, error) {
|
|
dbp := h.DagBuilderParams{
|
|
Dagserv: ds,
|
|
Maxlinks: h.DefaultLinksPerBlock,
|
|
}
|
|
|
|
return trickle.TrickleLayout(dbp.New(spl))
|
|
}
|