forked from react-native-documents/document-picker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (54 loc) · 1.66 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
import { NativeModules, Platform } from 'react-native';
const resolveAssetSource = require('react-native/Libraries/Image/resolveAssetSource');
const callbacks = {};
const DocumentPicker = {
show: (options, callback) => {
if (options.customActions) {
options.customActions = options.customActions.map(item => {
callbacks[item.key] = item.action;
let image = null;
if (item.image) {
image = resolveAssetSource(item.image);
}
return {
...item,
isCustomAction: true,
image
};
});
}
NativeModules.RNDocumentPicker.show(options, (err, res) => {
if (res && res.isCustomAction) {
callbacks[res.key](res, newRes => callback(null, newRes));
} else {
callback(err, res);
}
});
}
};
/**
* 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
*/
class DocumentPickerUtil {
static allFiles() {
return Platform.OS === 'android' ? '*/*' : 'public.content';
}
static images() {
return Platform.OS === 'android' ? 'image/*' : 'public.image';
}
static plainText() {
return Platform.OS === 'android' ? 'text/plain' : 'public.plain-text';
}
static audio() {
return Platform.OS === 'android' ? 'audio/*' : 'public.audio';
}
static video() {
return Platform.OS === 'android' ? 'video/*' : 'public.video';
}
static pdf() {
return Platform.OS === 'android' ? 'application/pdf' : 'com.adobe.pdf';
}
}
module.exports = { DocumentPickerUtil, DocumentPicker };