-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathpickDocument.ts
More file actions
59 lines (53 loc) · 1.54 KB
/
pickDocument.ts
File metadata and controls
59 lines (53 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* Types are approximated from what we need from the DocumentPicker API.
*
* For its full API, see https://github.com/react-native-documents/document-picker/blob/main/packages/document-picker/src/index.ts
* */
type ResponseValue = {
name: string;
size: number;
type: string;
uri: string;
};
type DocumentPickerType =
| {
pick: (opts?: { allowMultiSelection: boolean; type: string[] }) => Promise<ResponseValue[]>;
types: { allFiles: string };
}
| undefined;
let DocumentPicker: DocumentPickerType;
try {
DocumentPicker = require('@react-native-documents/picker');
} catch (err) {
// do nothing
console.log(
'The @react-native-documents/picker is not installed. Installing it will enable you to pick and upload files from within your app.',
);
}
export const pickDocument = DocumentPicker
? async ({ maxNumberOfFiles }: { maxNumberOfFiles: number }) => {
try {
if (!DocumentPicker) return { cancelled: true };
let res: ResponseValue[] = await DocumentPicker.pick({
allowMultiSelection: true,
type: [DocumentPicker.types.allFiles],
});
if (maxNumberOfFiles && res.length > maxNumberOfFiles) {
res = res.slice(0, maxNumberOfFiles);
}
return {
assets: res.map(({ name, size, type, uri }) => ({
mimeType: type,
name,
size,
uri,
})),
cancelled: false,
};
} catch (err) {
return {
cancelled: true,
};
}
}
: null;