@@ -8,8 +8,8 @@ import {cancelable} from '#/lib/async/cancelable'
8
8
import { ServerError } from '#/lib/media/video/errors'
9
9
import { CompressedVideo } from '#/lib/media/video/types'
10
10
import { createVideoEndpointUrl , mimeToExt } from '#/state/queries/video/util'
11
- import { useAgent , useSession } from '#/state/session'
12
- import { getServiceAuthAudFromUrl } from 'lib/strings/url-helpers '
11
+ import { useSession } from '#/state/session'
12
+ import { useServiceAuthToken , useVideoUploadLimits } from './video-upload.shared '
13
13
14
14
export const useUploadVideoMutation = ( {
15
15
onSuccess,
@@ -23,37 +23,30 @@ export const useUploadVideoMutation = ({
23
23
signal : AbortSignal
24
24
} ) => {
25
25
const { currentAccount} = useSession ( )
26
- const agent = useAgent ( )
26
+ const getToken = useServiceAuthToken ( {
27
+ lxm : 'com.atproto.repo.uploadBlob' ,
28
+ exp : Date . now ( ) / 1000 + 60 * 30 , // 30 minutes
29
+ } )
30
+ const checkLimits = useVideoUploadLimits ( )
27
31
const { _} = useLingui ( )
28
32
29
33
return useMutation ( {
30
34
mutationKey : [ 'video' , 'upload' ] ,
31
35
mutationFn : cancelable ( async ( video : CompressedVideo ) => {
36
+ await checkLimits ( )
37
+
32
38
const uri = createVideoEndpointUrl ( '/xrpc/app.bsky.video.uploadVideo' , {
33
39
did : currentAccount ! . did ,
34
40
name : `${ nanoid ( 12 ) } .${ mimeToExt ( video . mimeType ) } ` ,
35
41
} )
36
42
37
- const serviceAuthAud = getServiceAuthAudFromUrl ( agent . dispatchUrl )
38
-
39
- if ( ! serviceAuthAud ) {
40
- throw new Error ( 'Agent does not have a PDS URL' )
41
- }
42
-
43
- const { data : serviceAuth } = await agent . com . atproto . server . getServiceAuth (
44
- {
45
- aud : serviceAuthAud ,
46
- lxm : 'com.atproto.repo.uploadBlob' ,
47
- exp : Date . now ( ) / 1000 + 60 * 30 , // 30 minutes
48
- } ,
49
- )
50
-
51
43
let bytes = video . bytes
52
-
53
44
if ( ! bytes ) {
54
45
bytes = await fetch ( video . uri ) . then ( res => res . arrayBuffer ( ) )
55
46
}
56
47
48
+ const token = await getToken ( )
49
+
57
50
const xhr = new XMLHttpRequest ( )
58
51
const res = await new Promise < AppBskyVideoDefs . JobStatus > (
59
52
( resolve , reject ) => {
@@ -76,7 +69,7 @@ export const useUploadVideoMutation = ({
76
69
}
77
70
xhr . open ( 'POST' , uri )
78
71
xhr . setRequestHeader ( 'Content-Type' , video . mimeType )
79
- xhr . setRequestHeader ( 'Authorization' , `Bearer ${ serviceAuth . token } ` )
72
+ xhr . setRequestHeader ( 'Authorization' , `Bearer ${ token } ` )
80
73
xhr . send ( bytes )
81
74
} ,
82
75
)
0 commit comments