@@ -43,65 +43,48 @@ class DownloadAbortError extends Error {
4343let downloadCanceled
4444
4545/**
46- * Recursive download for file trees via browser file access API
46+ * Download for file trees via browser file access API
4747 */
4848const downloadTree = async (
4949 { datasetId, snapshotTag, client, dirHandle, toastId } ,
5050 path = "" ,
51- tree = null ,
5251) => {
5352 const filesToDownload = await downloadDataset ( client ) ( {
5453 datasetId,
5554 snapshotTag,
56- tree,
5755 } )
5856 for ( const [ _index , file ] of filesToDownload . entries ( ) ) {
5957 const downloadPath = path ? `${ path } /${ file . filename } ` : file . filename
60- if ( file . directory ) {
61- // Next tree level
62- await downloadTree (
63- {
58+ // Regular file
59+ if ( downloadCanceled ) {
60+ throw new DownloadAbortError ( "Download canceled by user request" )
61+ }
62+ const fileHandle = await openFileTree (
63+ dirHandle ,
64+ path ? `${ path } /${ file . filename } ` : file . filename ,
65+ )
66+ // Skip files which are already complete
67+ if ( fileHandle . size == file . size ) continue
68+ const writable = await fileHandle . createWritable ( )
69+ const { body, status, statusText } = await fetch ( file . urls [ 0 ] )
70+ let loaded = 0
71+ const progress = new TransformStream ( {
72+ transform ( chunk , controller ) {
73+ downloadToastUpdate ( toastId , loaded / file . size , {
6474 datasetId,
6575 snapshotTag,
66- client,
67- dirHandle,
68- toastId,
69- } ,
70- downloadPath ,
71- file . key ,
72- )
76+ downloadPath,
77+ dirName : dirHandle . name ,
78+ } )
79+ loaded += chunk . length
80+ controller . enqueue ( chunk )
81+ } ,
82+ } )
83+ if ( status === 200 ) {
84+ await body . pipeThrough ( progress ) . pipeTo ( writable )
7385 } else {
74- // Regular file
75- if ( downloadCanceled ) {
76- throw new DownloadAbortError ( "Download canceled by user request" )
77- }
78- const fileHandle = await openFileTree (
79- dirHandle ,
80- path ? `${ path } /${ file . filename } ` : file . filename ,
81- )
82- // Skip files which are already complete
83- if ( fileHandle . size == file . size ) continue
84- const writable = await fileHandle . createWritable ( )
85- const { body, status, statusText } = await fetch ( file . urls [ 0 ] )
86- let loaded = 0
87- const progress = new TransformStream ( {
88- transform ( chunk , controller ) {
89- downloadToastUpdate ( toastId , loaded / file . size , {
90- datasetId,
91- snapshotTag,
92- downloadPath,
93- dirName : dirHandle . name ,
94- } )
95- loaded += chunk . length
96- controller . enqueue ( chunk )
97- } ,
98- } )
99- if ( status === 200 ) {
100- await body . pipeThrough ( progress ) . pipeTo ( writable )
101- } else {
102- Sentry . captureException ( statusText )
103- return requestFailureToast ( file . filename )
104- }
86+ Sentry . captureException ( statusText )
87+ return requestFailureToast ( file . filename )
10588 }
10689 }
10790}
0 commit comments