forked from mybigday/react-native-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.android.js
More file actions
113 lines (94 loc) · 2.38 KB
/
index.android.js
File metadata and controls
113 lines (94 loc) · 2.38 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
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
import { NativeModules, DeviceEventEmitter } from "react-native";
const { RNS3TransferUtility } = NativeModules;
const transferTypes = ["upload", "download"];
const defaultOptions = {
region: "eu-west-1"
};
const subscribeCallbacks = {}; // [id]: function
DeviceEventEmitter.addListener("@_RNS3_Events", event => {
const { task, error } = event;
if (subscribeCallbacks[task.id]) {
subscribeCallbacks[task.id](error, task);
}
});
class TransferUtility {
async setupWithNative() {
const result = await RNS3TransferUtility.setupWithNative();
if (result) {
RNS3TransferUtility.initializeRNS3();
}
return result;
}
async setupWithBasic(options = {}) {
if (!options.access_key || !options.secret_key) {
return false;
}
if (!options.session_token) {
options.session_token = null;
}
const result = await RNS3TransferUtility.setupWithBasic({ ...defaultOptions, ...options });
if (result) {
RNS3TransferUtility.initializeRNS3();
}
return result;
}
async setupWithCognito(options = {}) {
if (!options.identity_pool_id) {
return false;
}
if (!options.caching) {
options.caching = false;
}
const result = await RNS3TransferUtility.setupWithCognito({ ...defaultOptions, ...options });
if (result) {
RNS3TransferUtility.initializeRNS3();
}
return result;
}
async upload(options = {}) {
if (!options.meta) {
options.meta = {};
}
const task = await RNS3TransferUtility.upload(options);
return task;
}
async download(options = {}) {
const task = await RNS3TransferUtility.download(options);
return task;
}
pause(id) {
RNS3TransferUtility.pause(id);
}
resume(id) {
RNS3TransferUtility.resume(id);
}
cancel(id) {
RNS3TransferUtility.cancel(id);
}
async deleteRecord(id) {
return RNS3TransferUtility.deleteRecord(id);
}
async getTask(id) {
return RNS3TransferUtility.getTask(id);
}
// idAsKey: return Object with id as key
async getTasks(type = "", idAsKey) {
if (transferTypes.indexOf(type) > -1) {
const tasks = await RNS3TransferUtility.getTasks(type);
if (!idAsKey) return tasks;
const idAsKeyTasks = {};
for (const task of tasks) {
idAsKeyTasks[task.id] = task;
}
return idAsKeyTasks;
}
return null;
}
subscribe(id, eventHandler) {
subscribeCallbacks[id] = eventHandler;
}
unsubscribe(id) {
delete subscribeCallbacks[id];
}
}
export const transferUtility = new TransferUtility();