forked from ajith-ab/react-native-receive-sharing-intent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
118 lines (103 loc) · 3.47 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
import { NativeModules, Platform, Linking, AppState } from 'react-native';
import MimeTypes from "./mimeType";
const { ReceiveSharingIntent } = NativeModules;
const isIos = Platform.OS === 'ios';
export default class ReceiveSharingIntentModule {
static getReceivedFiles = (handler, errorHandler, protocol = 'ShareMedia') => {
if (isIos) {
Linking.getInitialURL().then(res => {
if (res && res.startsWith(`${protocol}://dataUrl`)) {
this.getFileNames(handler, errorHandler, res);
}
}).catch(e => { });
Linking.addEventListener("url", (res) => {
console.log(res);
let url = res ? res.url : "";
if (url.startsWith(`${protocol}://dataUrl`)) {
this.getFileNames(handler,errorHandler, res.url);
}
});
}else{
AppState.addEventListener('change', (status) => {
if (status === 'active') {
this.getFileNames(handler,errorHandler, null);
}
});
this.getFileNames(handler,errorHandler, null);
}
}
static getFileNames = (handler,errorHandler, url) => {
if(isIos){
ReceiveSharingIntent.getFileNames(url).then(data=>{
let files = iosSortedData(data);
handler(files);
}).catch(e=>errorHandler(e));
}else{
ReceiveSharingIntent.getFileNames().then(fileObject => {
let files = Object.keys(fileObject).map((k) => fileObject[k])
handler(files);
}).catch(e=>{
errorHandler(e)
})
}
}
static clearReceivedFiles = () => {
ReceiveSharingIntent.clearFileNames();
}
}
const iosSortedData = (data) => {
let objects = { filePath: null, text: null, weblink: null, mimeType: null, contentUri: null, fileName: null, extension: null };
let file = data;
if (file.startsWith('text:')) {
let text = file.replace("text:", "");
if (text.startsWith("http")) {
let object = [
{ ...objects, weblink: text }
];
return object;
}
let object = [
{ ...objects, text: text }
];
return object;
} else if (file.startsWith('webUrl:')) {
let weblink = file.replace("webUrl:", "");
let object = [
{ ...objects, weblink: weblink }
];
return object;
} else {
try {
let files = JSON.parse(file)
let object = [];
for (let i = 0; i < files.length; i++) {
let path = files[i].path;
let obj = {
...objects,
fileName: getFileName(path),
extension: getExtension(path),
mimeType: getMimeType(path),
filePath: path
}
object.push(obj);
}
return object;
} catch (error) {
return [{ ...objects }];
}
}
}
var getFileName = (file) => {
return file.replace(/^.*(\\|\/|\:)/, '');
}
var getExtension = (fileName) => {
return fileName.substr(fileName.lastIndexOf('.') + 1);
}
var getMimeType = (file) => {
let ext = getExtension(file);
let extension = "." + ext.toLowerCase();
if (MimeTypes[extension]) {
return MimeTypes[extension];
}
return null
}