77 SafeAreaView ,
88 ScrollView ,
99 StyleSheet ,
10+ Switch ,
1011 Text ,
1112 TextInput ,
1213 View ,
@@ -22,6 +23,7 @@ import type { Video } from 'src/types';
2223export default function App ( ) {
2324 const [ videoFile , setVideoFile ] = React . useState < string | null > ( null ) ;
2425 const [ uploading , setUploading ] = React . useState < boolean > ( false ) ;
26+ const [ isProgressive , setIsProgressive ] = React . useState < boolean > ( false ) ;
2527 const [ uploadToken , setUploadToken ] = React . useState < string > ( '' ) ;
2628 const [ chunkSize , setChunkSize ] = React . useState < string > ( '20' ) ;
2729 const [ uploadResult , setUploadResult ] = React . useState < any | null > ( null ) ;
@@ -58,7 +60,12 @@ export default function App() {
5860 } , [ ] ) ;
5961
6062 const onUploadButtonPress = React . useCallback (
61- ( token : string , uri : string | null , chunkSize : string ) => {
63+ (
64+ token : string ,
65+ uri : string | null ,
66+ chunkSize : string ,
67+ isProgressive : boolean
68+ ) => {
6269 const chunkSizeInt = parseInt ( chunkSize ) ;
6370
6471 if ( ! uri ) {
@@ -80,19 +87,46 @@ export default function App() {
8087 const resolveUri = ( u : string ) : Promise < string > => {
8188 return Platform . OS === 'android'
8289 ? ReactNativeBlobUtil . fs . stat ( u ) . then ( ( stat ) => stat . path )
83- : new Promise ( ( resolve , _ ) => resolve ( u ) ) ;
90+ : new Promise ( ( resolve , _ ) => resolve ( u . replace ( 'file://' , '' ) ) ) ;
8491 } ;
8592
86- resolveUri ( uri ) . then ( ( u ) => {
87- ApiVideoUploader . uploadWithUploadToken ( token , u )
88- . then ( ( value : Video ) => {
89- setUploadResult ( value ) ;
90- setUploading ( false ) ;
91- } )
92- . catch ( ( e : any ) => {
93- Alert . alert ( 'Upload failed' , e ?. message || JSON . stringify ( e ) ) ;
94- setUploading ( false ) ;
93+ resolveUri ( uri ) . then ( async ( u ) => {
94+ if ( isProgressive ) {
95+ const size = ( await ReactNativeBlobUtil . fs . stat ( u ) ) . size ;
96+
97+ const session = ApiVideoUploader . createProgressiveUploadSession ( {
98+ token,
9599 } ) ;
100+ const chunkSizeBytes = 1024 * 1024 * chunkSizeInt ;
101+ let start = 0 ;
102+ for (
103+ let i = 0 ;
104+ start <= size - chunkSizeBytes ;
105+ start += chunkSizeBytes , i ++
106+ ) {
107+ await ReactNativeBlobUtil . fs . slice (
108+ u ,
109+ `${ u } .part${ i } ` ,
110+ start ,
111+ start + chunkSizeBytes
112+ ) ;
113+ await session . uploadPart ( `${ u } .part${ i } ` ) ;
114+ }
115+ await ReactNativeBlobUtil . fs . slice ( u , `${ u } .lastpart` , start , size ) ;
116+ const value = await session . uploadLastPart ( u + '.lastpart' ) ;
117+ setUploadResult ( value ) ;
118+ setUploading ( false ) ;
119+ } else {
120+ ApiVideoUploader . uploadWithUploadToken ( token , u )
121+ . then ( ( value : Video ) => {
122+ setUploadResult ( value ) ;
123+ setUploading ( false ) ;
124+ } )
125+ . catch ( ( e : any ) => {
126+ Alert . alert ( 'Upload failed' , e ?. message || JSON . stringify ( e ) ) ;
127+ setUploading ( false ) ;
128+ } ) ;
129+ }
96130 } ) ;
97131 } ,
98132 [ ]
@@ -165,11 +199,35 @@ export default function App() {
165199 keyboardType = "numeric"
166200 />
167201 </ View >
202+ < View
203+ style = { {
204+ borderLeftWidth : 1 ,
205+ marginLeft : 8 ,
206+ borderLeftColor : '#F64325' ,
207+ marginVertical : 5 ,
208+ alignItems : 'flex-start' ,
209+ } }
210+ >
211+ < Text style = { styles . label } > Progressive upload</ Text >
212+ < Switch
213+ disabled = { uploading }
214+ trackColor = { { false : '#767577' , true : '#767577' } }
215+ thumbColor = { isProgressive ? '#F64325' : '#f4f3f4' }
216+ ios_backgroundColor = "#3e3e3e"
217+ onValueChange = { ( ) => setIsProgressive ( ! isProgressive ) }
218+ value = { isProgressive }
219+ />
220+ </ View >
168221 < Text style = { styles . textSectionTitle } > And finally... upload!</ Text >
169222 < DemoButton
170223 disabled = { uploading }
171224 onPress = { ( ) =>
172- onUploadButtonPress ( uploadToken , videoFile , chunkSize )
225+ onUploadButtonPress (
226+ uploadToken ,
227+ videoFile ,
228+ chunkSize ,
229+ isProgressive
230+ )
173231 }
174232 >
175233 { uploading ? 'UPLOADING...' : 'UPLOAD' }
0 commit comments