Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reading data via object get vs. cat vs. object data #4115

Closed
thisconnect opened this issue Aug 1, 2017 · 4 comments
Closed

reading data via object get vs. cat vs. object data #4115

thisconnect opened this issue Aug 1, 2017 · 4 comments

Comments

@thisconnect
Copy link
Contributor

Version information:

go-ipfs version: 0.4.10-4679f806
Repo version: 5
System version: amd64/darwin
Golang version: go1.8.3

using Nodejs v6.11.0
ipfs-api@14.1.1

Type:

bug

Severity:

High?

Description:

trying to read the data of a PNG (38,994 bytes) in nodejs via js-ipfs-api and go-ipfs.

observations:

  • api.object.get returns a JSON object with a 'data' property containing a buffer with 72014 elements.
  • api.object.data returns a buffer with 39004
  • api.cat concating all chunks of the stream into a single buffer gives 38994 (correct!)

questions:

  • is this a bug / should they all return the same data?
  • what kind of buffer/array is the data field of the result of api.object.get and why is it so big?
  • api.object.data seems to have a few bytes in front
    • result.slice(6) seems to give the correct start, but I am unsure what to cut off the end

This is probably related to

Slicing off data (off the start/end) seems wrong and might break in the future if this is fixed. Any suggestion is appreciated.

Currently I try to

  • receive the first bytes to guess what file type
  • depending on the guess trying to stream the data (audio, video, picture)
@Stebalien
Copy link
Member

TL;DR, you probably don't want to use the object APIs.

IPFS files are stored in DagProtobuf IPLD nodes with the following format:

{
    "data": [bytes],
    "links": [ link1, link2, link3 ] // Links to other IPLD nodes.
}

In IPFS, the "data" field is usually another protocol buffer (encoded into raw bytes).

So, what's happening here?

  • api.object.get is returning a JSON representation of the DagProtobuf IPLD node with the data field encoded (raw binary and JSON don't play well together). The encoding here is a known and the discussion is currently happening at Handle IPLD changes proposed by @davidar and @mildred ipld/specs#39 (follow the trail of links if you want more context...).
  • api.object.data is returning the raw bytes of the data field (no encoding). This is slightly larger than the file itself because it's actually a protocol buffer that stores some metadata along with the file.
  • api.cat returns the actual file (as bytes).

@thisconnect
Copy link
Contributor Author

Thanks so much for your explanation. Sorry I wanted to reply earlier..

The discussion is kind of hard to follow

Is it correct that in the future we could use dag abi, with either CBOR or EJSON that would have better support for binary?

https://github.com/ipfs/ipfs/blob/master/IMPLEMENTATION_STATUS.md#dag

Should I close this issue, duplicate of #1582 ?

@Stebalien
Copy link
Member

The discussion is kind of hard to follow

I agree, sorry about that.

Is it correct that in the future we could use dag abi, with either CBOR or EJSON that would have better support for binary?

Yes. The object API will eventually be replaced with the dag API anyways. Here is the latest state of things: https://github.com/ipfs/interface-ipfs-core/issues/81#issuecomment-322756354

However, for what you're doing, you probably want to just use api.cat to cat the file as-is.

Should I close this issue, duplicate of #1582 ?

Yes. All of these issues are known issues, it's just a simple matter of fixing them 😄.

@thisconnect
Copy link
Contributor Author

thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants