Skip to content

Commit

Permalink
fix crashing when image download fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hfxbse committed May 14, 2024
1 parent b7c81fe commit ba32de6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
13 changes: 6 additions & 7 deletions src/cli/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ export async function settleGraph(graph: UnsettledUserGraph) {
profile: {
...user.profile,
image: await user.profile.image
.then(blobToDataUrl)
.catch((reason: any) => {
console.error({
message: `Failed to download profile picture. (User: ${user.profile.username})`,
reason
})
.then((image: Blob | null) => {
if (!image) {
console.error(`Failed to download profile picture. (User: ${user.profile.username})`)
return null;
}

return null;
return blobToDataUrl(image)
})
}
}
Expand Down
22 changes: 14 additions & 8 deletions src/instagram/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,24 @@ export async function fetchUser(username: string, session?: SessionData): Promis
return mapped;
}

export async function downloadProfilePicture(source: string | undefined): Promise<Blob> | null {
export async function downloadProfilePicture(source: string | undefined): Promise<Blob | null> {
if (!source) return null

const response = await fetch(source, {
headers: {
"Sec-Fetch-Site": "same-origin",
try {
const response = await fetch(source, {
headers: {
"Sec-Fetch-Site": "same-origin",
}
})

if (!response.ok) {
await response.text().then(console.error, () => {})
return null
}
})

if (!response.ok) {
throw Error(await response.text())
return await response.blob()
} catch (e) {
return null
}

return await response.blob()
}

0 comments on commit ba32de6

Please sign in to comment.