@@ -201,38 +201,42 @@ const useWhiteboardFilesManager = ({
201
201
}
202
202
203
203
const files = whiteboard . files ;
204
+ // leave only the incoming files that don't have a dataURL and are not in the fileStore
205
+ const pendingFileIds = Object . keys ( files ) . filter ( fileId => ! files [ fileId ] ?. dataURL && ! fileStore . current [ fileId ] ) ;
206
+
207
+ if ( ! pendingFileIds . length ) {
208
+ return ;
209
+ }
204
210
205
- const pendingFileIds = Object . keys ( files ) . filter ( fileId => ! files [ fileId ] ?. dataURL ) ;
206
211
log ( 'I need to download these files' , pendingFileIds ) ;
207
- const newFiles : BinaryFilesWithUrl = { } ;
208
212
209
213
setDownloadingFiles ( true ) ;
210
214
211
- try {
212
- await Promise . all (
213
- pendingFileIds . map ( async fileId => {
214
- const file = whiteboard ! . files ! [ fileId ] ;
215
- if ( fileStore . current [ fileId ] ?. dataURL ) {
216
- log ( `No need to download ${ fileId } already in the store` , fileStore . current [ fileId ] ) ;
217
- return ;
218
- }
219
- if ( file . url ) {
220
- log ( 'DOWNLOADING ' , file ) ;
221
- try {
222
- const dataURL = await fetchFileToDataURL ( file . url ) ;
223
- newFiles [ fileId ] = { ... file , dataURL } as BinaryFileDataWithUrl ;
224
- fileStoreAddFile ( fileId , newFiles [ fileId ] ) ;
225
- } catch ( e ) {
226
- error ( `Error downloading file: ${ file . url } ` , { label : 'whiteboard-file-manager' } ) ;
227
- }
228
- } else {
229
- error ( `Cannot download : ${ file . id } ` , { label : 'whiteboard-file-manager' } ) ;
230
- }
231
- } )
232
- ) ;
233
- } finally {
234
- setDownloadingFiles ( false ) ;
235
- }
215
+ await Promise . allSettled (
216
+ pendingFileIds . map ( async fileId => {
217
+ if ( fileStore . current [ fileId ] ?. dataURL ) {
218
+ log ( `No need to download ${ fileId } already in the store` , fileStore . current [ fileId ] ) ;
219
+ return ;
220
+ }
221
+ const file = whiteboard ! . files ! [ fileId ] ;
222
+ if ( ! file . url ) {
223
+ error ( `Cannot download: ${ file . id } ` , { label : 'whiteboard-file-manager' } ) ;
224
+ throw new Error ( `Cannot download: ${ file . id } ` ) ;
225
+ }
226
+
227
+ log ( 'DOWNLOADING ' , file ) ;
228
+ try {
229
+ const dataURL = await fetchFileToDataURL ( file . url ) ;
230
+ // try-catch will avoid putting the file in the store if fetching fails
231
+ fileStoreAddFile ( fileId , { ... file , dataURL } as BinaryFileDataWithUrl ) ;
232
+ } catch ( e ) {
233
+ error ( `Error downloading file : ${ file . url } ` , { label : 'whiteboard-file-manager' } ) ;
234
+ throw e ;
235
+ }
236
+ } )
237
+ ) ;
238
+
239
+ setDownloadingFiles ( false ) ;
236
240
} ;
237
241
238
242
/**
0 commit comments