Skip to content

Commit

Permalink
feat(php) upload with token and videoId
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-lando committed Mar 15, 2023
1 parent f0d51c5 commit e91bec8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
6 changes: 3 additions & 3 deletions templates/php/src/VideoUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function uploadWithUploadToken(string $token, \SplFileObject $file, strin
return $this->execute(
'/upload?token='.$token,
$file,
$this->getChunkSize($contentRange)
$this->getChunkSize($contentRange),
$videoId
);
}

Expand All @@ -65,14 +66,13 @@ public function uploadWithUploadToken(string $token, \SplFileObject $file, strin
* @return Video
* @throws ClientExceptionInterface
*/
private function execute(string $path, \SplFileObject $file, int $chunkSize): Video
private function execute(string $path, \SplFileObject $file, int $chunkSize, string $videoId = null): Video
{
$start = 0;
$fileSize = filesize($file->getRealPath());
$handle = fopen($file->getRealPath(), 'r');

$response = null;
$videoId = null;
$part = 1;
$partsCount = ceil($fileSize / $chunkSize);

Expand Down
4 changes: 2 additions & 2 deletions templates/php/statics/.github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ jobs:
- name: Install dependancies
uses: php-actions/composer@v5
with:
php_version: 7.3
php_version: 8.1
- name: PHPUnit tests
uses: php-actions/phpunit@v2
env:
API_KEY: ${{ secrets.API_KEY }}
BASE_URI: https://sandbox.api.video
with:
args: tests
args: tests --log-junit output.xml
29 changes: 29 additions & 0 deletions templates/php/tests/Api/VideosApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,33 @@ public function testUploadThumbnail()
$this->assertEquals('thumbnail.jpg', basename($video->getAssets()->getThumbnail()));
}

public function testUploadWithUploadTokenWithVideoId()
{
$token = (new Helper($this->client))->createUploadToken();

$video = $this->client->videos()->create((new VideoCreationPayload())
->setTitle('Test video creation')
->setDescription('Test description'));

// No authorization needed for this endpoint
$client = new Client(
$_ENV['BASE_URI'],
null,
new Psr18Client()
);

$uploadedVideo = $client->videos()->uploadWithUploadToken(
$token->getToken(),
new SplFileObject(__DIR__ . '/../resources/558k.mp4'),
null,
$video->getVideoId()
);

$this->client->uploadTokens()->deleteToken($token->getToken());

$this->assertNotNull($uploadedVideo->getAssets()->getPlayer());
}

public function testUploadWithUploadToken()
{
$token = (new Helper($this->client))->createUploadToken();
Expand All @@ -274,6 +301,8 @@ public function testUploadWithUploadToken()
new SplFileObject(__DIR__ . '/../resources/558k.mp4')
);

$this->client->uploadTokens()->deleteToken($token->getToken());

$this->assertNotNull($uploadedVideo->getAssets()->getPlayer());
}
}

0 comments on commit e91bec8

Please sign in to comment.