55use Vimeo \Exceptions \VimeoException ;
66use Vimeo \Exceptions \VimeoRequestException ;
77use Vimeo \Exceptions \VimeoUploadException ;
8- use Vimeo \Upload \TusClientFactory ;
8+ use Vimeo \Upload \TusClient ;
99
1010/**
1111 * Copyright 2013 Vimeo
@@ -36,6 +36,7 @@ class Vimeo
3636 const VERSIONS_ENDPOINT = '/versions ' ;
3737 const VERSION_STRING = 'application/vnd.vimeo.*+json; version=3.4 ' ;
3838 const USER_AGENT = 'vimeo.php 3.0.8; (http://developer.vimeo.com/api/docs) ' ;
39+ const MAX_BACKOFF = 8 ;
3940
4041 /** @var array */
4142 protected $ _curl_opts = array ();
@@ -52,18 +53,14 @@ class Vimeo
5253 /** @var null|string */
5354 private $ _access_token = null ;
5455
55- /** @var TusClientFactory */
56- private $ _tus_client_factory = null ;
57-
5856 /**
5957 * Creates the Vimeo library, and tracks the client and token information.
6058 *
6159 * @param string $client_id Your applications client id. Can be found on developer.vimeo.com/apps
6260 * @param string $client_secret Your applications client secret. Can be found on developer.vimeo.com/apps
6361 * @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.
6562 */
66- public function __construct (string $ client_id , string $ client_secret , ?string $ access_token = null , ? TusClientFactory $ tus_client_factory = null )
63+ public function __construct (string $ client_id , string $ client_secret , ?string $ access_token = null )
6764 {
6865 $ this ->_client_id = $ client_id ;
6966 $ this ->_client_secret = $ client_secret ;
@@ -76,7 +73,6 @@ public function __construct(string $client_id, string $client_secret, ?string $a
7673 //Certificate must indicate that the server is the server to which you meant to connect.
7774 CURLOPT_SSL_VERIFYHOST => 2 ,
7875 );
79- $ this ->_tus_client_factory = $ tus_client_factory ?? new TusClientFactory ();
8076 }
8177
8278 /**
@@ -503,7 +499,7 @@ public function uploadTexttrack($texttracks_uri, $file_path, $track_type, $langu
503499
504500 curl_close ($ curl );
505501 fclose ($ handle );
506-
502+
507503 if ($ curl_info ['http_code ' ] !== 200 ) {
508504 throw new VimeoUploadException ($ response );
509505 }
@@ -590,37 +586,31 @@ private function perform_upload_tus(string $file_path, $file_size, array $attemp
590586 }
591587
592588 $ url = $ attempt ['body ' ]['upload ' ]['upload_link ' ];
593- $ url_path = parse_url ($ url )['path ' ];
594-
595- $ base_url = str_replace ($ url_path , '' , $ url );
596- $ api_path = $ url_path ;
597- $ api_pathp = explode ('/ ' , $ api_path );
598- $ key = $ api_pathp [count ($ api_pathp ) - 1 ];
599- $ api_path = str_replace ('/ ' . $ key , '' , $ api_path );
600589
601590 $ bytes_uploaded = 0 ;
602591 $ failures = 0 ;
603592
604- $ client = $ this ->_tus_client_factory ->getTusClient ($ base_url , $ url );
605- $ client ->setApiPath ($ api_path );
606- $ client ->setKey ($ key )->file ($ file_path );
607- $ client ->getCache ()->set ($ client ->getKey (),[
608- 'location ' => $ url ,
609- 'expires_at ' => Carbon::now ()->addSeconds ($ client ->getCache ()->getTtl ())->format ($ client ->getCache ()::RFC_7231 ),
610- ]);
593+ $ tus_client = new TusClient ($ url , $ file_path );
611594
612595 do {
613596 try {
614- $ bytes_uploaded = $ client ->upload ($ chunk_size );
597+ $ bytes_uploaded = $ tus_client ->upload ($ chunk_size );
615598 } catch (\Exception $ e ) {
616- // We likely experienced a timeout, but if we experience three in a row, then we should back off and
617- // fail so as to not overwhelm servers that are, probably, down.
618- if ($ failures >= 3 ) {
599+
600+ if ($ e instanceof VimeoUploadException && !$ e ->isRetryable ()) {
601+ throw $ e ;
602+ }
603+ // Maximum retry limit of 10. Will retry for timeouts or connection errors.
604+ if ($ failures >= 10 ) {
619605 throw new VimeoUploadException ($ e ->getMessage ());
620606 }
621607
622608 $ failures ++;
623- sleep ((int )pow (4 , $ failures )); // sleep 4, 16, 64 seconds (based on failure count)
609+ $ backoff = (int )pow (2 , $ failures - 1 );
610+ if ($ backoff > self ::MAX_BACKOFF ) {
611+ $ backoff = self ::MAX_BACKOFF ;
612+ }
613+ sleep ($ backoff ); // sleep 2, 4, 8, 8... seconds (based on failure count)
624614 }
625615 } while ($ bytes_uploaded < $ file_size );
626616
0 commit comments