mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-28 08:47:42 +08:00
add -w improvement: wrap multiple files in one dir
> ipfs add a b c added Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9 a added QmR9pC5uCF3UExca8RSrCVL8eKv7nHMpATzbEQkAHpXmVM b added QmetGxZTgo8tYAKQH1KLsY13MxqeVHbxYVmvzBzJAKU6Z7 c added QmXg3WHLcjnz4ejeYF6FKVBkb4m1oKjQmF5fEWL9M1uQF3 > ipfs ls QmXg3WHLcjnz4ejeYF6FKVBkb4m1oKjQmF5fEWL9M1uQF3 Qmbvkmk9LFsGneteXk3G7YLqtLVME566ho6ibaQZZVHaC9 10 a QmR9pC5uCF3UExca8RSrCVL8eKv7nHMpATzbEQkAHpXmVM 10 b QmetGxZTgo8tYAKQH1KLsY13MxqeVHbxYVmvzBzJAKU6Z7 10 c License: MIT Signed-off-by: Juan Batiz-Benet <juan@benet.ai>
This commit is contained in:
@ -110,52 +110,65 @@ remains to be implemented.
|
||||
outChan := make(chan interface{}, 8)
|
||||
res.SetOutput((<-chan interface{})(outChan))
|
||||
|
||||
// addSingleFile is a function that adds a file given as a param.
|
||||
addSingleFile := func(file files.File) error {
|
||||
addParams := adder{
|
||||
node: n,
|
||||
out: outChan,
|
||||
progress: progress,
|
||||
hidden: hidden,
|
||||
trickle: trickle,
|
||||
}
|
||||
|
||||
rootnd, err := addParams.addFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rnk, err := rootnd.Key()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mp := n.Pinning.GetManual()
|
||||
mp.RemovePinWithMode(rnk, pin.Indirect)
|
||||
mp.PinWithMode(rnk, pin.Recursive)
|
||||
return n.Pinning.Flush()
|
||||
}
|
||||
|
||||
// addFilesSeparately loops over a convenience slice file to
|
||||
// add each file individually. e.g. 'ipfs add a b c'
|
||||
addFilesSeparately := func(sliceFile files.File) error {
|
||||
for {
|
||||
file, err := sliceFile.NextFile()
|
||||
if err != nil && err != io.EOF {
|
||||
return err
|
||||
}
|
||||
if file == nil {
|
||||
return nil // done
|
||||
}
|
||||
|
||||
if err := addSingleFile(file); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer close(outChan)
|
||||
|
||||
for {
|
||||
file, err := req.Files().NextFile()
|
||||
if err != nil && err != io.EOF {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
if file == nil { // done
|
||||
return
|
||||
}
|
||||
|
||||
addParams := adder{
|
||||
node: n,
|
||||
out: outChan,
|
||||
progress: progress,
|
||||
hidden: hidden,
|
||||
trickle: trickle,
|
||||
}
|
||||
|
||||
if wrap {
|
||||
file = files.NewSliceFile("", []files.File{file})
|
||||
}
|
||||
|
||||
rootnd, err := addParams.addFile(file)
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
rnk, err := rootnd.Key()
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
|
||||
mp := n.Pinning.GetManual()
|
||||
mp.RemovePinWithMode(rnk, pin.Indirect)
|
||||
mp.PinWithMode(rnk, pin.Recursive)
|
||||
|
||||
err = n.Pinning.Flush()
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
// really, we're unrapping, if !wrap, because
|
||||
// req.Files() is already a SliceFile() with all of them,
|
||||
// so can just use that slice as the wrapper.
|
||||
var err error
|
||||
if wrap {
|
||||
err = addSingleFile(req.Files())
|
||||
} else {
|
||||
err = addFilesSeparately(req.Files())
|
||||
}
|
||||
if err != nil {
|
||||
res.SetError(err, cmds.ErrNormal)
|
||||
return
|
||||
}
|
||||
}()
|
||||
},
|
||||
|
Reference in New Issue
Block a user