Sneak peek at a resource without downloading


I am currently working on a solution that requires checking the resource type. In other words: is it a directory or a file and its extension.

I tried using API method /dag/get?arg=hash to retrieve the information about a particular resource, although it works great for directories as I can understand what this directory contains and see all extensions of all files under it, this approach does not quite work for single files because it requires to download the entire file (that is encoded inside “data” field) and only after that I see the JSON response containing no information but the data and an empty links array.

My question: is there a way to check what’s the type and if possible an extension of the resource. Eventually I am looking forward to create the following pipeline:

  1. Input the hash
  2. Get the information about the hash: type (file/directory) and if it’s a single file - its extension
    In case of a file it’s crucial to avoid downloading the whole file as it could be a large file and it will slow down the aggregation process by a lot.

Any help is appreciated

The filename (and extension) is a property of the directory, not of the file itself. Therefore there is no way to know the filename from it’s CID.

Also, ipfs is content agnostic so, unless your file actually has been added using specific CID-codec, ipfs just sees “unixfs” nodes.

The only thing you can do is to try to sniff the first block of the file and figure out the type from there. The gateway endpoint does this to set Content-Type headers I think, but probably reads the full file first. You can see the blocks in a file with the refs command. The first one shouold be the first block. block get can get it and you can do file block.bin to figure out types, or use some library (just found after googling).

Thanks! Will try to work around that.