-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
176 lines (152 loc) · 5.57 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// @flow
/**
* Handles HTTP background file uploads from an iOS device.
*/
import {Platform, NativeModules, NativeEventEmitter} from 'react-native';
export type UploadEvent =
| 'progress'
| 'error'
| 'completed'
| 'cancelled'
| 'bgExpired';
export type NotificationArgs = {
enabled: boolean,
};
export type StartUploadArgs = {
url: string,
// Optional, if not given, must be multipart, can be used to upload form data
path?: string,
method?: 'PUT' | 'POST',
// Optional, because raw is default
type?: 'raw' | 'multipart',
// This option is needed for multipart type
field?: string,
customUploadId?: string,
// parameters are supported only in multipart type
parameters?: {[string]: string},
headers?: Object,
notification?: NotificationArgs,
};
const NativeModule = NativeModules.VydiaRNFileUploader;
const eventPrefix = 'RNFileUploader-';
const eventEmitter = new NativeEventEmitter(NativeModule);
// add event listeners so they always fire on the native side
// no longer needed.
// if (Platform.OS === 'ios') {
// const identity = () => {};
// eventEmitter.addListener(eventPrefix + 'progress', identity);
// eventEmitter.addListener(eventPrefix + 'error', identity);
// eventEmitter.addListener(eventPrefix + 'cancelled', identity);
// eventEmitter.addListener(eventPrefix + 'completed', identity);
// eventEmitter.addListener(eventPrefix + 'bgExpired', identity);
// }
/*
Gets file information for the path specified.
Example valid path is:
iOS: 'file:///var/mobile/Containers/Data/Application/3C8A0EFB-A316-45C0-A30A-761BF8CCF2F8/tmp/trim.A5F76017-14E9-4890-907E-36A045AF9436.MOV;
Returns an object:
If the file exists: {extension: "mp4", size: "3804316", exists: true, mimeType: "video/mp4", name: "20161116_074726.mp4"}
If the file doesn't exist: {exists: false} and might possibly include name or extension
The promise should never be rejected.
*/
export const getFileInfo = (path: string): Promise<Object> => {
return NativeModule.getFileInfo(path).then((data) => {
if (data.size) {
// size comes back as a string on android so we convert it here. if it's already a number this won't hurt anything
data.size = +data.size;
}
return data;
});
};
/*
Starts uploading a file to an HTTP endpoint.
Options object:
{
url: string. url to post to.
path: string. path to the file on the device none for no file
headers: hash of name/value header pairs
method: HTTP method to use. Default is "POST"
notification: hash for customizing tray notifiaction
enabled: boolean to enable/disabled notifications, true by default.
}
Returns a promise with the string ID of the upload. Will reject if there is a connection problem, the file doesn't exist, or there is some other problem.
It is recommended to add listeners in the .then of this promise.
*/
export const startUpload = (options: StartUploadArgs): Promise<string> =>
NativeModule.startUpload(options);
/*
Cancels active upload by string ID of the upload.
Upload ID is returned in a promise after a call to startUpload method,
use it to cancel started upload.
Event "cancelled" will be fired when upload is cancelled.
Returns a promise with boolean true if operation was successfully completed.
Will reject if there was an internal error or ID format is invalid.
*/
export const cancelUpload = (cancelUploadId: string): Promise<boolean> => {
if (typeof cancelUploadId !== 'string') {
return Promise.reject(new Error('Upload ID must be a string'));
}
return NativeModule.cancelUpload(cancelUploadId);
};
/*
Listens for the given event on the given upload ID (resolved from startUpload).
If you don't supply a value for uploadId, the event will fire for all uploads.
Events (id is always the upload ID):
progress - { id: string, progress: int (0-100) }
error - { id: string, error: string }
cancelled - { id: string, error: string }
completed - { id: string }
*/
export const addListener = (
eventType: UploadEvent,
uploadId: string,
listener: Function,
) => {
return eventEmitter.addListener(eventPrefix + eventType, (data) => {
if (!uploadId || !data || !data.id || data.id === uploadId) {
listener(data);
}
});
};
// call this to let the OS it can suspend again
// it will be called after a short timeout if it isn't called at all
export const canSuspendIfBackground = () => {
if (Platform.OS === 'ios') {
NativeModule.canSuspendIfBackground();
}
};
// returns remaining background time in seconds
export const getRemainingBgTime = (): Promise<number> => {
if (Platform.OS === 'ios') {
return NativeModule.getRemainingBgTime();
}
return Promise.resolve(10 * 60 * 24); // dummy for android, large number
};
// marks the beginning of a background task and returns its ID
// in order to request extra background time
// do not call more than once without calling endBackgroundTask
// useful if we need to do more background processing in addition to network requests
// canSuspendIfBackground should still be called in case we run out of time.
export const beginBackgroundTask = (): Promise<number> => {
if (Platform.OS === 'ios') {
return NativeModule.beginBackgroundTask();
}
return Promise.resolve(null); // dummy for android
};
// marks the end of background task using the id returned by begin
// failing to call this might end up on app termination
export const endBackgroundTask = (id: number) => {
if (Platform.OS === 'ios') {
NativeModule.endBackgroundTask(id);
}
};
export default {
startUpload,
cancelUpload,
addListener,
getFileInfo,
canSuspendIfBackground,
getRemainingBgTime,
beginBackgroundTask,
endBackgroundTask,
};