forked from react-native-documents/document-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (121 loc) · 3.5 KB
/
index.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
import { Platform, NativeModules } from 'react-native';
const { RNDocumentPicker } = NativeModules;
if (!RNDocumentPicker) {
// Use a timeout to ensure the warning is displayed in the YellowBox
setTimeout(() => {
console.warn(
'RNDocumentPicker: Native module is not available, make sure you have finished the installation process and rebuilt your app'
);
}, 0);
} else if (!RNDocumentPicker.pick && RNDocumentPicker.show) {
// Use a timeout to ensure the warning is displayed in the YellowBox
setTimeout(() => {
console.warn(
'RNDocumentPicker: Native module is obsolete, you may not have rebuilt your app after upgrading the library'
);
}, 0);
}
const E_DOCUMENT_PICKER_CANCELED = 'DOCUMENT_PICKER_CANCELED';
function pick(opts) {
if ('filetype' in opts) {
throw new TypeError(
'A `filetype` option was passed to DocumentPicker.pick, the correct option is `type`'
);
}
if ('types' in opts) {
throw new TypeError(
'A `types` option was passed to DocumentPicker.pick, the correct option is `type`'
);
}
if (!('type' in opts)) {
opts.type = DocumentPicker.types.allFiles;
}
opts.type = Array.isArray(opts.type) ? opts.type : [opts.type];
if (opts.type.some(type => type === undefined)) {
throw new TypeError(
'Unexpected undefined type option, did you try using a DocumentPicker.types.* that does not exist?'
);
}
if (Array.isArray(opts.type) && opts.type.length < 1) {
throw new TypeError(
'`type` option should not be an empty array, at least one type must be passed if the `type` option is not omitted'
);
}
opts.type.forEach(type => {
if (typeof type !== 'string') {
throw new TypeError(
'Invalid type option, expected a string not: ' + type
);
}
});
if (
opts.type.length > 1 &&
Platform.OS === 'android' &&
Platform.Version < 19
) {
console.warn(
`RNDocumentPicker: Android API level ${
Platform.Version
} does not support multiple types, falling back to */*`
);
}
return RNDocumentPicker.pick(opts);
}
const Types = {
mimeTypes: {
allFiles: '*/*',
audio: 'audio/*',
images: 'image/*',
plainText: 'text/plain',
pdf: 'application/pdf',
video: 'video/*',
},
utis: {
allFiles: 'public.content',
audio: 'public.audio',
images: 'public.image',
plainText: 'public.plain-text',
pdf: 'com.adobe.pdf',
video: 'public.movie',
},
extensions: {
allFiles: '*',
audio:
'.3g2 .3gp .aac .adt .adts .aif .aifc .aiff .asf .au .m3u .m4a .m4b .mid .midi .mp2 .mp3 .mp4 .rmi .snd .wav .wax .wma',
images: '.jpeg .jpg .png',
plainText: '.txt',
pdf: '.pdf',
video: '.mp4',
},
};
const PlatformTypes = {
android: Types.mimeTypes,
ios: Types.utis,
windows: Types.extensions,
};
export default class DocumentPicker {
/**
* Android requires mime types, iOS is a bit more complicated:
*
* @see https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html
*/
static types = PlatformTypes[Platform.OS] || Types.mimeTypes;
static pick(opts) {
const options = {
...opts,
multiple: false,
};
return pick(options).then(results => results[0]);
}
static pickMultiple(opts) {
const options = {
...opts,
multiple: true,
};
return pick(options);
}
static isCancel(err) {
return err && err.code === E_DOCUMENT_PICKER_CANCELED;
}
}