Skip to content
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
2 changes: 0 additions & 2 deletions image/src/routes/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ app.get('/*', async (c) => {
if (object === null) {
const status = await fetchIPFS({
path: fullPath,
gateway1: c.env.DEDICATED_GATEWAY,
gateway2: c.env.DEDICATED_BACKUP_GATEWAY,
})

const contentLength = status.response?.headers.get('content-length')
Expand Down
60 changes: 26 additions & 34 deletions image/src/utils/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import { $purify, getProviderList } from '@kodadot1/minipfs'

type FetchIPFS = {
path: string
gateway1: string
gateway2: string
}
import { $purify, getProviderList, ipfsProviders } from '@kodadot1/minipfs'

export function toIPFSDedicated(path: string) {
const infura = new URL(getProviderList(['infura_kodadot1'])[0])
Expand All @@ -23,36 +17,34 @@ export function ipfsUrl(ipfs?: string) {
return $purify(ipfs, ['kodadot_beta'])[0]
}

export async function fetchIPFS({ path, gateway1, gateway2 }: FetchIPFS) {
console.log('ipfs path', path)

const gwCfIpfs = await fetch(`https://cloudflare-ipfs.com/ipfs/${path}`)
console.log('fetch IPFS status', 'cloudflare-ipfs.com', gwCfIpfs.status)

if (gwCfIpfs.status === 200) {
return {
response: gwCfIpfs,
ok: true,
}
}

const gw1 = await fetch(`${gateway1}/ipfs/${path}`)
console.log('fetch IPFS status', gateway1, gw1.status)
async function resolveGateway({
path = '',
gateway = ipfsProviders.infura_kodadot1,
}) {
const response = await fetch(`${gateway}/ipfs/${path}`)
console.log('fetch IPFS status', gateway, response.status)

if (gw1.status === 200) {
return {
response: gw1,
ok: true,
}
}
return response
}

const gw2 = await fetch(`${gateway2}/ipfs/${path}`)
console.log('fetch IPFS status', gateway2, gw2.status)
export async function fetchIPFS({ path }: { path: string }) {
console.log('ipfs path', path)

if (gw2.status === 200) {
return {
response: gw2,
ok: true,
const gateways = [
ipfsProviders.apillon,
ipfsProviders.cloudflare,
ipfsProviders.filebase_kodadot,
ipfsProviders.infura_kodadot1,
]

for (const gateway of gateways) {
const response = await resolveGateway({ path, gateway })

if (response.status === 200) {
return {
response: response,
ok: true,
}
}
}

Expand Down