Ipfs URL/gateway fixes
Some checks failed
Tests / tests (push) Has been cancelled

This commit is contained in:
Dustin Brett 2025-01-09 22:28:50 -08:00
parent 43986ee86f
commit 74fc1036e6

View File

@ -7,9 +7,7 @@ import {
let IPFS_GATEWAY_URL = "";
export const IPFS_GATEWAY_URLS = [
"https://<CID>.ipfs.cf-ipfs.com/",
"https://<CID>.ipfs.dweb.link/",
"https://cloudflare-ipfs.com/ipfs/<CID>/",
"https://gateway.ipfs.io/ipfs/<CID>/",
];
@ -65,11 +63,12 @@ export const getIpfsGatewayUrl = async (
if (!IPFS_GATEWAY_URL) return "";
}
const { pathname, protocol, search } = new URL(ipfsUrl);
const { hostname, pathname, protocol, search } = new URL(ipfsUrl);
if (protocol !== "ipfs:") return "";
const [cid = "", ...path] = pathname.split("/").filter(Boolean);
const fullPath = `${hostname}${pathname}`;
const [cid = "", ...path] = fullPath.split("/").filter(Boolean);
const { CID } = await import("multiformats");
return `${IPFS_GATEWAY_URL.replace(
@ -82,17 +81,16 @@ export const getIpfsFileName = async (
ipfsUrl: string,
ipfsData: Buffer
): Promise<string> => {
const { pathname, searchParams } = new URL(ipfsUrl);
const { hostname, pathname, searchParams } = new URL(ipfsUrl);
const fileName = searchParams.get("filename");
if (fileName) return fileName;
const { fileTypeFromBuffer } = await import("file-type");
const { ext = "" } = (await fileTypeFromBuffer(ipfsData)) || {};
const fullPath = `${hostname}${pathname}`;
return `${pathname.split("/").filter(Boolean).join("_")}${
ext ? `.${ext}` : ""
}`;
return `${fullPath.split("/").filter(Boolean).join("_")}${ext ? `.${ext}` : ""}`;
};
export const getIpfsResource = async (ipfsUrl: string): Promise<Buffer> => {