Skip to content

Commit 2bcef71

Browse files
committed
Only one TransferUtility js file
1 parent ab899d2 commit 2bcef71

File tree

4 files changed

+65
-184
lines changed

4 files changed

+65
-184
lines changed

index.ios.js

Lines changed: 0 additions & 160 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
"name": "react-native-s3",
33
"version": "0.0.4",
44
"description": "A React Native wrapper for AWS S3 SDK",
5+
"main": "./src/index.js",
56
"scripts": {
6-
"lint": "eslint *.js example/*.js",
7+
"lint": "eslint src/*.js example/*.js",
78
"preinstall": "./scripts/download-ios.sh 2.3.5"
89
},
910
"repository": {
Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NativeModules, DeviceEventEmitter } from "react-native";
1+
import { NativeModules, NativeAppEventEmitter, DeviceEventEmitter, Platform } from "react-native";
22
import store from "react-native-simple-store";
33

44
const { RNS3TransferUtility } = NativeModules;
@@ -12,16 +12,36 @@ const defaultCognitoOptions = {
1212
cognito_region: "eu-west-1"
1313
};
1414
const storeKey = "@_RNS3_Tasks_Extra";
15-
let taskExtras; // [id]: { bucket, key, bytes }
15+
/*
16+
* taskExtra:
17+
* [id]:
18+
* iOS: { bucket, key, state, bytes, totalBytes }
19+
* Android: { bucket, key, bytes }
20+
*/
21+
let taskExtras;
1622
const subscribeCallbacks = {}; // [id]: function
1723

18-
DeviceEventEmitter.addListener("@_RNS3_Events", async event => {
24+
let EventEmitter;
25+
if (Platform.OS === "ios") {
26+
EventEmitter = NativeAppEventEmitter;
27+
} else if (Platform.OS === "android") {
28+
EventEmitter = DeviceEventEmitter;
29+
}
30+
31+
EventEmitter.addListener("@_RNS3_Events", async event => {
1932
if (!taskExtras) await getTaskExtras();
2033
const { task, error } = event;
21-
const { bytes } = task;
22-
const finalTask = await setTaskExtra(task, { bytes });
34+
35+
let finalTask = task;
36+
if (Platform.OS === "ios") {
37+
const { state, bytes, totalBytes } = task;
38+
finalTask = await setTaskExtra(task, { state, bytes, totalBytes });
39+
} else if (Platform.OS === "android") {
40+
const { bytes } = task;
41+
finalTask = await setTaskExtra(task, { bytes });
42+
}
2343
if (subscribeCallbacks[task.id]) {
24-
subscribeCallbacks[task.id](error, task);
44+
subscribeCallbacks[task.id](error, finalTask);
2545
}
2646
});
2747

@@ -44,15 +64,23 @@ async function setTaskExtra(task, values, isNew) {
4464
if (!taskExtras[id] || isNew) {
4565
taskExtras[id] = values;
4666
} else {
47-
if (values.bytes) {
48-
taskExtras[id] = { ...taskExtras[id], ...values };
67+
if (Platform.OS === "ios") {
68+
if (taskExtras[id].bytes && !values.bytes) {
69+
taskExtras[id] = { ...taskExtras[id], state: values.state };
70+
} else {
71+
taskExtras[id] = { ...taskExtras[id], ...values };
72+
}
73+
} else if (Platform.OS === "android") {
74+
if (values.bytes) {
75+
taskExtras[id] = { ...taskExtras[id], ...values };
76+
}
4977
}
5078
}
5179
await saveTaskExtras();
5280
return putExtra(task);
5381
}
5482

55-
class TransferUtility {
83+
export default class TransferUtility {
5684
async setupWithNative() {
5785
const result = await RNS3TransferUtility.setupWithNative();
5886
if (result) {
@@ -66,10 +94,10 @@ class TransferUtility {
6694
if (!options.access_key || !options.secret_key) {
6795
return false;
6896
}
69-
if (!options.session_token) {
97+
if (Platform.OS === "android" && !options.session_token) {
7098
options.session_token = null;
7199
}
72-
const result = await RNS3TransferUtility.setupWithBasic({ ...defaultOptions, ...options });
100+
const result = await RNS3TransferUtility.setupWithBasic({ ...defaultOptions, ...options});
73101
if (result) {
74102
await getTaskExtras();
75103
RNS3TransferUtility.initializeRNS3();
@@ -81,10 +109,7 @@ class TransferUtility {
81109
if (!options.identity_pool_id) {
82110
return false;
83111
}
84-
if (!options.caching) {
85-
options.caching = false;
86-
}
87-
const result = await RNS3TransferUtility.setupWithCognito({ ...defaultCognitoOptions, ...options });
112+
const result = await RNS3TransferUtility.setupWithBasic({ ...defaultCognitoOptions, ...options });
88113
if (result) {
89114
await getTaskExtras();
90115
RNS3TransferUtility.initializeRNS3();
@@ -97,20 +122,28 @@ class TransferUtility {
97122
options.meta = {};
98123
}
99124
const task = await RNS3TransferUtility.upload(options);
100-
const finalTask = await setTaskExtra(task, {
125+
const extra = {
101126
bucket: options.bucket,
102127
key: options.key
103-
}, true);
104-
return task;
128+
};
129+
if (Platform.OS === "ios") {
130+
extra.state = task.state;
131+
}
132+
const finalTask = await setTaskExtra(task, extra, true);
133+
return finalTask;
105134
}
106135

107136
async download(options = {}) {
108137
const task = await RNS3TransferUtility.download(options);
109-
const finalTask = await setTaskExtra(task, {
138+
const extra = {
110139
bucket: options.bucket,
111140
key: options.key
112-
}, true);
113-
return task;
141+
};
142+
if (Platform.OS === "ios") {
143+
extra.state = task.state;
144+
}
145+
const finalTask = await setTaskExtra(task, extra, true);
146+
return finalTask;
114147
}
115148

116149
pause(id) {
@@ -125,7 +158,11 @@ class TransferUtility {
125158
RNS3TransferUtility.cancel(id);
126159
}
127160

161+
// Android only
128162
async deleteRecord(id) {
163+
if (Platform.OS === "ios") {
164+
throw new Error("Not implemented");
165+
}
129166
return RNS3TransferUtility.deleteRecord(id);
130167
}
131168

@@ -162,5 +199,3 @@ class TransferUtility {
162199
delete subscribeCallbacks[id];
163200
}
164201
}
165-
166-
export const transferUtility = new TransferUtility();

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import TransferUtility from "./TransferUtility";
2+
3+
module.exports = {
4+
transferUtility: new TransferUtility()
5+
};

0 commit comments

Comments
 (0)