Skip to content

Commit

Permalink
Merge pull request #308 from vimeo/tus-client-factory
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronm67 authored Feb 3, 2022
2 parents d0635c1 + 08214b0 commit 663614c
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,11 @@ $video_response = $lib->request(
);
```

#### Using a custom TusClient

If the standard [TusPhp\Client](https:/ankitpokhrel/tus-php/blob/main/src/Tus/Client.php) doesn't work for you,
you can pass in a custom factory for a client that suits your needs. See the [custom_cache example](https:/vimeo/vimeo.php/blob/master/example/upload_image.php).

### Upload images

To upload an image, call the `uploadImage` method. It takes three parameters.
Expand Down
80 changes: 80 additions & 0 deletions example/upload_custom_cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
use TusPhp\Tus\Client;
use Vimeo\Upload\TusClient;
use Vimeo\Upload\TusClientFactory;
use Vimeo\Vimeo;
use Vimeo\Exceptions\VimeoUploadException;

/**
* Copyright 2022 Vimeo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
$config = require(__DIR__ . '/init.php');

class LocalCacheTusClientFactory extends TusClientFactory
{
public function getTusClient(string $base_uri, string $url): Client
{
$client = new TusClient($base_uri);
$client->setUrl($url);
$client->setCache(new \TusPhp\Cache\FileStore('./cache'));
return $client;
}
}

if (empty($config['access_token'])) {
throw new Exception(
'You can not upload a file without an access token. You can find this token on your app page, or generate ' .
'one using `auth.php`.'
);
}

// Instantiate the library with your client id, secret and access token (pulled from dev site), pass in the TusClientFactory
$lib = new Vimeo($config['client_id'], $config['client_secret'], $config['access_token'], new LocalCacheTusClientFactory());

// Create a variable with a hard coded path to your file system
$file_name = '<fully qualified path>';

echo 'Uploading: ' . $file_name . "\n";

try {
// Upload the file and include the video title and description.
$uri = $lib->upload($file_name, array(
'name' => 'Vimeo API SDK test upload',
'description' => "This video was uploaded through the Vimeo API's PHP SDK."
));

// Get the metadata response from the upload and log out the Vimeo.com url
$video_data = $lib->request($uri . '?fields=link');
echo '"' . $file_name . ' has been uploaded to ' . $video_data['body']['link'] . "\n";

// Make an API call to edit the title and description of the video.
$lib->request($uri, array(
'name' => 'Vimeo API SDK test edit',
'description' => "This video was edited through the Vimeo API's PHP SDK.",
), 'PATCH');

echo 'The title and description for ' . $uri . ' has been edited.' . "\n";

// Make an API call to see if the video is finished transcoding.
$video_data = $lib->request($uri . '?fields=transcode.status');
echo 'The transcode status for ' . $uri . ' is: ' . $video_data['body']['transcode']['status'] . "\n";
} catch (VimeoUploadException $e) {
// We may have had an error. We can't resolve it here necessarily, so report it to the user.
echo 'Error uploading ' . $file_name . "\n";
echo 'Server reported: ' . $e->getMessage() . "\n";
} catch (VimeoRequestException $e) {
echo 'There was an error making the request.' . "\n";
echo 'Server reported: ' . $e->getMessage() . "\n";
}
20 changes: 20 additions & 0 deletions src/Vimeo/Upload/TusClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Vimeo\Upload;

use TusPhp\Tus\Client;

class TusClientFactory
{
/**
* @param string $base_uri The fully qualified domain of the upload, ex: https://us-files.tus.vimeo.com
* @param string $url The fully qualified url of the upload, ex: https://us-files.tus.vimeo.com/files/vimeo-a1b2c3d4
* @return Client
*/
public function getTusClient(string $base_uri, string $url) : Client
{
$client = new TusClient($base_uri);
$client->setUrl($url);
return $client;
}
}
12 changes: 8 additions & 4 deletions src/Vimeo/Vimeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Vimeo\Exceptions\VimeoException;
use Vimeo\Exceptions\VimeoRequestException;
use Vimeo\Exceptions\VimeoUploadException;
use Vimeo\Upload\TusClient;
use Vimeo\Upload\TusClientFactory;

/**
* Copyright 2013 Vimeo
Expand Down Expand Up @@ -52,14 +52,18 @@ class Vimeo
/** @var null|string */
private $_access_token = null;

/** @var TusClientFactory */
private $_tus_client_factory = null;

/**
* Creates the Vimeo library, and tracks the client and token information.
*
* @param string $client_id Your applications client id. Can be found on developer.vimeo.com/apps
* @param string $client_secret Your applications client secret. Can be found on developer.vimeo.com/apps
* @param string|null $access_token Your access token. Can be found on developer.vimeo.com/apps or generated using OAuth 2.
* @param TusClientFactory|null $tus_client_interface Your tus client that will be used.
*/
public function __construct(string $client_id, string $client_secret, string $access_token = null)
public function __construct(string $client_id, string $client_secret, string $access_token = null, TusClientFactory $tus_client_factory = null)
{
$this->_client_id = $client_id;
$this->_client_secret = $client_secret;
Expand All @@ -72,6 +76,7 @@ public function __construct(string $client_id, string $client_secret, string $ac
//Certificate must indicate that the server is the server to which you meant to connect.
CURLOPT_SSL_VERIFYHOST => 2,
);
$this->_tus_client_factory = $tus_client_factory ?? new TusClientFactory();
}

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

$client = new TusClient($base_url);
$client = $this->_tus_client_factory->getTusClient($base_url, $url);
$client->setApiPath($api_path);
$client->setKey($key)->file($file_path);
$client->setUrl($url);
$client->getCache()->set($client->getKey(),[
'location' => $url,
'expires_at' => Carbon::now()->addSeconds($client->getCache()->getTtl())->format($client->getCache()::RFC_7231),
Expand Down

0 comments on commit 663614c

Please sign in to comment.