A nasty workaround for IPFS previewing

This commit is contained in:
Eric Allam
2022-06-07 14:13:24 +01:00
parent 2cb9bcd991
commit a53506564d
+9 -4
View File
@@ -37,6 +37,8 @@ async function getPeekalink(link: string): Promise<PreviewResult> {
} }
export async function getUriPreview(uri: string): Promise<PreviewResult> { export async function getUriPreview(uri: string): Promise<PreviewResult> {
console.log(`[getPreview][getUriPreview] ${uri}`);
const url = rewriteUrl(uri); const url = rewriteUrl(uri);
const head = await headUri(url.href); 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/ // Rewrites the URL to convert an ipfs: url to use https://ipfs.io/ipfs/
function rewriteUrl(url: URL | string): URL { function rewriteUrl(url: string): URL {
const unmodifiedUrl = const unmodifiedUrl = new URL(url);
typeof url === "string" ? new URL(url) : new URL(url.href);
console.log( console.log(
`[getPreview][rewriteUrl] ${unmodifiedUrl.href}, protocol: ${unmodifiedUrl.protocol}, hostname: ${unmodifiedUrl.hostname}, pathname: ${unmodifiedUrl.pathname}, search: ${unmodifiedUrl.search}, hash: ${unmodifiedUrl.hash}` `[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)}` `https://ipfs.io/ipfs/${unmodifiedUrl.pathname.substring(2)}`
); );
} else { } 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( return new URL(
`https://ipfs.io/ipfs/${unmodifiedUrl.hostname}${ `https://ipfs.io/ipfs/${hostname ?? unmodifiedUrl.hostname}${
unmodifiedUrl.pathname.length > 0 ? `/${unmodifiedUrl.pathname}` : "" unmodifiedUrl.pathname.length > 0 ? `/${unmodifiedUrl.pathname}` : ""
}${unmodifiedUrl.search}` }${unmodifiedUrl.search}`
); );