I need to retrieve nft-metadata for the nfts that have been added to my platform. Some of these Nfts have ipfs-urls as their tokenURIs and I am looking for a way to distinguish them from regular urls (location-addressed urls).
Previously, I used is-ipfs, but it was returning false even for valid ipfs-urls!
if (mtokenUri) {
var res;
var data;
console.log("### ipfs url check ###");
console.log(
isIPFS.urlOrPath(
"ipfs://bafybeihbsysdkemc3kyylegtfopkrcfiih4exnasoql2q36fb4zawlrwhy/volcano.json",
),
);
// false for "ipfs://bafybeihbsysdkemc3kyylegtfopkrcfiih4exnasoql2q36fb4zawlrwhy/volcano.json"
//this is not very reliable
if (isIPFS.urlOrPath(mtokenUri)) {
//gives false even for a valid ipfs-url
console.log("inside erc721 ipfs: ");
isIpfsUrl = true;
console.log("*** 721 isIpfs url ***");
console.log(isIPFS.urlOrPath(mtokenUri));
// example ccid = 'QmPzhc9ezphJ85qJWfVVpeHkPieDJznpYduGhMYD7Z4Ac9'
const cid_eg2 =
"bafybeihbsysdkemc3kyylegtfopkrcfiih4exnasoql2q36fb4zawlrwhy/volcano.json";
try {
res = await ipfs.cat(mtokenUri);
// data = await res.json()
console.log(res);
} catch (error) {
console.log(error);
}
//shows:cat {<suspended>}
} else {
//do normal fetch
try {
res = await fetch(mtokenUri);
data = await res.json();
console.log("erc721 normal fetch");
console.log("*** Normal fetch data *** ");
console.log(data);
_imageUrl = data.image;
_name = data.name;
_description = data.description;
} catch (error) {
console.log(error);
}
}
}
When I attempt to fetch an ipfs-url using 'fetch', it displays-
https://i.sstatic.net/VmbF0.png
Is there a more effective/reliable way to accomplish this?