From adc04105f003bb289c40cc984a87ca86ae98c347 Mon Sep 17 00:00:00 2001 From: Tempe Techie <95053628+tempe-techie@users.noreply.github.com> Date: Mon, 4 Dec 2023 14:56:06 +0100 Subject: [PATCH] custom IPFS gateway link in nuxt config; replace the spheron IPFS gateway with the one defined in nuxt config --- components/storage/FileUploadInput.vue | 5 +++-- nuxt.config.ts | 1 + pages/nft/index.vue | 9 +++++++++ utils/linkPreviewUtils.js | 5 +++-- utils/textUtils.js | 9 +++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/components/storage/FileUploadInput.vue b/components/storage/FileUploadInput.vue index b836f131..4dee6bab 100644 --- a/components/storage/FileUploadInput.vue +++ b/components/storage/FileUploadInput.vue @@ -123,9 +123,10 @@ export default { if (this.uploadToken) { const token = this.uploadToken; - const { protocolLink } = await upload([this.file], { token }); + const { protocolLink, cid } = await upload([this.file], { token }); - const fullFileUrl = protocolLink + "/" + this.newFileName; + //const fullFileUrl = protocolLink + "/" + this.newFileName; + const fullFileUrl = this.$config.ipfsGateway + cid + "/" + this.newFileName; // emit file url this.$emit("processUploadedFileUrl", fullFileUrl); diff --git a/nuxt.config.ts b/nuxt.config.ts index dd766040..ebe6b878 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -70,6 +70,7 @@ export default defineNuxtConfig({ iggyPostAddress: "0x63FE8216a66737CFE474DF3949F9081EbD4Bd800", iggyPostMinterAddress: "0xF48D3812ceD80bC78C8553d7C3b702b0F0d63903", iggyPostStatsAddress: "0xFCF878b629fF0Ef3bC033eFfCfFD39B00c9a68C5", + ipfsGateway: "https://cloudflare-ipfs.com/ipfs/", keysAddress: "0x34E7D66455BE3f6f0cCbF3df3b7c56b482530C8E", // FriendKeys contract address keysContext: "kjzl6cwe1jw14akr2rh1j3fhup1ewfr2uyyd6l85qllbe2d5fxywt7d8rqnau6j", keysFeatured: ["tempe", "tekr"], diff --git a/pages/nft/index.vue b/pages/nft/index.vue index a8dcbc3d..27b24f98 100644 --- a/pages/nft/index.vue +++ b/pages/nft/index.vue @@ -293,6 +293,15 @@ export default { collection["image"] = cImage; } + // check if collection image uses Spheron IPFS gateway (in that case replace it with the IPFS gateway defined in the config) + if (collection.image.includes(".ipfs.sphn.link/")) { + const linkParts = collection.image.split(".ipfs.sphn.link/"); + const cid = linkParts[0].replace("https://", ""); + const newImageLink = this.$config.ipfsGateway + cid + "/" + linkParts[1]; + collection["image"] = newImageLink; + cImage = newImageLink; + } + // store collection object in storage storeCollection(window, inputArray[i], collection); diff --git a/utils/linkPreviewUtils.js b/utils/linkPreviewUtils.js index 1b686415..7f2f345e 100644 --- a/utils/linkPreviewUtils.js +++ b/utils/linkPreviewUtils.js @@ -66,8 +66,9 @@ async function fetchNftMetadata(url, addr, tokenId, rpcUrl, marketplace) { if (nftMetadataUri.startsWith("ipfs://")) { // ipfs://QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/2614 // https://dweb.link/ipfs/QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/2614 + // https://cloudflare-ipfs.com/ipfs/QmeSjSinHpPnmXmspMjwiXyN6zS4E9zccariGR3jxcaWtq/2614 - nftMetadataUri = nftMetadataUri.replace("ipfs://", "https://dweb.link/ipfs/"); + nftMetadataUri = nftMetadataUri.replace("ipfs://", "https://cloudflare-ipfs.com/ipfs/"); } const res = await axios.get(nftMetadataUri); @@ -75,7 +76,7 @@ async function fetchNftMetadata(url, addr, tokenId, rpcUrl, marketplace) { } if (json["image"].startsWith("ipfs://")) { - json["image"] = json["image"].replace("ipfs://", "https://dweb.link/ipfs/"); + json["image"] = json["image"].replace("ipfs://", "https://cloudflare-ipfs.com/ipfs/"); } if (!json["description"]) { diff --git a/utils/textUtils.js b/utils/textUtils.js index 2a872d97..d4a85b86 100644 --- a/utils/textUtils.js +++ b/utils/textUtils.js @@ -338,12 +338,21 @@ export function hasTextBlankCharacters(text) { } export function imgParsing(text) { + const config = useRuntimeConfig(); + const imageRegex = /(?:https?:\/\/(?:www\.)?)?(?:[-\w]+\.)+[^\s]+\.(?:jpe?g|gif|png|img)/gi; //const imageRegex = /(https?:\/\/.*\.(?:png|jpg|jpeg|gif))/i; if (!imageRegex.test(text)) { return text }; return text.replace(imageRegex, function(url) { + if (url.includes(".ipfs.sphn.link/")) { + // replace a link to Spheron IPFS Gateway with an IPFS Gateway set in config + const linkParts = url.split(".ipfs.sphn.link/"); + const ipfsHash = linkParts[0].replace("https://", ""); + const ipfsLink = config.ipfsGateway + ipfsHash + "/" + linkParts[1]; + return '
'; + } return '
'; }) }