@@ -88,8 +88,8 @@ function toFilePromises(item: DataTransferItem) {
88
88
// Safari supports dropping an image node from a different window and can be retrieved using
89
89
// the DataTransferItem.getAsFile() API
90
90
// NOTE: FileSystemEntry.file() throws if trying to get the file
91
- if ( entry && entry . isDirectory ) {
92
- return fromDirEntry ( entry ) as any ;
91
+ if ( entry && isDirectoryEntry ( entry ) ) {
92
+ return fromDirectoryEntry ( entry ) ;
93
93
}
94
94
95
95
return fromDataTransferItem ( item , entry ) ;
@@ -124,64 +124,62 @@ function fromDataTransferItem(
124
124
return Promise . resolve ( fwp ) ;
125
125
}
126
126
127
- // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemEntry
128
- async function fromEntry ( entry : any ) {
129
- return entry . isDirectory ? fromDirEntry ( entry ) : fromFileEntry ( entry ) ;
127
+ async function fromEntry (
128
+ entry : FileSystemEntry ,
129
+ ) : Promise < FileWithPath | FileArray [ ] > {
130
+ if ( isDirectoryEntry ( entry ) ) {
131
+ return fromDirectoryEntry ( entry ) ;
132
+ } else if ( isFileEntry ( entry ) ) {
133
+ return fromFileEntry ( entry ) ;
134
+ }
135
+
136
+ return [ ] ;
130
137
}
131
138
132
- // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry
133
- function fromDirEntry ( entry : any ) {
139
+ async function fromDirectoryEntry (
140
+ entry : FileSystemDirectoryEntry ,
141
+ ) : Promise < FileArray [ ] > {
134
142
const reader = entry . createReader ( ) ;
143
+ const entries : Promise < FileValue [ ] > [ ] = [ ] ;
135
144
136
- return new Promise < FileArray [ ] > ( ( resolve , reject ) => {
137
- const entries : Promise < FileValue [ ] > [ ] = [ ] ;
138
-
139
- function readEntries ( ) {
140
- // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry/createReader
141
- // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryReader/readEntries
142
- reader . readEntries (
143
- async ( batch : any [ ] ) => {
144
- if ( ! batch . length ) {
145
- // Done reading directory
146
- try {
147
- const files = await Promise . all ( entries ) ;
148
- resolve ( files ) ;
149
- } catch ( err ) {
150
- reject ( err ) ;
151
- }
152
- } else {
153
- const items = Promise . all ( batch . map ( fromEntry ) ) ;
154
- entries . push ( items ) ;
155
-
156
- // Continue reading
157
- readEntries ( ) ;
158
- }
159
- } ,
160
- ( err : any ) => {
161
- reject ( err ) ;
162
- } ,
163
- ) ;
145
+ while ( true ) {
146
+ const batch = await readEntries ( reader ) ;
147
+
148
+ if ( ! batch . length ) {
149
+ break ;
164
150
}
165
151
166
- readEntries ( ) ;
167
- } ) ;
152
+ entries . push ( Promise . all ( batch . map ( fromEntry ) ) ) ;
153
+ }
154
+
155
+ const files = await Promise . all ( entries ) ;
156
+ return files ;
168
157
}
169
158
170
- // https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry
171
- async function fromFileEntry ( entry : any ) {
172
- return new Promise < FileWithPath > ( ( resolve , reject ) => {
173
- entry . file (
174
- ( file : FileWithPath ) => {
175
- const fwp = toFileWithPath ( file , entry . fullPath ) ;
176
- resolve ( fwp ) ;
177
- } ,
178
- ( err : any ) => {
179
- reject ( err ) ;
180
- } ,
181
- ) ;
182
- } ) ;
159
+ async function fromFileEntry (
160
+ entry : FileSystemFileEntry ,
161
+ ) : Promise < FileWithPath > {
162
+ const file = await getFile ( entry ) ;
163
+ const fileWithPath = toFileWithPath ( file , entry . fullPath ) ;
164
+
165
+ return fileWithPath ;
183
166
}
184
167
168
+ const isDirectoryEntry = (
169
+ entry : FileSystemEntry ,
170
+ ) : entry is FileSystemDirectoryEntry => entry . isDirectory ;
171
+
172
+ const isFileEntry = ( entry : FileSystemEntry ) : entry is FileSystemFileEntry =>
173
+ entry . isFile ;
174
+
175
+ const getFile = ( entry : FileSystemFileEntry ) : Promise < File > =>
176
+ new Promise ( ( resolve , reject ) => entry . file ( resolve , reject ) ) ;
177
+
178
+ const readEntries = (
179
+ reader : FileSystemDirectoryReader ,
180
+ ) : Promise < FileSystemEntry [ ] > =>
181
+ new Promise ( ( resolve , reject ) => reader . readEntries ( resolve , reject ) ) ;
182
+
185
183
// Infinite type recursion
186
184
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
187
185
interface FileArray extends Array < FileValue > { }
0 commit comments