1818import com .facebook .react .modules .core .DeviceEventManagerModule ;
1919
2020import net .gotev .uploadservice .BinaryUploadRequest ;
21+ import net .gotev .uploadservice .HttpUploadRequest ;
22+ import net .gotev .uploadservice .MultipartUploadRequest ;
2123import net .gotev .uploadservice .ServerResponse ;
2224import net .gotev .uploadservice .UploadInfo ;
2325import net .gotev .uploadservice .UploadNotificationConfig ;
@@ -83,18 +85,8 @@ public void getFileInfo(String path, final Promise promise) {
8385 }
8486
8587 /*
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.
9890 */
9991 @ ReactMethod
10092 public void startUpload (ReadableMap options , final Promise promise ) {
@@ -108,66 +100,108 @@ public void startUpload(ReadableMap options, final Promise promise) {
108100 return ;
109101 }
110102 }
103+
111104 if (options .hasKey ("headers" ) && options .getType ("headers" ) != ReadableType .Map ) {
112105 promise .reject (new IllegalArgumentException ("headers must be a hash." ));
113106 return ;
114107 }
108+
115109 if (options .hasKey ("notification" ) && options .getType ("notification" ) != ReadableType .Map ) {
116110 promise .reject (new IllegalArgumentException ("notification must be a hash." ));
117111 return ;
118112 }
119113
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+
120129 WritableMap notification = new WritableNativeMap ();
121130 notification .putBoolean ("enabled" , true );
131+
122132 if (options .hasKey ("notification" )) {
123133 notification .merge (options .getMap ("notification" ));
124134 }
125135
126136 String url = options .getString ("url" );
127137 String filePath = options .getString ("path" );
128138 String method = options .hasKey ("method" ) && options .getType ("method" ) == ReadableType .String ? options .getString ("method" ) : "POST" ;
139+
129140 final String customUploadId = options .hasKey ("customUploadId" ) && options .getType ("method" ) == ReadableType .String ? options .getString ("customUploadId" ) : null ;
141+
130142 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+
168201 if (notification .getBoolean ("enabled" )) {
169202 request .setNotificationConfig (new UploadNotificationConfig ());
170203 }
204+
171205 if (options .hasKey ("headers" )) {
172206 ReadableMap headers = options .getMap ("headers" );
173207 ReadableMapKeySetIterator keys = headers .keySetIterator ();
@@ -180,6 +214,7 @@ public void onCancelled(Context context, UploadInfo uploadInfo) {
180214 request .addHeader (key , headers .getString (key ));
181215 }
182216 }
217+
183218 String uploadId = request .startUpload ();
184219 promise .resolve (customUploadId != null ? customUploadId : uploadId );
185220 } catch (Exception exc ) {
0 commit comments