提交 aa0704e6 作者: Łukasz Magiera

add: fix wrap with multiple files

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 0b9e89dd
...@@ -181,25 +181,11 @@ You can now check what blocks have been created by: ...@@ -181,25 +181,11 @@ You can now check what blocks have been created by:
return err return err
} }
toadd := []files.Node{req.Files} toadd := req.Files
name := []string{""} if wrap {
if !wrap { toadd = files.NewSliceDirectory([]files.DirEntry{
toadd, name = nil, nil files.FileEntry("", req.Files),
})
// process all entries in case user adds multiple files
it := req.Files.Entries()
for it.Next() {
toadd = append(toadd, it.Node())
name = append(name, it.Name())
}
if err := it.Err(); err != nil {
return err
}
if len(toadd) == 0 {
return fmt.Errorf("expected a file argument")
}
} }
opts := []options.UnixfsAddOption{ opts := []options.UnixfsAddOption{
...@@ -217,8 +203,6 @@ You can now check what blocks have been created by: ...@@ -217,8 +203,6 @@ You can now check what blocks have been created by:
options.Unixfs.Progress(progress), options.Unixfs.Progress(progress),
options.Unixfs.Silent(silent), options.Unixfs.Silent(silent),
nil, // reserved for events, must be last
} }
if cidVerSet { if cidVerSet {
...@@ -233,17 +217,21 @@ You can now check what blocks have been created by: ...@@ -233,17 +217,21 @@ You can now check what blocks have been created by:
opts = append(opts, options.Unixfs.Layout(options.TrickleLayout)) opts = append(opts, options.Unixfs.Layout(options.TrickleLayout))
} }
for i := range toadd { opts = append(opts, nil) // events option placeholder
_, dir := toadd[i].(files.Directory)
var added int
addit := toadd.Entries()
for addit.Next() {
_, dir := addit.Node().(files.Directory)
errCh := make(chan error, 1) errCh := make(chan error, 1)
events := make(chan interface{}, adderOutChanSize) events := make(chan interface{}, adderOutChanSize)
opts[len(opts)-1] = options.Unixfs.Events(events) opts[len(opts)-1] = options.Unixfs.Events(events)
go func() { go func() {
var err error var err error
defer func() { errCh <- err }()
defer close(events) defer close(events)
_, err = api.Unixfs().Add(req.Context, toadd[i], opts...) _, err = api.Unixfs().Add(req.Context, addit.Node(), opts...)
errCh <- err
}() }()
for event := range events { for event := range events {
...@@ -257,10 +245,10 @@ You can now check what blocks have been created by: ...@@ -257,10 +245,10 @@ You can now check what blocks have been created by:
h = enc.Encode(output.Path.Cid()) h = enc.Encode(output.Path.Cid())
} }
if !dir && name[i] != "" { if !dir && addit.Name() != "" {
output.Name = name[i] output.Name = addit.Name()
} else { } else {
output.Name = path.Join(name[i], output.Name) output.Name = path.Join(addit.Name(), output.Name)
} }
if err := res.Emit(&AddEvent{ if err := res.Emit(&AddEvent{
...@@ -274,9 +262,15 @@ You can now check what blocks have been created by: ...@@ -274,9 +262,15 @@ You can now check what blocks have been created by:
} }
if err := <-errCh; err != nil { if err := <-errCh; err != nil {
return nil return err
} }
added++
} }
if added == 0 {
return fmt.Errorf("expected a file argument")
}
return nil return nil
}, },
PostRun: cmds.PostRunMap{ PostRun: cmds.PostRunMap{
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论