18
18
import com .facebook .react .modules .core .DeviceEventManagerModule ;
19
19
20
20
import net .gotev .uploadservice .BinaryUploadRequest ;
21
+ import net .gotev .uploadservice .HttpUploadRequest ;
22
+ import net .gotev .uploadservice .MultipartUploadRequest ;
21
23
import net .gotev .uploadservice .ServerResponse ;
22
24
import net .gotev .uploadservice .UploadInfo ;
23
25
import net .gotev .uploadservice .UploadNotificationConfig ;
@@ -83,18 +85,8 @@ public void getFileInfo(String path, final Promise promise) {
83
85
}
84
86
85
87
/*
86
- * Starts a file upload.
87
- * Options are passed in as the first argument as a js hash:
88
- * {
89
- * url: string. url to post to.
90
- * path: string. path to the file on the device
91
- * headers: hash of name/value header pairs
92
- * method: HTTP method to use. Default is "POST"
93
- * notification: hash for customizing tray notifiaction
94
- * enabled: boolean to enable/disabled notifications, true by default.
95
- * }
96
- *
97
- * Returns a promise with the string ID of the upload.
88
+ * Starts a file upload.
89
+ * Returns a promise with the string ID of the upload.
98
90
*/
99
91
@ ReactMethod
100
92
public void startUpload (ReadableMap options , final Promise promise ) {
@@ -108,66 +100,108 @@ public void startUpload(ReadableMap options, final Promise promise) {
108
100
return ;
109
101
}
110
102
}
103
+
111
104
if (options .hasKey ("headers" ) && options .getType ("headers" ) != ReadableType .Map ) {
112
105
promise .reject (new IllegalArgumentException ("headers must be a hash." ));
113
106
return ;
114
107
}
108
+
115
109
if (options .hasKey ("notification" ) && options .getType ("notification" ) != ReadableType .Map ) {
116
110
promise .reject (new IllegalArgumentException ("notification must be a hash." ));
117
111
return ;
118
112
}
119
113
114
+ String requestType = "raw" ;
115
+
116
+ if (options .hasKey ("type" )) {
117
+ requestType = options .getString ("type" );
118
+ if (requestType == null ) {
119
+ promise .reject (new IllegalArgumentException ("type must be string." ));
120
+ return ;
121
+ }
122
+
123
+ if (!requestType .equals ("raw" ) && !requestType .equals ("multipart" )) {
124
+ promise .reject (new IllegalArgumentException ("type should be string: raw or multipart." ));
125
+ return ;
126
+ }
127
+ }
128
+
120
129
WritableMap notification = new WritableNativeMap ();
121
130
notification .putBoolean ("enabled" , true );
131
+
122
132
if (options .hasKey ("notification" )) {
123
133
notification .merge (options .getMap ("notification" ));
124
134
}
125
135
126
136
String url = options .getString ("url" );
127
137
String filePath = options .getString ("path" );
128
138
String method = options .hasKey ("method" ) && options .getType ("method" ) == ReadableType .String ? options .getString ("method" ) : "POST" ;
139
+
129
140
final String customUploadId = options .hasKey ("customUploadId" ) && options .getType ("method" ) == ReadableType .String ? options .getString ("customUploadId" ) : null ;
141
+
130
142
try {
131
- final BinaryUploadRequest request = (BinaryUploadRequest ) new BinaryUploadRequest (this .getReactApplicationContext (), url )
132
- .setMethod (method )
133
- .setFileToUpload (filePath )
134
- .setMaxRetries (2 )
135
- .setDelegate (new UploadStatusDelegate () {
136
- @ Override
137
- public void onProgress (Context context , UploadInfo uploadInfo ) {
138
- WritableMap params = Arguments .createMap ();
139
- params .putString ("id" , customUploadId != null ? customUploadId : uploadInfo .getUploadId ());
140
- params .putInt ("progress" , uploadInfo .getProgressPercent ()); //0-100
141
- sendEvent ("progress" , params );
142
- }
143
-
144
- @ Override
145
- public void onError (Context context , UploadInfo uploadInfo , Exception exception ) {
146
- WritableMap params = Arguments .createMap ();
147
- params .putString ("id" , customUploadId != null ? customUploadId : uploadInfo .getUploadId ());
148
- params .putString ("error" , exception .getMessage ());
149
- sendEvent ("error" , params );
150
- }
151
-
152
- @ Override
153
- public void onCompleted (Context context , UploadInfo uploadInfo , ServerResponse serverResponse ) {
154
- WritableMap params = Arguments .createMap ();
155
- params .putString ("id" , customUploadId != null ? customUploadId : uploadInfo .getUploadId ());
156
- params .putInt ("responseCode" , serverResponse .getHttpCode ());
157
- params .putString ("responseBody" , serverResponse .getBodyAsString ());
158
- sendEvent ("completed" , params );
159
- }
160
-
161
- @ Override
162
- public void onCancelled (Context context , UploadInfo uploadInfo ) {
163
- WritableMap params = Arguments .createMap ();
164
- params .putString ("id" , customUploadId != null ? customUploadId : uploadInfo .getUploadId ());
165
- sendEvent ("cancelled" , params );
166
- }
167
- });
143
+ UploadStatusDelegate statusDelegate = new UploadStatusDelegate () {
144
+ @ Override
145
+ public void onProgress (Context context , UploadInfo uploadInfo ) {
146
+ WritableMap params = Arguments .createMap ();
147
+ params .putString ("id" , customUploadId != null ? customUploadId : uploadInfo .getUploadId ());
148
+ params .putInt ("progress" , uploadInfo .getProgressPercent ()); //0-100
149
+ sendEvent ("progress" , params );
150
+ }
151
+
152
+ @ Override
153
+ public void onError (Context context , UploadInfo uploadInfo , Exception exception ) {
154
+ WritableMap params = Arguments .createMap ();
155
+ params .putString ("id" , customUploadId != null ? customUploadId : uploadInfo .getUploadId ());
156
+ params .putString ("error" , exception .getMessage ());
157
+ sendEvent ("error" , params );
158
+ }
159
+
160
+ @ Override
161
+ public void onCompleted (Context context , UploadInfo uploadInfo , ServerResponse serverResponse ) {
162
+ WritableMap params = Arguments .createMap ();
163
+ params .putString ("id" , customUploadId != null ? customUploadId : uploadInfo .getUploadId ());
164
+ params .putInt ("responseCode" , serverResponse .getHttpCode ());
165
+ params .putString ("responseBody" , serverResponse .getBodyAsString ());
166
+ sendEvent ("completed" , params );
167
+ }
168
+
169
+ @ Override
170
+ public void onCancelled (Context context , UploadInfo uploadInfo ) {
171
+ WritableMap params = Arguments .createMap ();
172
+ params .putString ("id" , customUploadId != null ? customUploadId : uploadInfo .getUploadId ());
173
+ sendEvent ("cancelled" , params );
174
+ }
175
+ };
176
+
177
+ HttpUploadRequest <?> request ;
178
+
179
+ if (requestType .equals ("raw" )) {
180
+ request = new BinaryUploadRequest (this .getReactApplicationContext (), customUploadId , url )
181
+ .setFileToUpload (filePath );
182
+ } else {
183
+ if (!options .hasKey ("field" )) {
184
+ promise .reject (new IllegalArgumentException ("field is required field for multipart type." ));
185
+ return ;
186
+ }
187
+
188
+ if (options .getType ("field" ) != ReadableType .String ) {
189
+ promise .reject (new IllegalArgumentException ("field must be string." ));
190
+ return ;
191
+ }
192
+
193
+ request = new MultipartUploadRequest (this .getReactApplicationContext (), customUploadId , url )
194
+ .addFileToUpload (filePath , options .getString ("field" ));
195
+ }
196
+
197
+ request .setMethod (method )
198
+ .setMaxRetries (2 )
199
+ .setDelegate (statusDelegate );
200
+
168
201
if (notification .getBoolean ("enabled" )) {
169
202
request .setNotificationConfig (new UploadNotificationConfig ());
170
203
}
204
+
171
205
if (options .hasKey ("headers" )) {
172
206
ReadableMap headers = options .getMap ("headers" );
173
207
ReadableMapKeySetIterator keys = headers .keySetIterator ();
@@ -180,6 +214,7 @@ public void onCancelled(Context context, UploadInfo uploadInfo) {
180
214
request .addHeader (key , headers .getString (key ));
181
215
}
182
216
}
217
+
183
218
String uploadId = request .startUpload ();
184
219
promise .resolve (customUploadId != null ? customUploadId : uploadId );
185
220
} catch (Exception exc ) {
0 commit comments