From a53506564d060b7b2d610c2817fb669f69b96634 Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Tue, 7 Jun 2022 14:13:20 +0100 Subject: [PATCH] A nasty workaround for IPFS previewing --- app/services/uriPreview.server.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/services/uriPreview.server.ts b/app/services/uriPreview.server.ts index e789275..626aa4d 100644 --- a/app/services/uriPreview.server.ts +++ b/app/services/uriPreview.server.ts @@ -37,6 +37,8 @@ async function getPeekalink(link: string): Promise { } export async function getUriPreview(uri: string): Promise { + console.log(`[getPreview][getUriPreview] ${uri}`); + const url = rewriteUrl(uri); const head = await headUri(url.href); @@ -153,9 +155,8 @@ function createPreviewImage(uri: string, head: HeadInfo): PreviewImage { } // Rewrites the URL to convert an ipfs: url to use https://ipfs.io/ipfs/ -function rewriteUrl(url: URL | string): URL { - const unmodifiedUrl = - typeof url === "string" ? new URL(url) : new URL(url.href); +function rewriteUrl(url: string): URL { + const unmodifiedUrl = new URL(url); console.log( `[getPreview][rewriteUrl] ${unmodifiedUrl.href}, protocol: ${unmodifiedUrl.protocol}, hostname: ${unmodifiedUrl.hostname}, pathname: ${unmodifiedUrl.pathname}, search: ${unmodifiedUrl.search}, hash: ${unmodifiedUrl.hash}` @@ -168,8 +169,12 @@ function rewriteUrl(url: URL | string): URL { `https://ipfs.io/ipfs/${unmodifiedUrl.pathname.substring(2)}` ); } else { + // Parse out the "hostname" from the raw url because hostnames are case-insensitive and automatically lowercased + const urlMatches = url.match(/^ipfs:\/\/([A-Za-z0-9]+)(\/.*)?/i); + const hostname = urlMatches?.[1]; + return new URL( - `https://ipfs.io/ipfs/${unmodifiedUrl.hostname}${ + `https://ipfs.io/ipfs/${hostname ?? unmodifiedUrl.hostname}${ unmodifiedUrl.pathname.length > 0 ? `/${unmodifiedUrl.pathname}` : "" }${unmodifiedUrl.search}` );