2018-02-28 22:49:03 +00:00
|
|
|
package importer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-ipfs-cmdkit/files"
|
|
|
|
)
|
|
|
|
|
2018-04-20 15:41:15 +00:00
|
|
|
func shouldIgnore(err error) bool {
|
2018-04-26 23:40:32 +00:00
|
|
|
if err == errNotFound {
|
2018-04-20 15:41:15 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-02-28 22:49:03 +00:00
|
|
|
// ToChannel imports file to ipfs ipld nodes, outputting nodes on the
|
|
|
|
// provided channel
|
2018-04-20 22:57:13 +00:00
|
|
|
func ToChannel(ctx context.Context, f files.File, hidden bool,
|
|
|
|
trickle bool, raw bool, silent 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,
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:31:06 +00:00
|
|
|
fileAdder, err := NewAdder(ctx, nil, dserv)
|
2018-02-28 22:49:03 +00:00
|
|
|
if err != nil {
|
|
|
|
go func() {
|
|
|
|
errChan <- err
|
|
|
|
}()
|
|
|
|
return printChan, outChan, errChan
|
|
|
|
}
|
|
|
|
fileAdder.Hidden = hidden
|
|
|
|
fileAdder.Trickle = trickle
|
|
|
|
fileAdder.RawLeaves = raw
|
|
|
|
fileAdder.Silent = silent
|
2018-04-20 22:57:13 +00:00
|
|
|
// Files added in one session are wrapped. This is because if files
|
|
|
|
// are sharded together then the share one logical clusterDAG root hash
|
|
|
|
fileAdder.Wrap = true
|
2018-02-28 22:49:03 +00:00
|
|
|
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()
|
2018-04-20 15:41:15 +00:00
|
|
|
if err != nil && !shouldIgnore(err) {
|
2018-02-28 22:49:03 +00:00
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return printChan, outChan, errChan
|
|
|
|
}
|