Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ipfs logic when uri is just a cid #1081

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading