Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Better IPFS fetching #103

Merged
merged 4 commits into from
Oct 24, 2022
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
120 changes: 120 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"test": "vitest --run --dir tests"
},
"dependencies": {
"@kodadot1/minipfs": "^0.1.0-rc.0",
"@subsquid/archive-registry": "1.0.14",
"@subsquid/cli": "^0.7.1",
"@subsquid/graphql-server": "^3.2.0",
Expand Down
4 changes: 2 additions & 2 deletions src/mappings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function handleMetadata(
return meta;
}

const metadata = await fetchMetadata<TokenMetadata>({ metadata: id });
const metadata = await fetchMetadata<TokenMetadata>(id);
if (isEmpty(metadata)) {
return null;
}
Expand Down Expand Up @@ -360,7 +360,7 @@ async function createEvent(
event.nft = final;
await store.save(event);
} catch (e) {
logError(e, (e) => logger.warn(`[[${interaction}]]: ${final.id} Reason: ${e.message}`));
logError(e, (err) => logger.warn(`[[${interaction}]]: ${final.id} Reason: ${err.message}`));
}
}

Expand Down
63 changes: 6 additions & 57 deletions src/mappings/utils/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,16 @@
import Axios from 'axios';
import { ensure, SanitizerFunc, SomethingWithMeta } from './types';
import logger, { logError } from './logger';
import { $obtain } from '@kodadot1/minipfs';
import logger from './logger';
import { ensure } from './types';

export const BASE_URL = 'https://kodadot.mypinata.cloud/';

const api = Axios.create({
baseURL: BASE_URL,
headers: {
'Content-Type': 'application/json',
},
withCredentials: false,
});

export const sanitizeIpfsUrl = (ipfsUrl: string): string => {
const rr = /^ipfs:\/\/ipfs/;
if (rr.test(ipfsUrl)) {
return ipfsUrl.replace('ipfs://', BASE_URL);
}

const r = /^ipfs:\/\//;
if (r.test(ipfsUrl)) {
return ipfsUrl.replace('ipfs://', `${BASE_URL}ipfs/`);
}

return ipfsUrl;
};

export const fetchMetadata = async <T>(
rmrk: SomethingWithMeta,
sanitizer: SanitizerFunc = sanitizeIpfsUrl,
): Promise<T> => {
export const fetchMetadata = async <T>(metadata: string): Promise<T> => {
try {
if (!rmrk.metadata) {
if (!metadata) {
return ensure<T>({});
}

const { status, data } = await api.get<T>(sanitizer(rmrk.metadata));
logger.watch('[IPFS]', status, rmrk.metadata);
if (status < 400) {
return data;
}
return await $obtain<T>(metadata);
} catch (e) {
logger.warn('IPFS Err', e);
}

return ensure<T>({});
};

export const fetchMimeType = async (ipfsLink?: string, sanitizer: SanitizerFunc = sanitizeIpfsUrl): Promise<string | undefined> => {
if (!ipfsLink) {
return undefined;
}

const assetUrl = sanitizer(ipfsLink);

try {
const { headers } = await api.head(assetUrl);
return headers['content-type'];
} catch (e) {
logError(e, (err) => logger.warn(`[MIME TYPE] Unable to access type of ${assetUrl}\n\nReason ${err.message}`));
return undefined;
}
};

// eslint-disable-next-line import/no-default-export
export default api;