Skip to content

Commit

Permalink
- fixed #251
Browse files Browse the repository at this point in the history
- improvement multiple attachment upload perfomance
  • Loading branch information
lesstif committed May 14, 2019
1 parent 7415c37 commit 6f69965
Showing 1 changed file with 20 additions and 51 deletions.
71 changes: 20 additions & 51 deletions src/JiraClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,8 @@ public function exec($context, $post_data = null, $custom_request = null, $cooki
*
* @return resource
*/
private function createUploadHandle($url, $upload_file)
private function createUploadHandle($url, $upload_file, $ch)
{
curl_reset($this->curl);
$ch = $this->curl;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);

Expand Down Expand Up @@ -345,68 +343,39 @@ public function upload($context, $filePathArray)
{
$url = $this->createUrlByContext($context);

// return value
$result_code = 200;

$chArr = [];
$results = [];
$mh = curl_multi_init();

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

return $results;
}
$chArr[$idx] = $this->createUploadHandle($url, $filePathArray[$idx]);

curl_multi_add_handle($mh, $chArr[$idx]);
}
$this->multi = NULL;

$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
$idx = 0;
foreach ($filePathArray as $file) {
$this->createUploadHandle($url, $file, $ch);

// Get content and remove handles.
$body = '';
for ($idx = 0; $idx < count($chArr); $idx++) {
$ch = $chArr[$idx];
$response = curl_exec($ch);

$results[$idx] = curl_multi_getcontent($ch);

// if request failed.
if (!$results[$idx]) {
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// if request failed or have no result.
if (!$response) {
$http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$body = curl_error($ch);

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

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

// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
if ($this->http_response != 200 && $this->http_response != 201) {
$body = 'CURL HTTP Request Failed: Status Code : '
.$this->http_response.', URL:'.$url;

$this->log->error($body);
throw new JiraException($msg);
}
} else {
$results[$idx] = $response;
}
}

$this->closeCURLHandle($chArr, $mh, $body, $result_code);
curl_close($ch);

return $results;
}
Expand Down

0 comments on commit 6f69965

Please sign in to comment.