1
+ import type * as ExpoDocumentPicker from 'expo-document-picker' ;
1
2
import type * as ExpoFs from 'expo-file-system' ;
2
3
import type * as ExpoImagePicker from 'expo-image-picker' ;
3
4
4
5
import type { FilePickerResponse } from '../platform/types' ;
5
6
import normalizeFile from './normalizeFile' ;
6
7
7
8
const expoBackwardUtils = {
8
- toCanceled ( result : ExpoImagePicker . ImagePickerResult ) {
9
- // @ts -expect-error backward compatibility
10
- return result . canceled ?? result . cancelled ;
9
+ imagePicker : {
10
+ isCanceled ( result : ExpoImagePicker . ImagePickerResult ) {
11
+ // @ts -expect-error backward compatibility
12
+ return result . canceled ?? result . cancelled ;
13
+ } ,
14
+ async toFilePickerResponses (
15
+ result : ExpoImagePicker . ImagePickerResult ,
16
+ fsModule : typeof ExpoFs ,
17
+ ) : Promise < FilePickerResponse [ ] > {
18
+ if ( result . assets ) {
19
+ const assets = result . assets || [ ] ;
20
+ const promises = assets . map ( ( { fileName : name , fileSize : size , type, uri } ) =>
21
+ normalizeFile ( { uri, size, name, type } ) ,
22
+ ) ;
23
+
24
+ return Promise . all ( promises ) ;
25
+ } else if ( 'uri' in result && typeof result . uri === 'string' ) {
26
+ const fileInfo = await fsModule . getInfoAsync ( result . uri ) ;
27
+ const response = await normalizeFile ( { uri : result . uri , size : expoBackwardUtils . toFileSize ( fileInfo ) } ) ;
28
+ return [ response ] ;
29
+ } else {
30
+ return [ ] ;
31
+ }
32
+ } ,
33
+ } ,
34
+ documentPicker : {
35
+ isCanceled ( result : ExpoDocumentPicker . DocumentPickerResult ) {
36
+ // @ts -expect-error backward compatibility
37
+ return result . canceled ?? result . type === 'cancel' ;
38
+ } ,
39
+ async toFilePickerResponses ( result : ExpoDocumentPicker . DocumentPickerResult ) : Promise < FilePickerResponse [ ] > {
40
+ if ( result . assets ) {
41
+ const assets = result . assets || [ ] ;
42
+ const promises = assets . map ( ( { name, size, mimeType, uri } ) =>
43
+ normalizeFile ( { uri, size, name, type : mimeType } ) ,
44
+ ) ;
45
+
46
+ return Promise . all ( promises ) ;
47
+ } else if ( 'uri' in result && typeof result . uri === 'string' ) {
48
+ // @ts -expect-error backward compatibility
49
+ const { mimeType, uri, size, name } = result ;
50
+ const response = await normalizeFile ( { uri, size, name, type : mimeType } ) ;
51
+
52
+ return [ response ] ;
53
+ } else {
54
+ return [ ] ;
55
+ }
56
+ } ,
11
57
} ,
12
58
toFileSize ( info : ExpoFs . FileInfo ) {
13
59
if ( 'size' in info ) {
@@ -16,25 +62,6 @@ const expoBackwardUtils = {
16
62
return 0 ;
17
63
}
18
64
} ,
19
- async toFilePickerResponses (
20
- result : ExpoImagePicker . ImagePickerResult ,
21
- fsModule : typeof ExpoFs ,
22
- ) : Promise < FilePickerResponse [ ] > {
23
- if ( result . assets ) {
24
- const assets = result . assets || [ ] ;
25
- const promises = assets . map ( ( { fileName : name , fileSize : size , type, uri } ) =>
26
- normalizeFile ( { uri, size, name, type } ) ,
27
- ) ;
28
-
29
- return Promise . all ( promises ) ;
30
- } else if ( 'uri' in result && typeof result . uri === 'string' ) {
31
- const fileInfo = await fsModule . getInfoAsync ( result . uri ) ;
32
- const response = await normalizeFile ( { uri : result . uri , size : this . toFileSize ( fileInfo ) } ) ;
33
- return [ response ] ;
34
- } else {
35
- return [ ] ;
36
- }
37
- } ,
38
65
} ;
39
66
40
67
export default expoBackwardUtils ;
0 commit comments