Skip to content

Commit 663614c

Browse files
authored
Merge pull request #308 from vimeo/tus-client-factory
2 parents d0635c1 + 08214b0 commit 663614c

4 files changed

Lines changed: 113 additions & 4 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ $video_response = $lib->request(
221221
);
222222
```
223223

224+
#### Using a custom TusClient
225+
226+
If the standard [TusPhp\Client](https://github.com/ankitpokhrel/tus-php/blob/main/src/Tus/Client.php) doesn't work for you,
227+
you can pass in a custom factory for a client that suits your needs. See the [custom_cache example](https://github.com/vimeo/vimeo.php/blob/master/example/upload_image.php).
228+
224229
### Upload images
225230

226231
To upload an image, call the `uploadImage` method. It takes three parameters.

example/upload_custom_cache.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
use TusPhp\Tus\Client;
3+
use Vimeo\Upload\TusClient;
4+
use Vimeo\Upload\TusClientFactory;
5+
use Vimeo\Vimeo;
6+
use Vimeo\Exceptions\VimeoUploadException;
7+
8+
/**
9+
* Copyright 2022 Vimeo
10+
*
11+
* Licensed under the Apache License, Version 2.0 (the "License");
12+
* you may not use this file except in compliance with the License.
13+
* You may obtain a copy of the License at
14+
*
15+
* http://www.apache.org/licenses/LICENSE-2.0
16+
*
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*/
23+
$config = require(__DIR__ . '/init.php');
24+
25+
class LocalCacheTusClientFactory extends TusClientFactory
26+
{
27+
public function getTusClient(string $base_uri, string $url): Client
28+
{
29+
$client = new TusClient($base_uri);
30+
$client->setUrl($url);
31+
$client->setCache(new \TusPhp\Cache\FileStore('./cache'));
32+
return $client;
33+
}
34+
}
35+
36+
if (empty($config['access_token'])) {
37+
throw new Exception(
38+
'You can not upload a file without an access token. You can find this token on your app page, or generate ' .
39+
'one using `auth.php`.'
40+
);
41+
}
42+
43+
// Instantiate the library with your client id, secret and access token (pulled from dev site), pass in the TusClientFactory
44+
$lib = new Vimeo($config['client_id'], $config['client_secret'], $config['access_token'], new LocalCacheTusClientFactory());
45+
46+
// Create a variable with a hard coded path to your file system
47+
$file_name = '<fully qualified path>';
48+
49+
echo 'Uploading: ' . $file_name . "\n";
50+
51+
try {
52+
// Upload the file and include the video title and description.
53+
$uri = $lib->upload($file_name, array(
54+
'name' => 'Vimeo API SDK test upload',
55+
'description' => "This video was uploaded through the Vimeo API's PHP SDK."
56+
));
57+
58+
// Get the metadata response from the upload and log out the Vimeo.com url
59+
$video_data = $lib->request($uri . '?fields=link');
60+
echo '"' . $file_name . ' has been uploaded to ' . $video_data['body']['link'] . "\n";
61+
62+
// Make an API call to edit the title and description of the video.
63+
$lib->request($uri, array(
64+
'name' => 'Vimeo API SDK test edit',
65+
'description' => "This video was edited through the Vimeo API's PHP SDK.",
66+
), 'PATCH');
67+
68+
echo 'The title and description for ' . $uri . ' has been edited.' . "\n";
69+
70+
// Make an API call to see if the video is finished transcoding.
71+
$video_data = $lib->request($uri . '?fields=transcode.status');
72+
echo 'The transcode status for ' . $uri . ' is: ' . $video_data['body']['transcode']['status'] . "\n";
73+
} catch (VimeoUploadException $e) {
74+
// We may have had an error. We can't resolve it here necessarily, so report it to the user.
75+
echo 'Error uploading ' . $file_name . "\n";
76+
echo 'Server reported: ' . $e->getMessage() . "\n";
77+
} catch (VimeoRequestException $e) {
78+
echo 'There was an error making the request.' . "\n";
79+
echo 'Server reported: ' . $e->getMessage() . "\n";
80+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Vimeo\Upload;
4+
5+
use TusPhp\Tus\Client;
6+
7+
class TusClientFactory
8+
{
9+
/**
10+
* @param string $base_uri The fully qualified domain of the upload, ex: https://us-files.tus.vimeo.com
11+
* @param string $url The fully qualified url of the upload, ex: https://us-files.tus.vimeo.com/files/vimeo-a1b2c3d4
12+
* @return Client
13+
*/
14+
public function getTusClient(string $base_uri, string $url) : Client
15+
{
16+
$client = new TusClient($base_uri);
17+
$client->setUrl($url);
18+
return $client;
19+
}
20+
}

src/Vimeo/Vimeo.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Vimeo\Exceptions\VimeoException;
66
use Vimeo\Exceptions\VimeoRequestException;
77
use Vimeo\Exceptions\VimeoUploadException;
8-
use Vimeo\Upload\TusClient;
8+
use Vimeo\Upload\TusClientFactory;
99

1010
/**
1111
* Copyright 2013 Vimeo
@@ -52,14 +52,18 @@ class Vimeo
5252
/** @var null|string */
5353
private $_access_token = null;
5454

55+
/** @var TusClientFactory */
56+
private $_tus_client_factory = null;
57+
5558
/**
5659
* Creates the Vimeo library, and tracks the client and token information.
5760
*
5861
* @param string $client_id Your applications client id. Can be found on developer.vimeo.com/apps
5962
* @param string $client_secret Your applications client secret. Can be found on developer.vimeo.com/apps
6063
* @param string|null $access_token Your access token. Can be found on developer.vimeo.com/apps or generated using OAuth 2.
64+
* @param TusClientFactory|null $tus_client_interface Your tus client that will be used.
6165
*/
62-
public function __construct(string $client_id, string $client_secret, string $access_token = null)
66+
public function __construct(string $client_id, string $client_secret, string $access_token = null, TusClientFactory $tus_client_factory = null)
6367
{
6468
$this->_client_id = $client_id;
6569
$this->_client_secret = $client_secret;
@@ -72,6 +76,7 @@ public function __construct(string $client_id, string $client_secret, string $ac
7276
//Certificate must indicate that the server is the server to which you meant to connect.
7377
CURLOPT_SSL_VERIFYHOST => 2,
7478
);
79+
$this->_tus_client_factory = $tus_client_factory ?? new TusClientFactory();
7580
}
7681

7782
/**
@@ -589,10 +594,9 @@ private function perform_upload_tus(string $file_path, $file_size, array $attemp
589594
$failures = 0;
590595
$chunk_size = $this->getTusUploadChunkSize($default_chunk_size, (int)$file_size);
591596

592-
$client = new TusClient($base_url);
597+
$client = $this->_tus_client_factory->getTusClient($base_url, $url);
593598
$client->setApiPath($api_path);
594599
$client->setKey($key)->file($file_path);
595-
$client->setUrl($url);
596600
$client->getCache()->set($client->getKey(),[
597601
'location' => $url,
598602
'expires_at' => Carbon::now()->addSeconds($client->getCache()->getTtl())->format($client->getCache()::RFC_7231),

0 commit comments

Comments
 (0)