Skip to content

Commit

Permalink
Ipfs URL/gateway fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinBrett committed Jan 10, 2025
1 parent 43986ee commit 74fc103
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions utils/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>/",
];

Expand Down Expand Up @@ -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(
Expand All @@ -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> => {
Expand Down

0 comments on commit 74fc103

Please sign in to comment.