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

Commit

Permalink
Fix issue with chunked upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Antho committed Aug 7, 2018
1 parent 39a07df commit 8800c04
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
37 changes: 20 additions & 17 deletions src/Api/Videos.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ApiVideo\Client\Buzz\FormByteRangeUpload;
use ApiVideo\Client\Buzz\OAuthBrowser;
use ApiVideo\Client\Model\Video;
use Buzz\Exception\RequestException;
use Buzz\Message\Form\FormUpload;
use Buzz\Message\RequestInterface;

Expand Down Expand Up @@ -184,25 +185,27 @@ public function upload($source, array $properties = array(), $videoId = null)
$chunk = fopen($chunkPath, 'w+b');
$from = $copiedBytes;
$copiedBytes += stream_copy_to_stream($resource, $chunk, $this->chunkSize, $copiedBytes);

$response = $this->browser->submit(
"/videos/$videoId/source",
array('file' => new FormByteRangeUpload($chunkPath, $from, $copiedBytes, $length)),
RequestInterface::METHOD_POST,
array(
'Content-Range' => 'bytes '.$from.'-'.($copiedBytes - 1).'/'.$length,
'Expect' => '',
)
);

if ($response->getStatusCode() >= 400) {
$this->registerLastError($response);

return null;
$lastResponse = null;
$response= null;
try {
$response = $this->browser->submit(
"/videos/$videoId/source",
array('file' => new FormByteRangeUpload($chunkPath, $from, $copiedBytes, $length)),
RequestInterface::METHOD_POST,
array(
'Content-Range' => 'bytes '.$from.'-'.($copiedBytes - 1).'/'.$length,
'Expect' => '',
)
);
if($response->isSuccessful() && $response->getStatusCode() !== 100){
$lastResponse = $this->unmarshal($response);
}
} catch (RequestException $e) {
if ($e->getCode() !== 100 && $e->getCode() >= 400) {
return null;
}
}

$lastResponse = $this->unmarshal($response);

fclose($chunk);
unlink($chunkPath);

Expand Down
4 changes: 0 additions & 4 deletions tests/Api/VideosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1084,11 +1084,7 @@ public function uploadLargeFileWithBadVideoIdFailed()
$videos = new Videos($mockedBrowser);
$videos->chunkSize = 10 * 1024 * 1024;
$result = $videos->upload($this->getValideVideo()->url(), array(), 'vilWKqgywdX55mg8Yu8WgDZ0');

$this->assertNull($result);
$error = $videos->getLastError();

$this->assertSame(404, $error['status']);
}

/**
Expand Down

0 comments on commit 8800c04

Please sign in to comment.