Identify Content-Type from CID

I’m trying to understand if I can set the content type of a file uploaded to IPFS.

Context: I’m working on suggesting a protocol improvement proposal for a project and I want to introduce support to ipfs:// URLs. However, clients must be able to know the content type of the linked file (html, json, plain text) in order to decide how to parse it. Requiring a gateway is not an option, I might as well limit the proposal to http(s)://.

Content type can’t be derived from a CID, one simple and most popular approach to know the content type (i.e. file extension) is to wrap the file in a folder, which would preserve the filename, and thus enable to know the content type. I am not sure if this is useful.

But I have observed that Web Browser can sometimes identify the content type without the file extension, if you explore that, maybe you will find a solutoin.

Hello @vrypan
You can set the content type for files on IPFS, but you need some extra steps:

  • Upload your file to IPFS. Create and upload a metadata file with the file hash and content type.

  • You can use IPNS to create a persistent address with content type info.

  • You can use file extensions (like .html, .json) to indicate content type.

  • And maintain a database mapping IPFS hashes to content types

Thank you!

This is too much overhead :frowning: for this use case.