How to get file content

I am using this js-ipfs/FILES.md at 3d68a4493d7dcc550a4c449490f5c950d00c27fc · ipfs/js-ipfs · GitHub with ipfs.get

Content is an Uint8Array. How do I get a file from it ? the idea is that sometimes hash of ipfs could have json file, sometimes png and so on. How is it possible to get the actual file ? I know that i can directly use fetch(https://gateway.com/ipfs/cidHere) but, this is a little bit worse, because gateways are usually slow… So I was thinking that ipfs.get was really way faster, but the content it returns is just uint8array and can’t really get the file out of it.

What would you suggest ?

That is the file. Files are arrays of bytes.

1 Like

You’re probably looking for JavaScript: Create and save file - Stack Overflow

1 Like

@stebalien

Q1. I need to get file’s content without downloading it, because it’s a metadata.json which contains some key: values I need to extract. Do you think this is still possible ? but for sure, this goes out of IPFS topic totally.

Q2. Do you think ipfs.get is much faster than getting the file from gateway ? in the end, ipfs.get still makes a get request to the node… But in my opinion, ipfs.get doesn’t make a request to gateway, but directly to a node and this is why I think it’s much faster than gateway… Do I sound right ?

Thanks a lot.

Q1. I need to get file’s content without downloading it, because it’s a metadata.json which contains some key: values I need to extract. Do you think this is still possible ? but for sure, this goes out of IPFS topic totally.

Yes. You need to convert the bytes to a string (casting - Converting byte array to string in javascript - Stack Overflow), then parse it as JSON.

Q2. Do you think ipfs.get is much faster than getting the file from gateway ? in the end, ipfs.get still makes a get request to the node… But in my opinion, ipfs.get doesn’t make a request to gateway, but directly to a node and this is why I think it’s much faster than gateway… Do I sound right ?

ipfs.get is the right way to download files as it actually uses IPFS. The gateway exists as a crutch until IPFS is widely deployed and supported.

In terms of performance… it depends. If the gateway is under load, it might be slower. However, once the gateway finds/downloads the file once, it’ll likely be faster.

1 Like

@stebalien

About the first one… It’s really hard to be doing this…

Sometimes, I get the direct hash which i can’t figure out what type it is, because ipfs.get doesn’t return its type at all which means I can’t download the file or anything like this… Sometimes it’s png, jpg or anything else and sometimes, it’s .json which means I need to decode it as you suggested above, but not sure about anything else as i can’t figure out about file type…

You either need to:

  1. Get the filename. This is a part of the directory not the file itself. If someone just gives you a hash of a file, you won’t have this information.
  2. Detect the file type based on magic numbers. For that you’ll have to google “detect file type javascript”.
1 Like