ipfs/go-ipfs-http-client

Use CoreAPI within `Unixfs().Get()`

Opened this issue · 2 comments

Currently, this directly invokes the ls HTTP API endpoint but that means we need to duplicate some logic. It would be nice if we could just call Unixfs().Ls() and use that.

Hello @Stebalien
I'm so exciting to work with IPFS
I'm trying to find what this issue about
I found that we are able to use Unixfs().Get() normally as you can find in this Gist
https://gist.github.com/tarekbadrshalaan/3efa2d69d169d00dc5d717e9afa1b284#file-use_go-ipfs-http-client-go-L24

so please try to explain more and I will work on it.

This issue is about refactoring Unixfs().Get() to use Unixfs().Ls() instead of manually calling ls:

resp, err := api.core().Request("ls", p.String()).
Option("resolve-size", true).
Option("stream", true).Send(ctx)

That way, we don't need to re-implement decoding ls here:

func (it *apiIter) Next() bool {
if it.ctx.Err() != nil {
it.err = it.ctx.Err()
return false
}
var out lsOutput
if err := it.dec.Decode(&out); err != nil {
if err != io.EOF {
it.err = err
}
return false
}
if len(out.Objects) != 1 {
it.err = fmt.Errorf("ls returned more objects than expected (%d)", len(out.Objects))
return false
}
if len(out.Objects[0].Links) != 1 {
it.err = fmt.Errorf("ls returned more links than expected (%d)", len(out.Objects[0].Links))
return false
}
it.cur = out.Objects[0].Links[0]
c, err := cid.Parse(it.cur.Hash)
if err != nil {
it.err = err
return false
}
switch it.cur.Type {
case unixfs.THAMTShard, unixfs.TMetadata, unixfs.TDirectory:
it.curFile, err = it.core.getDir(it.ctx, path.IpfsPath(c), int64(it.cur.Size))
if err != nil {
it.err = err
return false
}
case unixfs.TFile:
it.curFile, err = it.core.getFile(it.ctx, path.IpfsPath(c), int64(it.cur.Size))
if err != nil {
it.err = err
return false
}
default:
it.err = fmt.Errorf("file type %d not supported", it.cur.Type)
return false
}
return true
}