Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

Resumed file upload on curl error #1106

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Facebook/FacebookClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
namespace Facebook;

use Facebook\Exceptions\FacebookResponseException;
use Facebook\HttpClients\FacebookHttpClientInterface;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookStreamHttpClient;
Expand Down Expand Up @@ -192,7 +193,7 @@ public function prepareRequestMessage(FacebookRequest $request)
* @param FacebookRequest $request
*
* @return FacebookResponse
*
* @throws FacebookResponseException
* @throws FacebookSDKException
*/
public function sendRequest(FacebookRequest $request)
Expand Down
9 changes: 9 additions & 0 deletions src/Facebook/FileUpload/FacebookResumableUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function start($endpoint, FacebookFile $file)
* @return FacebookTransferChunk
*
* @throws FacebookResponseException
* @throws FacebookSDKException
*/
public function transfer($endpoint, FacebookTransferChunk $chunk, $allowToThrow = false)
{
Expand Down Expand Up @@ -132,6 +133,12 @@ public function transfer($endpoint, FacebookTransferChunk $chunk, $allowToThrow
}

// Return the same chunk entity so it can be retried.
return $chunk;
} catch (FacebookSDKException $e) {
if ($allowToThrow) {
throw $e;
}

return $chunk;
}

Expand Down Expand Up @@ -167,6 +174,8 @@ public function finish($endpoint, $uploadSessionId, $metadata = [])
* @param array $params The params to send with the request.
*
* @return array
* @throws FacebookResponseException
* @throws FacebookSDKException
*/
private function sendUploadRequest($endpoint, $params = [])
{
Expand Down
10 changes: 10 additions & 0 deletions tests/FileUpload/FacebookResumableUploaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,14 @@ public function testFailedResumableTransferWillNotThrowAndReturnNewChunk()
$this->assertEquals(40, $newChunk->getStartOffset());
$this->assertEquals(50, $newChunk->getEndOffset());
}

public function testFailedClientRequestWillNotThrowAndReturnSameChunk()
{
$this->graphApi->failOnClientRequest();
$uploader = new FacebookResumableUploader($this->fbApp, $this->client, 'access_token', 'v2.4');

$chunk = new FacebookTransferChunk($this->file, '1', '2', '3', '4');
$newChunk = $uploader->transfer('/me/videos', $chunk);
$this->assertSame($newChunk, $chunk);
}
}
10 changes: 10 additions & 0 deletions tests/Fixtures/FakeGraphApiForResumableUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace Facebook\Tests\Fixtures;

use Facebook\Exceptions\FacebookSDKException;
use Facebook\Http\GraphRawResponse;
use Facebook\HttpClients\FacebookHttpClientInterface;

Expand All @@ -42,6 +43,11 @@ public function failOnTransfer()
$this->respondWith = 'FAIL_ON_TRANSFER';
}

public function failOnClientRequest()
{
$this->respondWith = 'FAIL_ON_CLIENT_REQUEST';
}

public function failOnTransferAndUploadNewChunk()
{
$this->respondWith = 'FAIL_ON_TRANSFER_AND_UPLOAD_NEW_CHUNK';
Expand Down Expand Up @@ -95,6 +101,10 @@ private function respondTransfer()
);
}

if ($this->respondWith == 'FAIL_ON_CLIENT_REQUEST') {
throw new FacebookSDKException("SSL_write() returned SYSCALL, errno = 104", 55);
}

switch ($this->transferCount) {
case 0:
$data = ['start_offset' => 20, 'end_offset' => 40];
Expand Down