@@ -174,18 +174,7 @@ public function exec($context, $post_data = null, $custom_request = null) {
174
174
return $ response ;
175
175
}
176
176
177
- /**
178
- * file upload
179
- *
180
- * @param context url context
181
- * @param upload_file upload file.
182
- *
183
- * @todo multiple file upload suppport.
184
- *
185
- */
186
- public function upload ($ context , $ upload_file ) {
187
- $ url = $ this ->host . $ this ->api_uri . '/ ' . preg_replace ('/\// ' , '' , $ context , 1 );
188
-
177
+ private function createUploadHandle ($ url , $ upload_file ) {
189
178
$ ch =curl_init ();
190
179
curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
191
180
curl_setopt ($ ch , CURLOPT_URL , $ url );
@@ -227,38 +216,93 @@ public function upload($context, $upload_file) {
227
216
228
217
curl_setopt ($ ch , CURLOPT_VERBOSE , $ this ->CURLOPT_VERBOSE );
229
218
230
- $ this ->log ->addDebug ('Curl exec= ' . $ url );
231
- $ response = curl_exec ($ ch );
232
-
233
- // if request failed.
234
- if (!$ response ) {
235
- $ this ->http_response = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
236
- $ body = curl_error ($ ch );
237
- curl_close ($ ch );
219
+ $ this ->log ->addDebug ('Curl exec= ' . $ url );
220
+ return $ ch ;
221
+ }
238
222
239
- //The server successfully processed the request, but is not returning any content.
240
- if ($ this ->http_response == 204 ){
241
- return "" ;
242
- }
243
-
244
- // HostNotFound, No route to Host, etc Network error
245
- $ this ->log ->addError ("CURL Error: = " . $ body );
246
- throw new JIRAException ("CURL Error: = " . $ body );
247
- } else {
248
- // if request was ok, parsing http response code.
249
- $ this ->http_response = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
223
+ /**
224
+ * file upload
225
+ *
226
+ * @param context url context
227
+ * @param filePathArray upload file path.
228
+ *
229
+ */
230
+ public function upload ($ context , $ filePathArray ) {
231
+ $ url = $ this ->host . $ this ->api_uri . '/ ' . preg_replace ('/\// ' , '' , $ context , 1 );
250
232
233
+ // return value
234
+ $ result_code = 200 ;
235
+
236
+ $ chArr = array ();
237
+ $ results = array ();
238
+ $ mh = curl_multi_init ();
239
+
240
+ for ($ idx = 0 ; $ idx < count ($ filePathArray ); $ idx ++) {
241
+ $ file = $ filePathArray [$ idx ];
242
+ if (file_exists ($ file ) == false ) {
243
+ $ body = "File $ file not found " ;
244
+ $ result_code = -1 ;
245
+ goto end;
246
+ }
247
+ $ chArr [$ idx ] = $ this ->createUploadHandle ($ url , $ filePathArray [$ idx ]);
248
+
249
+ curl_multi_add_handle ($ mh , $ chArr [$ idx ]);
250
+ }
251
+
252
+ $ running = null ;
253
+ do {
254
+ curl_multi_exec ($ mh , $ running );
255
+ }
256
+ while ($ running > 0 );
257
+
258
+ // Get content and remove handles.
259
+ for ($ idx = 0 ; $ idx < count ($ chArr ); $ idx ++) {
260
+ $ ch = $ chArr [$ idx ];
261
+
262
+ $ results [$ idx ] = curl_multi_getcontent ($ ch );
263
+
264
+ // if request failed.
265
+ if (!$ results [$ idx ]) {
266
+ $ this ->http_response = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
267
+ $ body = curl_error ($ ch );
268
+
269
+ //The server successfully processed the request, but is not returning any content.
270
+ if ($ this ->http_response == 204 ){
271
+ continue ;
272
+ }
273
+
274
+ // HostNotFound, No route to Host, etc Network error
275
+ $ result_code = -1 ;
276
+ $ body = "CURL Error: = " . $ body ;
277
+ $ this ->log ->addError ($ body );
278
+ } else {
279
+ // if request was ok, parsing http response code.
280
+ $ result_code = $ this ->http_response = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
281
+
282
+ // don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
283
+ if ($ this ->http_response != 200 && $ this ->http_response != 201 ) {
284
+ $ body = "CURL HTTP Request Failed: Status Code : "
285
+ . $ this ->http_response . ", URL: " . $ url
286
+ . "\nError Message : " . $ response ;
287
+ $ this ->log ->addError ($ body );
288
+ }
289
+ }
290
+ }
291
+
292
+ // clean up
293
+ end:
294
+ foreach ($ chArr as $ ch ) {
295
+ $ this ->log ->addDebug ("CURL Close handle.. " );
251
296
curl_close ($ ch );
297
+ curl_multi_remove_handle ($ mh , $ ch );
298
+ }
299
+ $ this ->log ->addDebug ("CURL Multi Close handle.. " );
300
+ curl_multi_close ($ mh );
301
+ if ($ result_code != 200 ) {
302
+ throw new JIRAException ("CURL Error: = " . $ body , $ result_code );
303
+ }
252
304
253
- // don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
254
- if ($ this ->http_response != 200 && $ this ->http_response != 201 ) {
255
- throw new JIRAException ("CURL HTTP Request Failed: Status Code : "
256
- . $ this ->http_response . ", URL: " . $ url
257
- . "\nError Message : " . $ response , $ this ->http_response );
258
- }
259
- }
260
-
261
- return $ response ;
305
+ return $ results ;
262
306
}
263
307
}
264
308
0 commit comments