1- import { Dispatch , SetStateAction , useState } from " react" ;
2- import { useMutation , UseMutationOptions } from " @tanstack/react-query" ;
1+ import { Dispatch , SetStateAction , useState } from ' react' ;
2+ import { useMutation , UseMutationOptions } from ' @tanstack/react-query' ;
33
4- import { SerializedException } from " @/pythonTypes" ;
4+ import { SerializedException } from ' @/pythonTypes' ;
55
6- import { APIError } from " ./common" ;
6+ import { APIError } from ' ./common' ;
77
88export interface FileUploadProgress {
99 name : string ;
@@ -34,8 +34,13 @@ export const fileUploadMutationOptions: UseMutationOptions<
3434> = {
3535 mutationFn : async ( { files, targetDir, setProgress } ) => {
3636 let uploadedBytesTotal = 0 ;
37- const totalBytes = Array . from ( files ) . reduce ( ( acc , file ) => acc + file . size , 0 ) ;
38- console . log ( `Uploading ${ files . length } files, total size ${ totalBytes } bytes` ) ;
37+ const totalBytes = Array . from ( files ) . reduce (
38+ ( acc , file ) => acc + file . size ,
39+ 0
40+ ) ;
41+ console . log (
42+ `Uploading ${ files . length } files, total size ${ totalBytes } bytes`
43+ ) ;
3944
4045 // init progress bars for each file
4146 // Note that you cannot use an object for the batch progress, because
@@ -98,7 +103,9 @@ export const fileUploadMutationOptions: UseMutationOptions<
98103 loaded : uploadedBytesTotal ,
99104 } ;
100105 } ) ;
101- console . debug ( `Uploaded file ${ i + 1 } /${ files . length } : ${ file . name } ` ) ;
106+ console . debug (
107+ `Uploaded file ${ i + 1 } /${ files . length } : ${ file . name } `
108+ ) ;
102109 }
103110
104111 const finished = performance . now ( ) ;
@@ -115,7 +122,7 @@ export const fileUploadMutationOptions: UseMutationOptions<
115122 ( finished - started ) / 1000
116123 } seconds`
117124 ) ;
118- return { status : "ok" } ;
125+ return { status : 'ok' } ;
119126 } ,
120127} ;
121128
@@ -131,20 +138,23 @@ async function uploadFile(
131138) : Promise < { status : string } > {
132139 // Validate headers (filename and target dir) before uploading
133140 // Raises if invalid
134- await fetch ( " /file_upload/validate" , {
135- method : " POST" ,
141+ await fetch ( ' /file_upload/validate' , {
142+ method : ' POST' ,
136143 headers : {
137- " X-Filename" : encodeURIComponent ( file . name ) ,
138- " X-File-Target-Dir" : targetDir ,
144+ ' X-Filename' : encodeURIComponent ( file . name ) ,
145+ ' X-File-Target-Dir' : targetDir ,
139146 } ,
140147 } ) ;
141148
142149 return new Promise < { status : string } > ( ( resolve , reject ) => {
143150 const req = new XMLHttpRequest ( ) ;
144- req . responseType = "json" ;
145- req . open ( "POST" , "/api_v1/file_upload" , true ) ;
146- req . setRequestHeader ( "X-Filename" , encodeURIComponent ( file . name ) ) ;
147- req . setRequestHeader ( "X-File-Target-Dir" , encodeURIComponent ( targetDir ) ) ;
151+ req . responseType = 'json' ;
152+ req . open ( 'POST' , '/api_v1/file_upload' , true ) ;
153+ req . setRequestHeader ( 'X-Filename' , encodeURIComponent ( file . name ) ) ;
154+ req . setRequestHeader (
155+ 'X-File-Target-Dir' ,
156+ encodeURIComponent ( targetDir )
157+ ) ;
148158 // req.setRequestHeader("Content-Length", String(file.size));
149159
150160 req . upload . onprogress = ( event ) => {
@@ -157,18 +167,18 @@ async function uploadFile(
157167 if ( req . status >= 200 && req . status < 300 ) {
158168 // onprogress is not called automatically when finally done
159169 // onProgress?.({ total: file.size, loaded: file.size });
160- console . log ( " File upload resolve" ) ;
161- resolve ( { status : "ok" } ) ;
170+ console . log ( ' File upload resolve' ) ;
171+ resolve ( { status : 'ok' } ) ;
162172 } else {
163173 const json_error = req . response as SerializedException ;
164- console . error ( " File upload error:" , json_error ) ;
174+ console . error ( ' File upload error:' , json_error ) ;
165175 reject ( new APIError ( json_error , req . status ) ) ;
166176 }
167177 } ;
168178
169179 req . onerror = ( ) => {
170180 const json_error = req . response as SerializedException ;
171- console . error ( " File upload error:" , req ) ;
181+ console . error ( ' File upload error:' , req ) ;
172182 reject ( new APIError ( json_error , req . status ) ) ;
173183 } ;
174184 req . send ( file ) ;
0 commit comments