Skip to content

Commit

Permalink
fix: ipfs logic when uri is just a cid (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertfolch-redeemeum committed May 21, 2024
1 parent d7db7fa commit a774030
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"@apollo/client": "^3.8.1",
"@bosonprotocol/chat-sdk": "^1.3.1-alpha.9",
"@bosonprotocol/react-kit": "^0.30.0",
"@bosonprotocol/react-kit": "^0.30.1",
"@davatar/react": "^1.10.4",
"@ethersproject/address": "^5.6.1",
"@ethersproject/units": "^5.7.0",
Expand Down
7 changes: 7 additions & 0 deletions src/lib/utils/ipfs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,11 @@ describe("getIpfsGatewayUrl", () => {
const result = getIpfsGatewayUrl(uri);
expect(result).toBe(expected);
});

it("should return a sanitized URL when the input URI is just the cid", () => {
const cid = "QmSeuDdMZqHxTqvraUKzKKJn9Vo9RELsA8xJUbDn6BGc6d";
const expected = `https://ipfs.io/ipfs/${cid}`;
const result = getIpfsGatewayUrl(cid);
expect(result).toBe(expected);
});
});
7 changes: 7 additions & 0 deletions src/lib/utils/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export function getIpfsGatewayUrl(
}
const { gateway = CONFIG.ipfsGateway } = opts;
try {
try {
CID.parse(uri);
const cid = uri;
return `${gateway}/${cid}`.replace(/([^:]\/)\/+/g, "$1");
} catch {
// if uri it's not the cid only, then continue as expected and ignore this error
}
const url = new URL(
uri.startsWith("ipfs://") ? uri.replace("ipfs://", "https://") : uri
);
Expand Down

0 comments on commit a774030

Please sign in to comment.