about summary refs log tree commit diff
path: root/src/lib/media/video/upload.web.ts
blob: d1b441a369df5f582af444ea817a6e4645a11d44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {AppBskyVideoDefs} from '@atproto/api'
import {BskyAgent} from '@atproto/api'
import {I18n} from '@lingui/core'
import {msg} from '@lingui/macro'
import {nanoid} from 'nanoid/non-secure'

import {AbortError} from '#/lib/async/cancelable'
import {ServerError} from '#/lib/media/video/errors'
import {CompressedVideo} from '#/lib/media/video/types'
import {getServiceAuthToken, getVideoUploadLimits} from './upload.shared'
import {createVideoEndpointUrl, mimeToExt} from './util'

export async function uploadVideo({
  video,
  agent,
  did,
  setProgress,
  signal,
  _,
}: {
  video: CompressedVideo
  agent: BskyAgent
  did: string
  setProgress: (progress: number) => void
  signal: AbortSignal
  _: I18n['_']
}) {
  if (signal.aborted) {
    throw new AbortError()
  }
  await getVideoUploadLimits(agent, _)

  const uri = createVideoEndpointUrl('/xrpc/app.bsky.video.uploadVideo', {
    did,
    name: `${nanoid(12)}.${mimeToExt(video.mimeType)}`,
  })

  let bytes = video.bytes
  if (!bytes) {
    if (signal.aborted) {
      throw new AbortError()
    }
    bytes = await fetch(video.uri).then(res => res.arrayBuffer())
  }

  if (signal.aborted) {
    throw new AbortError()
  }
  const token = await getServiceAuthToken({
    agent,
    lxm: 'com.atproto.repo.uploadBlob',
    exp: Date.now() / 1000 + 60 * 30, // 30 minutes
  })

  if (signal.aborted) {
    throw new AbortError()
  }
  const xhr = new XMLHttpRequest()
  const res = await new Promise<AppBskyVideoDefs.JobStatus>(
    (resolve, reject) => {
      xhr.upload.addEventListener('progress', e => {
        const progress = e.loaded / e.total
        setProgress(progress)
      })
      xhr.onloadend = () => {
        if (signal.aborted) {
          reject(new AbortError())
        } else if (xhr.readyState === 4) {
          const uploadRes = JSON.parse(
            xhr.responseText,
          ) as AppBskyVideoDefs.JobStatus
          resolve(uploadRes)
        } else {
          reject(new ServerError(_(msg`Failed to upload video`)))
        }
      }
      xhr.onerror = () => {
        reject(new ServerError(_(msg`Failed to upload video`)))
      }
      xhr.open('POST', uri)
      xhr.setRequestHeader('Content-Type', video.mimeType)
      xhr.setRequestHeader('Authorization', `Bearer ${token}`)
      xhr.send(bytes)
    },
  )

  if (!res.jobId) {
    throw new ServerError(res.error || _(msg`Failed to upload video`))
  }

  if (signal.aborted) {
    throw new AbortError()
  }
  return res
}