Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455import {type ImagePickerAsset} from 'expo-image-picker'import {type AppBskyVideoDefs, type AtpAgent, type BlobRef} from '@atproto/api'import {type I18n} from '@lingui/core'import {msg} from '@lingui/core/macro'
import {AbortError} from '#/lib/async/cancelable'import {VIDEO_MAX_SIZE_MB} from '#/lib/constants'import {compressVideo} from '#/lib/media/video/compress'import { ServerError, UploadLimitError, VideoTooLargeError,} from '#/lib/media/video/errors'import {type CompressedVideo} from '#/lib/media/video/types'import {uploadVideo} from '#/lib/media/video/upload'import {createVideoAgent} from '#/lib/media/video/util'import {isNetworkError} from '#/lib/strings/errors'import {logger} from '#/logger'
type CaptionsTrack = {lang: string; file: File}
export type VideoAction = | { type: 'compressing_to_uploading' video: CompressedVideo signal: AbortSignal } | { type: 'uploading_to_processing' jobId: string signal: AbortSignal } | {type: 'to_error'; error: string; signal: AbortSignal} | { type: 'to_done' blobRef: BlobRef signal: AbortSignal } | {type: 'update_progress'; progress: number; signal: AbortSignal} | { type: 'update_alt_text' altText: string signal: AbortSignal } | { type: 'update_captions' updater: (prev: CaptionsTrack[]) => CaptionsTrack[] signal: AbortSignal } | { type: 'update_job_status' jobStatus: AppBskyVideoDefs.JobStatus signal: AbortSignal }
const noopController = new AbortController()noopController.abort()
export const NO_VIDEO = Object.freeze({ status: 'idle', progress: 0, abortController: noopController, asset: undefined, video: undefined, jobId: undefined, pendingPublish: undefined, altText: '', captions: [],})
export type NoVideoState = typeof NO_VIDEO
type ErrorState = { status: 'error' progress: 100 abortController: AbortController asset: ImagePickerAsset | null video: CompressedVideo | null jobId: string | null error: string pendingPublish?: undefined altText: string captions: CaptionsTrack[]}
type CompressingState = { status: 'compressing' progress: number abortController: AbortController asset: ImagePickerAsset video?: undefined jobId?: undefined pendingPublish?: undefined altText: string captions: CaptionsTrack[]}
type UploadingState = { status: 'uploading' progress: number abortController: AbortController asset: ImagePickerAsset video: CompressedVideo jobId?: undefined pendingPublish?: undefined altText: string captions: CaptionsTrack[]}
type ProcessingState = { status: 'processing' progress: number abortController: AbortController asset: ImagePickerAsset video: CompressedVideo jobId: string jobStatus: AppBskyVideoDefs.JobStatus | null pendingPublish?: undefined altText: string captions: CaptionsTrack[]}
type DoneState = { status: 'done' progress: 100 abortController: AbortController asset: ImagePickerAsset video: CompressedVideo jobId?: undefined pendingPublish: {blobRef: BlobRef} altText: string captions: CaptionsTrack[]}
export type VideoState = | ErrorState | CompressingState | UploadingState | ProcessingState | DoneState
export function createVideoState( asset: ImagePickerAsset, abortController: AbortController,): CompressingState { return { status: 'compressing', progress: 0, abortController, asset, altText: '', captions: [], }}
export function videoReducer( state: VideoState, action: VideoAction,): VideoState { if (action.signal.aborted || action.signal !== state.abortController.signal) { // This action is stale and the process that spawned it is no longer relevant. return state } if (action.type === 'to_error') { return { status: 'error', progress: 100, abortController: state.abortController, error: action.error, asset: state.asset ?? null, video: state.video ?? null, jobId: state.jobId ?? null, altText: state.altText, captions: state.captions, } } else if (action.type === 'update_progress') { if (state.status === 'compressing' || state.status === 'uploading') { return { ...state, progress: action.progress, } } } else if (action.type === 'update_alt_text') { return { ...state, altText: action.altText, } } else if (action.type === 'update_captions') { return { ...state, captions: action.updater(state.captions), } } else if (action.type === 'compressing_to_uploading') { if (state.status === 'compressing') { return { status: 'uploading', progress: 0, abortController: state.abortController, asset: state.asset, video: action.video, altText: state.altText, captions: state.captions, } } return state } else if (action.type === 'uploading_to_processing') { if (state.status === 'uploading') { return { status: 'processing', progress: 0, abortController: state.abortController, asset: state.asset, video: state.video, jobId: action.jobId, jobStatus: null, altText: state.altText, captions: state.captions, } } } else if (action.type === 'update_job_status') { if (state.status === 'processing') { return { ...state, jobStatus: action.jobStatus, progress: action.jobStatus.progress !== undefined ? action.jobStatus.progress / 100 : state.progress, } } } else if (action.type === 'to_done') { if (state.status === 'processing') { return { status: 'done', progress: 100, abortController: state.abortController, asset: state.asset, video: state.video, pendingPublish: { blobRef: action.blobRef, }, altText: state.altText, captions: state.captions, } } } console.error( 'Unexpected video action (' + action.type + ') while in ' + state.status + ' state', ) return state}
function trunc2dp(num: number) { return Math.trunc(num * 100) / 100}
export async function processVideo( asset: ImagePickerAsset, dispatch: (action: VideoAction) => void, agent: AtpAgent, did: string, signal: AbortSignal, i18n: I18n,) { let video: CompressedVideo | undefined try { video = await compressVideo(asset, { onProgress: num => { dispatch({type: 'update_progress', progress: trunc2dp(num), signal}) }, signal, }) } catch (e) { const message = getCompressErrorMessage(e, i18n) if (message !== null) { dispatch({ type: 'to_error', error: message, signal, }) } return } dispatch({ type: 'compressing_to_uploading', video, signal, })
let uploadResponse: AppBskyVideoDefs.JobStatus | undefined try { uploadResponse = await uploadVideo({ video, agent, did, signal, i18n, setProgress: p => { dispatch({type: 'update_progress', progress: p, signal}) }, }) } catch (e) { const message = getUploadErrorMessage(e, i18n) if (message !== null) { dispatch({ type: 'to_error', error: message, signal, }) } return }
const jobId = uploadResponse.jobId dispatch({ type: 'uploading_to_processing', jobId, signal, })
let pollFailures = 0 while (true) { if (signal.aborted) { return // Exit async loop }
const videoAgent = createVideoAgent() let status: AppBskyVideoDefs.JobStatus | undefined let blob: BlobRef | undefined try { const response = await videoAgent.app.bsky.video.getJobStatus({jobId}) status = response.data.jobStatus pollFailures = 0
if (status.state === 'JOB_STATE_COMPLETED') { blob = status.blob if (!blob) { throw new Error('Job completed, but did not return a blob') } } else if (status.state === 'JOB_STATE_FAILED') { throw new Error(status.error ?? 'Job failed to process') } } catch (e) { if (!status) { pollFailures++ if (pollFailures < 50) { await new Promise(resolve => setTimeout(resolve, 5000)) continue // Continue async loop } }
logger.error('Error processing video', {safeMessage: e}) dispatch({ type: 'to_error', error: i18n._(msg`Video failed to process`), signal, }) return // Exit async loop }
if (blob) { dispatch({ type: 'to_done', blobRef: blob, signal, }) } else { dispatch({ type: 'update_job_status', jobStatus: status, signal, }) }
if ( status.state !== 'JOB_STATE_COMPLETED' && status.state !== 'JOB_STATE_FAILED' ) { await new Promise(resolve => setTimeout(resolve, 1500)) continue // Continue async loop }
return // Exit async loop }}
function getCompressErrorMessage(e: unknown, i18n: I18n): string | null { if (e instanceof AbortError) { return null } if (e instanceof VideoTooLargeError) { return i18n._( msg`The selected video is larger than ${VIDEO_MAX_SIZE_MB} MB. Please try again with a smaller file.`, ) } logger.error('Error compressing video', {safeMessage: e}) return i18n._(msg`An error occurred while compressing the video.`)}
function getUploadErrorMessage(e: unknown, i18n: I18n): string | null { if (e instanceof AbortError) { return null } if (e instanceof ServerError || e instanceof UploadLimitError) { // https://github.com/bluesky-social/tango/blob/lumi/lumi/worker/permissions.go#L77 switch (e.message) { case 'User is not allowed to upload videos': return i18n._(msg`You are not allowed to upload videos.`) case 'Uploading is disabled at the moment': return i18n._( msg`Hold up! We’re gradually giving access to video, and you’re still waiting in line. Check back soon!`, ) case "Failed to get user's upload stats": return i18n._( msg`We were unable to determine if you are allowed to upload videos. Please try again.`, ) case 'User has exceeded daily upload bytes limit': return i18n._( msg`You've reached your daily limit for video uploads (too many bytes)`, ) case 'User has exceeded daily upload videos limit': return i18n._( msg`You've reached your daily limit for video uploads (too many videos)`, ) case 'Account is not old enough to upload videos': return i18n._( msg`Your account is not yet old enough to upload videos. Please try again later.`, ) case 'file size (100000001 bytes) is larger than the maximum allowed size (100000000 bytes)': case 'file size (300000001 bytes) is larger than the maximum allowed size (300000000 bytes)': return i18n._( msg`The selected video is larger than ${VIDEO_MAX_SIZE_MB} MB. Please try again with a smaller file.`, ) case 'Confirm your email address to upload videos': return i18n._(msg`Please confirm your email address to upload videos.`) } }
if (isNetworkError(e)) { return i18n._( msg`An error occurred while uploading the video. Please check your internet connection and try again.`, ) } else { // only log errors if they are unknown (and not network errors) logger.error('Error uploading video', {safeMessage: e}) }
const message = e instanceof Error ? e.message : '' return i18n._(msg`An error occurred while uploading the video. ${message}`)}