Skip to content

Commit

Permalink
feat: handleCleanPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
vinicius-sacramento authored and DaviPtrs committed Dec 30, 2024
1 parent 291dc4d commit c6ba359
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,24 @@ export function buildIpfsGatewayUrl(cid: CID): string {
return `https://ipfs.io/ipfs/${cidStr}`;
}

const handleCleanPrefix = (uri: string, prefix: string): string => {
// Continuously remove the prefix while it is present at the start
while (uri.startsWith(prefix)) {
uri = uri.slice(prefix.length);
}
return uri;
};

export function parseIpfsUri(uri: string): Result<CID, CustomDataError> {
const validated = URL_SCHEMA.safeParse(uri);
if (!validated.success) {
const message = `Invalid IPFS URI '${uri}'`;
return { Err: { message } };
}
const ipfsPrefix = "ipfs://";
const rest = uri.startsWith(ipfsPrefix) ? uri.slice(ipfsPrefix.length) : uri;

const rest = handleCleanPrefix(uri, ipfsPrefix)

try {
const cid = CID.parse(rest);
return { Ok: cid };
Expand Down

0 comments on commit c6ba359

Please sign in to comment.