Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Use CoreAPI within Unixfs().Get() #11

Open
Stebalien opened this issue Mar 26, 2019 · 2 comments
Open

Use CoreAPI within Unixfs().Get() #11

Stebalien opened this issue Mar 26, 2019 · 2 comments
Labels
kind/enhancement A net-new feature or improvement to an existing feature

Comments

@Stebalien
Copy link
Member

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.

@Stebalien Stebalien added the kind/enhancement A net-new feature or improvement to an existing feature label Mar 26, 2019
@tarekbadrshalaan
Copy link

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.

@Stebalien
Copy link
Member Author

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
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind/enhancement A net-new feature or improvement to an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants