import IPFSGatewayTools from '@pinata/ipfs-gateway-tools/dist/node'; import config from '../config/config'; export const ipfsConvertToGateway = ( url: string | null, forcePinataGateway: boolean = false ): string | undefined => { if (!url) { return; } if (url.startsWith('http') && !url.includes('moralis')) { return url; } const sourceUrl = url; const gatewayTools = new IPFSGatewayTools(); const { cid } = gatewayTools.containsCID(url); const desiredUrl = gatewayTools.convertToDesiredGateway( sourceUrl, forcePinataGateway ? config.ipfsPinataGatewayUrl : config.ipfsGatewayUrl ); const splitted = sourceUrl.split(cid); return desiredUrl + (splitted[1].length > 1 ? splitted[1] : ''); }; export const getIpfsCid = (url: string | null): string | undefined => { if (!url) { return; } const gatewayTools = new IPFSGatewayTools(); const cid = gatewayTools.containsCID(url).cid; if (cid) { return `ipfs://${cid}`; } return; };