Skip to content

Instantly share code, notes, and snippets.

@korrio
Created March 27, 2025 06:49
Show Gist options
  • Save korrio/193f52cd4bbad13da92724f957a44e2a to your computer and use it in GitHub Desktop.
Save korrio/193f52cd4bbad13da92724f957a44e2a to your computer and use it in GitHub Desktop.
fetchFileByURL.tsx
async function fetchFileByURL(url: string): Promise<File> {
const res = await fetch(url, {
credentials: 'include',
method: 'GET',
})
if (!res.ok) {
throw new Error(`Failed to fetch file from ${url}, status: ${res.status}`)
}
const data = await res.arrayBuffer()
return {
name: url.split('/').pop() || `file-${Date.now()}`,
data: Buffer.from(data),
mimetype: `image/${url.split('.').pop()}`,
size: data.byteLength,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment