forked from mybigday/react-native-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
150 lines (129 loc) · 3.48 KB
/
index.ios.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
140
141
142
143
144
145
146
147
148
149
150
import { NativeModules, NativeAppEventEmitter } from "react-native";
import store from "react-native-simple-store";
const { RNS3TransferUtility } = NativeModules;
const transferTypes = ["upload", "download"];
const defaultOptions = {
region: "eu-west-1"
};
const storeKey = "@_RNS3_Tasks_Extra";
let taskExtras; // [id]: { state, bytes, totalBytes}
const subscribeCallbacks = {}; // [id]: function
NativeAppEventEmitter.addListener("@_RNS3_Events", async event => {
if (!taskExtras) await getTaskExtras();
const { task, /*type, */error } = event;
const { state, bytes, totalBytes } = task;
const finalTask = await setTaskExtra(task, { state, bytes, totalBytes });
if (subscribeCallbacks[task.id]) {
subscribeCallbacks[task.id](error, finalTask);
}
});
async function getTaskExtras() {
taskExtras = await store.get(storeKey) || {};
return taskExtras;
}
function putExtra(task) {
if (!taskExtras[task.id]) return task;
return { ...task, ...taskExtras[task.id] };
}
function saveTaskExtras() {
return store.save(storeKey, taskExtras);
}
async function setTaskExtra(task, values, isNew) {
const { id } = task;
if (!taskExtras[id] || isNew) {
taskExtras[id] = values;
} else {
if (taskExtras[id].bytes && !values.bytes) {
taskExtras[id] = { ...taskExtras[id], state: values.bytes };
} else {
taskExtras[id] = { ...taskExtras[id], ...values };
}
}
await saveTaskExtras();
return putExtra(task);
}
class TransferUtility {
async setupWithNative() {
RNS3TransferUtility.setupWithNative();
await getTaskExtras();
RNS3TransferUtility.initialize();
return true;
}
async setupWithBasic(options = {}) {
if (!options.access_key || !options.secret_key) {
return false;
}
RNS3TransferUtility.setupWithBasic({ ...defaultOptions, ...options});
await getTaskExtras();
RNS3TransferUtility.initialize();
return true;
}
async setupWithCognito(options = {}) {
if (!options.identity_pool_id) {
return false;
}
RNS3TransferUtility.setupWithCognito({ ...defaultOptions, ...options});
await getTaskExtras();
RNS3TransferUtility.initialize();
return true;
}
async upload(options = {}) {
if (!options.meta) {
options.meta = {};
}
const task = await RNS3TransferUtility.upload(options);
const finalTask = await setTaskExtra(task, {
bucket: options.bucket,
key: options.key,
state: task.state
}, true);
return finalTask;
}
async download(options = {}) {
const task = await RNS3TransferUtility.download(options);
const finalTask = await setTaskExtra(task, {
bucket: options.bucket,
key: options.key,
state: task.state
}, true);
return finalTask;
}
pause(id) {
RNS3TransferUtility.pause(id);
}
resume(id) {
RNS3TransferUtility.resume(id);
}
cancel(id) {
RNS3TransferUtility.cancel(id);
}
async getTask(id) {
const task = await RNS3TransferUtility.getTask(id);
if (task) {
return putExtra(task);
}
return null;
}
// idAsKey: return Object with id as key
async getTasks(type = "", idAsKey) {
if (transferTypes.indexOf(type) > -1) {
let tasks = await RNS3TransferUtility.getTasks(type);
tasks = tasks.map(task => putExtra(task));
if (!idAsKey) return tasks;
const idAsKeyTasks = {};
for (const task of tasks) {
idAsKeyTasks[task.id] = task;
}
return idAsKeyTasks;
}
return null;
}
subscribe(id, eventHandler) {
if (!taskExtras[id]) return;
subscribeCallbacks[id] = eventHandler;
}
unsubscribe(id) {
delete subscribeCallbacks[id];
}
}
export const transferUtility = new TransferUtility();