mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {BskyAgent, ComAtprotoRepoUploadBlob} from '@atproto/api'
2
3/**
4 * @note It is recommended, on web, to use the `file` instance of the file
5 * selector input element, rather than a `data:` URL, to avoid
6 * loading the file into memory. `File` extends `Blob` "file" instances can
7 * be passed directly to this function.
8 */
9export async function uploadBlob(
10 agent: BskyAgent,
11 input: string | Blob,
12 encoding?: string,
13): Promise<ComAtprotoRepoUploadBlob.Response> {
14 if (typeof input === 'string' && input.startsWith('data:')) {
15 const blob = await fetch(input).then(r => r.blob())
16 return agent.uploadBlob(blob, {encoding})
17 }
18
19 if (input instanceof Blob) {
20 return agent.uploadBlob(input, {
21 encoding,
22 })
23 }
24
25 throw new TypeError(`Invalid uploadBlob input: ${typeof input}`)
26}