there is no extension in ipfs url.
How to check if it is image or video.
Please help me if you have any idea.
Thanks
Usually checking the Content-Type
header will work for this.
fetch(
'https://gateway.moralisipfs.com/ipfs/QmPbxeGcXhYQQNgsC6a36dDyYUcHgMLnGKnF8pVFmGsvqi'
).then((response) => {
const contentType = response.headers.get('content-type');
console.log('contentType', contentType); // image/png
});
You can try a different gateway like https://ipfs.io/ipfs/
. If youβre using a Moralis server, you could check the Content-Type of an IPFS resource using a cloud function which you call.
https://ipfs.io/ipfs/ is not working sometimes.
But could you show me the cloud function how to call with example code?
Can do something like:
Moralis.Cloud.define('getContentType', async function (request) {
return Moralis.Cloud.httpRequest({ url: request.params.url }).then(
(response) => {
const contentType = response.headers['content-type'];
return contentType;
}
);
});
Or do something similar for any kind of backend youβre using.
1 Like
Sometimes not working with cors error.
Is there any best solution to avoid this issue? same ipfs.io/ipfs
You could use your own dedicated IPFS gateway. Another way is using a service like Cloudinary where they can serve content (images/video) for you (they fetch from the gateway links and save it for you).
You can try with head requests to see if you have enough info in the response