Skip to content

Commit 58d8093

Browse files
authored
Merge pull request #342 from aspaseltiner-vimeo/patch-1
perform_tus_upload: allow overriding default per-PATCH chunk size
2 parents d3c49b7 + 3764690 commit 58d8093

1 file changed

Lines changed: 12 additions & 29 deletions

File tree

src/Vimeo/Vimeo.php

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,12 @@ public function buildAuthorizationEndpoint($redirect_uri, $scope = 'public', $st
314314
* @link https://developer.vimeo.com/api/endpoints/videos#POST/users/{user_id}/videos
315315
* @param string $file_path Path to the video file to upload.
316316
* @param array $params Parameters to send when creating a new video (name, privacy restrictions, etc.).
317+
* @param int|null $override_chunk_size Optionally override the default chunk size of 100MB.
317318
* @return string Video URI
318319
* @throws VimeoRequestException
319320
* @throws VimeoUploadException
320321
*/
321-
public function upload($file_path, array $params = array())
322+
public function upload($file_path, array $params = array(), ?int $override_chunk_size = null)
322323
{
323324
// Validate that our file is real.
324325
if (!is_file($file_path)) {
@@ -340,7 +341,7 @@ public function upload($file_path, array $params = array())
340341
throw new VimeoUploadException('Unable to initiate an upload.' . $attempt_error);
341342
}
342343

343-
return $this->perform_upload_tus($file_path, $file_size, $attempt);
344+
return $this->perform_upload_tus($file_path, $file_size, $attempt, $override_chunk_size);
344345
}
345346

346347
/**
@@ -349,11 +350,12 @@ public function upload($file_path, array $params = array())
349350
* @link https://developer.vimeo.com/api/endpoints/videos#POST/videos/{video_id}/versions
350351
* @param string $video_uri Video uri of the video file to replace.
351352
* @param string $file_path Path to the video file to upload.
353+
* @param int|null $override_chunk_size Optionally override the default chunk size of 100MB.
352354
* @return string Video URI
353355
* @throws VimeoRequestException
354356
* @throws VimeoUploadException
355357
*/
356-
public function replace($video_uri, $file_path, array $params = array())
358+
public function replace($video_uri, $file_path, array $params = array(), ?int $override_chunk_size = null)
357359
{
358360
// Validate that our file is real.
359361
if (!is_file($file_path)) {
@@ -379,7 +381,7 @@ public function replace($video_uri, $file_path, array $params = array())
379381
// `uri` doesn't come back from `/videos/:id/versions` so we need to manually set it here for uploading.
380382
$attempt['body']['uri'] = $video_uri;
381383

382-
return $this->perform_upload_tus($file_path, $file_size, $attempt);
384+
return $this->perform_upload_tus($file_path, $file_size, $attempt, $override_chunk_size);
383385
}
384386

385387
/**
@@ -576,12 +578,16 @@ private function _authHeader(): string
576578
* @param string $file_path Path to the video file to upload.
577579
* @param int|float $file_size Size of the video file.
578580
* @param array $attempt Upload attempt data.
581+
* @param int|null $override_chunk_size Optionally override the default chunk size of 100MB.
579582
* @return string
580583
* @throws VimeoUploadException
581584
*/
582-
private function perform_upload_tus(string $file_path, $file_size, array $attempt): string
585+
private function perform_upload_tus(string $file_path, $file_size, array $attempt, ?int $override_chunk_size = null): string
583586
{
584-
$default_chunk_size = (100 * 1024 * 1024); // 100 MB
587+
$chunk_size = (100 * 1024 * 1024); // 100 MB
588+
if ($override_chunk_size) {
589+
$chunk_size = $override_chunk_size;
590+
}
585591

586592
$url = $attempt['body']['upload']['upload_link'];
587593
$url_path = parse_url($url)['path'];
@@ -594,7 +600,6 @@ private function perform_upload_tus(string $file_path, $file_size, array $attemp
594600

595601
$bytes_uploaded = 0;
596602
$failures = 0;
597-
$chunk_size = $this->getTusUploadChunkSize($default_chunk_size, (int)$file_size);
598603

599604
$client = $this->_tus_client_factory->getTusClient($base_url, $url);
600605
$client->setApiPath($api_path);
@@ -621,26 +626,4 @@ private function perform_upload_tus(string $file_path, $file_size, array $attemp
621626

622627
return $attempt['body']['uri'];
623628
}
624-
625-
/**
626-
* Enforces the notion that a user may supply any `proposed_chunk_size`, as long as it results in 1024 or less
627-
* proposed chunks. In the event it does not, then the chunk size becomes the file size divided by 1024.
628-
*
629-
* @param int $proposed_chunk_size
630-
* @param int $file_size
631-
* @return int
632-
*/
633-
private function getTusUploadChunkSize(int $proposed_chunk_size, int $file_size): int
634-
{
635-
$proposed_chunk_size = ($proposed_chunk_size <= 0) ? 1 : $proposed_chunk_size;
636-
$chunks = floor($file_size / $proposed_chunk_size);
637-
$divides_evenly = $file_size % $proposed_chunk_size === 0;
638-
$number_of_chunks_proposed = ($divides_evenly) ? $chunks : $chunks + 1;
639-
640-
if ($number_of_chunks_proposed > 1024) {
641-
return (int)floor($file_size / 1024) + 1;
642-
}
643-
644-
return $proposed_chunk_size;
645-
}
646629
}

0 commit comments

Comments
 (0)