Skip to content

Commit 6f69965

Browse files
committed
- fixed #251
- improvement multiple attachment upload perfomance
1 parent 7415c37 commit 6f69965

File tree

1 file changed

+20
-51
lines changed

1 file changed

+20
-51
lines changed

src/JiraClient.php

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,8 @@ public function exec($context, $post_data = null, $custom_request = null, $cooki
278278
*
279279
* @return resource
280280
*/
281-
private function createUploadHandle($url, $upload_file)
281+
private function createUploadHandle($url, $upload_file, $ch)
282282
{
283-
curl_reset($this->curl);
284-
$ch = $this->curl;
285283
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
286284
curl_setopt($ch, CURLOPT_URL, $url);
287285

@@ -345,68 +343,39 @@ public function upload($context, $filePathArray)
345343
{
346344
$url = $this->createUrlByContext($context);
347345

348-
// return value
349-
$result_code = 200;
350-
351-
$chArr = [];
352346
$results = [];
353-
$mh = curl_multi_init();
354347

355-
for ($idx = 0; $idx < count($filePathArray); $idx++) {
356-
$file = $filePathArray[$idx];
357-
if (file_exists($file) == false) {
358-
$body = "File $file not found";
359-
$result_code = -1;
360-
$this->closeCURLHandle($chArr, $mh, $body, $result_code);
348+
$ch = curl_init();
361349

362-
return $results;
363-
}
364-
$chArr[$idx] = $this->createUploadHandle($url, $filePathArray[$idx]);
365-
366-
curl_multi_add_handle($mh, $chArr[$idx]);
367-
}
350+
$this->multi = NULL;
368351

369-
$running = null;
370-
do {
371-
curl_multi_exec($mh, $running);
372-
} while ($running > 0);
352+
$idx = 0;
353+
foreach ($filePathArray as $file) {
354+
$this->createUploadHandle($url, $file, $ch);
373355

374-
// Get content and remove handles.
375-
$body = '';
376-
for ($idx = 0; $idx < count($chArr); $idx++) {
377-
$ch = $chArr[$idx];
356+
$response = curl_exec($ch);
378357

379-
$results[$idx] = curl_multi_getcontent($ch);
380-
381-
// if request failed.
382-
if (!$results[$idx]) {
383-
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
358+
// if request failed or have no result.
359+
if (!$response) {
360+
$http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
384361
$body = curl_error($ch);
385362

386-
//The server successfully processed the request, but is not returning any content.
387-
if ($this->http_response == 204) {
388-
continue;
389-
}
363+
if ($http_response === 204 || $http_response === 201 || $http_response === 200) {
364+
$results[$idx] = $response;
365+
} else {
366+
$msg = sprintf('CURL Error: http response=%d, %s', $http_response, $body);
367+
$this->log->error($msg);
390368

391-
// HostNotFound, No route to Host, etc Network error
392-
$result_code = -1;
393-
$body = 'CURL Error: = '.$body;
394-
$this->log->error($body);
395-
} else {
396-
// if request was ok, parsing http response code.
397-
$result_code = $this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
369+
curl_close($ch);
398370

399-
// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
400-
if ($this->http_response != 200 && $this->http_response != 201) {
401-
$body = 'CURL HTTP Request Failed: Status Code : '
402-
.$this->http_response.', URL:'.$url;
403-
404-
$this->log->error($body);
371+
throw new JiraException($msg);
405372
}
373+
} else {
374+
$results[$idx] = $response;
406375
}
407376
}
408377

409-
$this->closeCURLHandle($chArr, $mh, $body, $result_code);
378+
curl_close($ch);
410379

411380
return $results;
412381
}

0 commit comments

Comments
 (0)