2018-02-28 22:49:03 +00:00
|
|
|
package importer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-ipfs-cmdkit/files"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToChannel imports file to ipfs ipld nodes, outputting nodes on the
|
|
|
|
// provided channel
|
2018-03-01 00:19:51 +00:00
|
|
|
func ToChannel(ctx context.Context, f files.File, progress bool, hidden bool,
|
2018-02-28 22:49:03 +00:00
|
|
|
trickle bool, raw bool, silent bool, wrap bool,
|
2018-03-05 20:20:33 +00:00
|
|
|
chunker string) (<-chan *api.AddedOutput, <-chan *api.NodeWithMeta, <-chan error) {
|
2018-02-28 22:49:03 +00:00
|
|
|
|
|
|
|
printChan := make(chan *api.AddedOutput)
|
|
|
|
errChan := make(chan error)
|
2018-03-05 20:20:33 +00:00
|
|
|
outChan := make(chan *api.NodeWithMeta)
|
2018-02-28 22:49:03 +00:00
|
|
|
|
|
|
|
dserv := &outDAGService{
|
|
|
|
membership: make(map[string]struct{}),
|
|
|
|
outChan: outChan,
|
|
|
|
}
|
|
|
|
|
|
|
|
fileAdder, err := NewAdder(ctx, nil, nil, dserv)
|
|
|
|
if err != nil {
|
|
|
|
go func() {
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
return printChan, outChan, errChan
|
|
|
|
}
|
|
|
|
// fileAdder.Progress = progress //TODO get progress working eventually. dont need complexity right now
|
|
|
|
fileAdder.Hidden = hidden
|
|
|
|
fileAdder.Trickle = trickle
|
|
|
|
fileAdder.RawLeaves = raw
|
|
|
|
fileAdder.Silent = silent
|
|
|
|
fileAdder.Wrap = wrap
|
|
|
|
fileAdder.Chunker = chunker
|
|
|
|
fileAdder.Out = printChan
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(printChan)
|
|
|
|
defer close(outChan)
|
|
|
|
defer close(errChan)
|
|
|
|
// add all files under the root, as in ipfs
|
|
|
|
for {
|
|
|
|
file, err := f.NextFile()
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if err := fileAdder.AddFile(file); err != nil {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := fileAdder.Finalize()
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "dagservice: block not found") {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return printChan, outChan, errChan
|
|
|
|
}
|