Skip to content

Commit 37af87b

Browse files
authored
Merge pull request #11 from kleros/fix/retry-tokens
fix: retry 5 times to get image buffer data and sharp it
2 parents d7feb78 + f47f13f commit 37af87b

File tree

1 file changed

+68
-62
lines changed

1 file changed

+68
-62
lines changed

src/index.ts

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -68,73 +68,79 @@ async function main() {
6868
} else {
6969
const imageUrl = `${process.env.IPFS_GATEWAY}${token.logoURI}`
7070
console.info(`Fetching image from URL: ${imageUrl}`)
71-
const imageBuffer = await (await fetch(imageUrl)).buffer()
72-
73-
try {
74-
const imageSharp = sharp(imageBuffer)
75-
const metadata = await imageSharp.metadata()
76-
77-
console.debug(`Image metadata:`, metadata)
78-
79-
if (!metadata.format) {
80-
throw new Error('Unsupported image format')
81-
}
82-
83-
const resizedImageBuffer = await imageSharp.resize(64, 64).png().toBuffer()
84-
85-
console.info(` Pinning shrunk image to ${process.env.IPFS_GATEWAY}`)
86-
let ipfsResponse: IPFSResponse[] | null = null
87-
88-
for (let attempt = 1; attempt <= 10; attempt++) {
89-
try {
90-
ipfsResponse = await ipfsPublish(
91-
`${token.symbol}.png`,
92-
resizedImageBuffer,
93-
)
94-
console.info(` Done.`)
95-
break
96-
} catch (err) {
97-
console.warn(
98-
` Failed to upload ${token.symbol} to gateway IPFS.`,
99-
err,
100-
)
101-
if (attempt === 5) {
102-
console.error(
103-
` Could not upload ${token.symbol} image to gateway IPFS after 5 attempts.`,
71+
for (let attempt = 1; attempt <= 10; attempt++) {
72+
try {
73+
const imageBuffer = await (await fetch(imageUrl)).buffer()
74+
const imageSharp = sharp(imageBuffer)
75+
const metadata = await imageSharp.metadata()
76+
77+
console.debug(`Image metadata:`, metadata)
78+
79+
if (!metadata.format) {
80+
throw new Error('Unsupported image format')
81+
}
82+
83+
const resizedImageBuffer = await imageSharp.resize(64, 64).png().toBuffer()
84+
85+
console.info(` Pinning shrunk image to ${process.env.IPFS_GATEWAY}`)
86+
let ipfsResponse: IPFSResponse[] | null = null
87+
88+
for (let attemptIPFS = 1; attemptIPFS <= 5; attemptIPFS++) {
89+
try {
90+
ipfsResponse = await ipfsPublish(
91+
`${token.symbol}.png`,
92+
resizedImageBuffer,
93+
)
94+
console.info(` Done.`)
95+
break
96+
} catch (err) {
97+
console.warn(
98+
` Failed to upload ${token.symbol} to gateway IPFS.`,
99+
err,
104100
)
105-
} else {
106-
console.warn(` Retrying ${attempt + 1} of ${5}`)
101+
if (attemptIPFS === 5) {
102+
console.error(
103+
` Could not upload ${token.symbol} image to gateway IPFS after 5 attempts.`,
104+
)
105+
} else {
106+
console.warn(` Retrying ${attemptIPFS + 1} of ${5}`)
107+
}
107108
}
108109
}
110+
111+
if (!ipfsResponse) {
112+
console.error()
113+
throw new Error(
114+
`Failed to upload ${token.symbol} image to ipfs gateway. Halting`,
115+
)
116+
}
117+
118+
const multihash = ipfsResponse[0].hash
119+
120+
if (ipfsResponse) {
121+
// Was successfully pinned to IPFS, no point in resubmitting.
122+
console.log(` Caching ${multihash}`)
123+
await db.put(
124+
cacheName(token.logoURI as string, token.symbol),
125+
multihash,
126+
)
127+
}
128+
129+
tokensWithLogo.push({
130+
...token,
131+
logoURI: `ipfs://${multihash}`,
132+
})
133+
break
134+
} catch (err) {
135+
console.error(`Failed to process image for token ${token.symbol}:`, err)
136+
console.error(`Token details:`, token)
137+
console.warn(` Retrying ${attempt + 1} of ${10}`)
138+
if (attempt == 10) {
139+
throw (err)
140+
}
109141
}
110-
111-
if (!ipfsResponse) {
112-
console.error()
113-
throw new Error(
114-
`Failed to upload ${token.symbol} image to ipfs gateway. Halting`,
115-
)
116-
}
117-
118-
const multihash = ipfsResponse[0].hash
119-
120-
if (ipfsResponse) {
121-
// Was successfully pinned to IPFS, no point in resubmitting.
122-
console.log(` Caching ${multihash}`)
123-
await db.put(
124-
cacheName(token.logoURI as string, token.symbol),
125-
multihash,
126-
)
127-
}
128-
129-
tokensWithLogo.push({
130-
...token,
131-
logoURI: `ipfs://${multihash}`,
132-
})
133-
} catch (err) {
134-
console.error(`Failed to process image for token ${token.symbol}:`, err)
135-
console.error(`Token details:`, token)
136-
throw err // stop execution on error
137142
}
143+
138144
}
139145
}
140146

0 commit comments

Comments
 (0)