-
Notifications
You must be signed in to change notification settings - Fork 210
/
upload_custom_cache.php
80 lines (69 loc) · 3.11 KB
/
upload_custom_cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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";
}