Created
March 27, 2025 06:49
-
-
Save korrio/193f52cd4bbad13da92724f957a44e2a to your computer and use it in GitHub Desktop.
fetchFileByURL.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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