poli-charts
Version:
The best graphics for your Hedera Network application NFTs.
35 lines (29 loc) • 1.14 kB
JavaScript
// Función para convertir una URL IPFS en un enlace HTTP utilizando gateways alternativos
export async function resolveIpfsImageUrl(ipfsUrl) {
if (!ipfsUrl || typeof ipfsUrl !== "string") {
console.warn("Invalid IPFS URL:", ipfsUrl);
return null; // Devuelve null si la URL no es válida
}
const gateways = ["https://dweb.link/ipfs/"];
const ipfsHash = removeTrailingSlash(ipfsUrl.split("ipfs://")[1]);
for (const gateway of gateways) {
const gatewayUrl = `${gateway}${ipfsHash}`;
try {
const response = await fetch(gatewayUrl, {
method: "GET",
headers: { accept: "application/json" },
});
if (response.ok) {
return gatewayUrl; // Retorna la primera URL válida
}
} catch (error) {
console.warn(`Error fetching from gateway ${gateway}:`, error);
}
}
console.error(`Failed to resolve IPFS image URL: ${ipfsUrl}`);
return null;
}
function removeTrailingSlash(url) {
if (!url || typeof url !== "string") return url; // Verifica que la URL sea válida
return url.endsWith("/") ? url.slice(0, -1) : url;
}