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 (
15 typeof input === 'string' &&
16 (input.startsWith('data:') || input.startsWith('blob:'))
17 ) {
18 const blob = await fetch(input).then(r => r.blob())
19 return agent.uploadBlob(blob, {encoding})
20 }
21
22 if (input instanceof Blob) {
23 return agent.uploadBlob(input, {
24 encoding,
25 })
26 }
27
28 throw new TypeError(`Invalid uploadBlob input: ${typeof input}`)
29}