From b2d70f65b3ec04205eb09e09599a06da04186f83 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Mon, 17 Apr 2023 11:25:36 -0400 Subject: [PATCH 01/31] multipart uploader method --- src/Multipart/AbstractUploader.php | 7 +++++++ src/Multipart/UploadState.php | 10 ++++++++- src/S3/MultipartUploader.php | 33 ++++++++++++++++++++++++++++++ src/S3/MultipartUploadingTrait.php | 7 +++++++ src/S3/ObjectUploader.php | 18 ++++++++++++++-- 5 files changed, 72 insertions(+), 3 deletions(-) diff --git a/src/Multipart/AbstractUploader.php b/src/Multipart/AbstractUploader.php index 75e6794660..7d905d938f 100644 --- a/src/Multipart/AbstractUploader.php +++ b/src/Multipart/AbstractUploader.php @@ -60,6 +60,8 @@ protected function getUploadCommands(callable $resultHandler) ); $command->getHandlerList()->appendSign($resultHandler, 'mup'); $numberOfParts = $this->getNumberOfParts($this->state->getPartSize()); +////prints multiple times bc it's inside the if statement-------------------------------------------------------------------------------------------------- +// echo __METHOD__ . " | Calculating # of parts: " . $numberOfParts . "\n"; if (isset($numberOfParts) && $partNumber > $numberOfParts) { throw new $this->config['exception_class']( $this->state, @@ -87,6 +89,8 @@ protected function getUploadCommands(callable $resultHandler) $this->source->read($this->state->getPartSize()); } } +//prints near the end of the handleResult print statements? Maybe the thing about "Or do we just create parts til we reach the end of the file?"------------------------------------------------------------------------------------------------------------- +// print "AbstractUploader Number of parts: " . $numberOfParts . "\n"; } /** @@ -147,4 +151,7 @@ protected function getNumberOfParts($partSize) } return null; } + } + + diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 4108c4f13b..aa9334d902 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -97,6 +97,7 @@ public function markPartAsUploaded($partNumber, array $partData = []) */ public function hasPartBeenUploaded($partNumber) { +// echo __METHOD__ . " | checking if uploaded: " . $partNumber . "\n"; return isset($this->uploadedParts[$partNumber]); } @@ -108,7 +109,6 @@ public function hasPartBeenUploaded($partNumber) public function getUploadedParts() { ksort($this->uploadedParts); - return $this->uploadedParts; } @@ -118,6 +118,14 @@ public function getUploadedParts() * @param int $status Status is an integer code defined by the constants * CREATED, INITIATED, and COMPLETED on this class. */ + +// public function updateProgressBar($contentLength) +// { +//// echo "part size " . $this->partSize . "\n"; +// +// echo "total uploaded: " . ($this->uploadedBytes += $contentLength) . "\n"; +// return array_shift($this->progressBar); +// } public function setStatus($status) { $this->status = $status; diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index ae47d7e5fd..eefe472d9b 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -19,6 +19,18 @@ class MultipartUploader extends AbstractUploader const PART_MIN_SIZE = 5242880; const PART_MAX_SIZE = 5368709120; const PART_MAX_NUM = 10000; + private $uploadedBytes = 0; + private $progressBar = [ + "Transfer initiated...\n| | 0.0%\n", + "|== | 12.5%\n", + "|===== | 25.0%\n", + "|======= | 37.5%\n", + "|========== | 50.0%\n", + "|============ | 62.5%\n", + "|=============== | 75.0%\n", + "|================= | 87.5%\n", + "|====================| 100.0%\nTransfer complete!\n" + ]; /** * Creates a multipart upload for an S3 object. @@ -70,6 +82,13 @@ public function __construct( 'key' => null, 'exception_class' => S3MultipartUploadException::class, ]); + $totalSize = $this->source->getSize(); + $this->progressThresholds = []; + for ($i=1;$i<=8;$i++) { + $this->progressThresholds []= round($totalSize*($i/8)); + } + print_r($this->progressThresholds); + echo array_shift($this->progressBar); } protected function loadUploadWorkflowInfo() @@ -134,10 +153,24 @@ protected function createPart($seekable, $number) } $data['ContentLength'] = $contentLength; +// echo $data['ContentLength']; + $this->uploadedBytes += $contentLength; + $this->displayProgress($contentLength); return $data; } + protected function displayProgress($length) + { + $threshold = $this->progressThresholds; + + if (!empty($threshold) and !empty($this->progressBar) and $this->uploadedBytes >= $threshold[0]) { + echo $this->uploadedBytes . " is larger than or = to " . $threshold[0] . "\n"; + array_shift($this->progressThresholds); + echo array_shift($this->progressBar); + } + } + protected function extractETag(ResultInterface $result) { return $result['ETag']; diff --git a/src/S3/MultipartUploadingTrait.php b/src/S3/MultipartUploadingTrait.php index baccf58c51..2cc8f23612 100644 --- a/src/S3/MultipartUploadingTrait.php +++ b/src/S3/MultipartUploadingTrait.php @@ -55,8 +55,15 @@ protected function handleResult(CommandInterface $command, ResultInterface $resu 'PartNumber' => $command['PartNumber'], 'ETag' => $this->extractETag($result), ]); +// $progressResult = $this->getState()->updateProgressBar($command["ContentLength"]); +// echo $progressResult; } + protected function displayProgress(CommandInterface $command) + { + $progressResult = $this->getState()->updateProgressBar($command["ContentLength"]); + return $progressResult; + } abstract protected function extractETag(ResultInterface $result); protected function getCompleteParams() diff --git a/src/S3/ObjectUploader.php b/src/S3/ObjectUploader.php index 4bbc583a71..2cbf018226 100644 --- a/src/S3/ObjectUploader.php +++ b/src/S3/ObjectUploader.php @@ -63,6 +63,12 @@ public function __construct( // Handle "add_content_md5" option. $this->addContentMD5 = isset($options['add_content_md5']) && $options['add_content_md5'] === true; + /*$totalSize = $this->body->getSize(); + $this->progressThresholds = []; + for ($i=1;$i<=8;$i++) { + $this->progressThresholds []= round($totalSize*($i/8)); + } + print_r($this->progressThresholds);*/ } /** @@ -74,6 +80,7 @@ public function promise() $mup_threshold = $this->options['mup_threshold']; if ($this->requiresMultipart($this->body, $mup_threshold)) { // Perform a multipart upload. + echo "Total bytes: " . $this->body->getSize() . "\n"; return (new MultipartUploader($this->client, $this->body, [ 'bucket' => $this->bucket, 'key' => $this->key, @@ -92,12 +99,19 @@ public function promise() if (is_callable($this->options['before_upload'])) { $this->options['before_upload']($command); } - return $this->client->executeAsync($command); +// return $this->client->executeAsync($command); + $test = $this->client->executeAsync($command); + return $test; + } public function upload() { - return $this->promise()->wait(); + $result = $this->promise()->wait(); +// if ($result["@metadata"]["statusCode"] == '200') { +// echo "|====================| 100.0%\nTransfer complete!\n"; +// } + return $result; } /** From 51f757452069638583ec5eb7561298c2045598eb Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Mon, 17 Apr 2023 12:14:29 -0400 Subject: [PATCH 02/31] update displayProgress method --- src/S3/MultipartUploader.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index eefe472d9b..a4e77603b1 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -153,14 +153,13 @@ protected function createPart($seekable, $number) } $data['ContentLength'] = $contentLength; -// echo $data['ContentLength']; $this->uploadedBytes += $contentLength; - $this->displayProgress($contentLength); + $this->displayProgress(); return $data; } - protected function displayProgress($length) + protected function displayProgress() { $threshold = $this->progressThresholds; From 5cb12d5128908c9018b898dc54df0dc5c507f7fa Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Mon, 17 Apr 2023 12:47:56 -0400 Subject: [PATCH 03/31] removed comments --- src/Multipart/AbstractUploader.php | 2 -- src/Multipart/UploadState.php | 7 ------- src/S3/MultipartUploader.php | 2 -- src/S3/MultipartUploadingTrait.php | 7 ------- src/S3/ObjectUploader.php | 10 ---------- 5 files changed, 28 deletions(-) diff --git a/src/Multipart/AbstractUploader.php b/src/Multipart/AbstractUploader.php index 7d905d938f..848bb6b5ca 100644 --- a/src/Multipart/AbstractUploader.php +++ b/src/Multipart/AbstractUploader.php @@ -60,8 +60,6 @@ protected function getUploadCommands(callable $resultHandler) ); $command->getHandlerList()->appendSign($resultHandler, 'mup'); $numberOfParts = $this->getNumberOfParts($this->state->getPartSize()); -////prints multiple times bc it's inside the if statement-------------------------------------------------------------------------------------------------- -// echo __METHOD__ . " | Calculating # of parts: " . $numberOfParts . "\n"; if (isset($numberOfParts) && $partNumber > $numberOfParts) { throw new $this->config['exception_class']( $this->state, diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index aa9334d902..105ffab24b 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -119,13 +119,6 @@ public function getUploadedParts() * CREATED, INITIATED, and COMPLETED on this class. */ -// public function updateProgressBar($contentLength) -// { -//// echo "part size " . $this->partSize . "\n"; -// -// echo "total uploaded: " . ($this->uploadedBytes += $contentLength) . "\n"; -// return array_shift($this->progressBar); -// } public function setStatus($status) { $this->status = $status; diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index a4e77603b1..90469a1000 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -87,7 +87,6 @@ public function __construct( for ($i=1;$i<=8;$i++) { $this->progressThresholds []= round($totalSize*($i/8)); } - print_r($this->progressThresholds); echo array_shift($this->progressBar); } @@ -164,7 +163,6 @@ protected function displayProgress() $threshold = $this->progressThresholds; if (!empty($threshold) and !empty($this->progressBar) and $this->uploadedBytes >= $threshold[0]) { - echo $this->uploadedBytes . " is larger than or = to " . $threshold[0] . "\n"; array_shift($this->progressThresholds); echo array_shift($this->progressBar); } diff --git a/src/S3/MultipartUploadingTrait.php b/src/S3/MultipartUploadingTrait.php index 2cc8f23612..baccf58c51 100644 --- a/src/S3/MultipartUploadingTrait.php +++ b/src/S3/MultipartUploadingTrait.php @@ -55,15 +55,8 @@ protected function handleResult(CommandInterface $command, ResultInterface $resu 'PartNumber' => $command['PartNumber'], 'ETag' => $this->extractETag($result), ]); -// $progressResult = $this->getState()->updateProgressBar($command["ContentLength"]); -// echo $progressResult; } - protected function displayProgress(CommandInterface $command) - { - $progressResult = $this->getState()->updateProgressBar($command["ContentLength"]); - return $progressResult; - } abstract protected function extractETag(ResultInterface $result); protected function getCompleteParams() diff --git a/src/S3/ObjectUploader.php b/src/S3/ObjectUploader.php index 2cbf018226..2b899b97a3 100644 --- a/src/S3/ObjectUploader.php +++ b/src/S3/ObjectUploader.php @@ -63,12 +63,6 @@ public function __construct( // Handle "add_content_md5" option. $this->addContentMD5 = isset($options['add_content_md5']) && $options['add_content_md5'] === true; - /*$totalSize = $this->body->getSize(); - $this->progressThresholds = []; - for ($i=1;$i<=8;$i++) { - $this->progressThresholds []= round($totalSize*($i/8)); - } - print_r($this->progressThresholds);*/ } /** @@ -80,7 +74,6 @@ public function promise() $mup_threshold = $this->options['mup_threshold']; if ($this->requiresMultipart($this->body, $mup_threshold)) { // Perform a multipart upload. - echo "Total bytes: " . $this->body->getSize() . "\n"; return (new MultipartUploader($this->client, $this->body, [ 'bucket' => $this->bucket, 'key' => $this->key, @@ -108,9 +101,6 @@ public function promise() public function upload() { $result = $this->promise()->wait(); -// if ($result["@metadata"]["statusCode"] == '200') { -// echo "|====================| 100.0%\nTransfer complete!\n"; -// } return $result; } From ebd06c4da9f2fcfd1b877da0fe6238b50311e670 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Tue, 18 Apr 2023 23:39:01 -0400 Subject: [PATCH 04/31] added ability to display progressBar for MultipartUpload and MultipartCopy --- src/Multipart/AbstractUploader.php | 2 -- src/Multipart/UploadState.php | 29 +++++++++++++++++++++++++++ src/S3/MultipartCopy.php | 2 ++ src/S3/MultipartUploader.php | 32 ++---------------------------- src/S3/MultipartUploadingTrait.php | 14 +++++++++++++ src/S3/ObjectUploader.php | 8 ++------ 6 files changed, 49 insertions(+), 38 deletions(-) diff --git a/src/Multipart/AbstractUploader.php b/src/Multipart/AbstractUploader.php index 848bb6b5ca..dc1724f7a0 100644 --- a/src/Multipart/AbstractUploader.php +++ b/src/Multipart/AbstractUploader.php @@ -87,8 +87,6 @@ protected function getUploadCommands(callable $resultHandler) $this->source->read($this->state->getPartSize()); } } -//prints near the end of the handleResult print statements? Maybe the thing about "Or do we just create parts til we reach the end of the file?"------------------------------------------------------------------------------------------------------------- -// print "AbstractUploader Number of parts: " . $numberOfParts . "\n"; } /** diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 105ffab24b..807a685521 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -13,6 +13,18 @@ class UploadState const INITIATED = 1; const COMPLETED = 2; + protected $progressBar = [ + "Transfer initiated...\n| | 0.0%\n", + "|== | 12.5%\n", + "|===== | 25.0%\n", + "|======= | 37.5%\n", + "|========== | 50.0%\n", + "|============ | 62.5%\n", + "|=============== | 75.0%\n", + "|================= | 87.5%\n", + "|====================| 100.0%\nTransfer complete!\n" + ]; + /** @var array Params used to identity the upload. */ private $id; @@ -31,6 +43,7 @@ class UploadState public function __construct(array $id) { $this->id = $id; + echo array_shift($this->progressBar); } /** @@ -76,6 +89,22 @@ public function setPartSize($partSize) $this->partSize = $partSize; } + public function setProgressThresholds($thresholds) + { + $this->progressThresholds = $thresholds; + } + + public function displayProgress($totalUploaded) + { + while (!empty($this->progressThresholds) + && !empty($this->progressBar) + && $totalUploaded >= $this->progressThresholds[0]) + { + array_shift($this->progressThresholds); + echo array_shift($this->progressBar); + } + } + /** * Marks a part as being uploaded. * diff --git a/src/S3/MultipartCopy.php b/src/S3/MultipartCopy.php index 5b26dea79e..7383910d3b 100644 --- a/src/S3/MultipartCopy.php +++ b/src/S3/MultipartCopy.php @@ -75,6 +75,8 @@ public function __construct( $client, array_change_key_case($config) + ['source_metadata' => null] ); + + $this->createProgressThresholds($this->sourceMetadata["ContentLength"]); } /** diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index 90469a1000..0c9a49bf1c 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -19,18 +19,6 @@ class MultipartUploader extends AbstractUploader const PART_MIN_SIZE = 5242880; const PART_MAX_SIZE = 5368709120; const PART_MAX_NUM = 10000; - private $uploadedBytes = 0; - private $progressBar = [ - "Transfer initiated...\n| | 0.0%\n", - "|== | 12.5%\n", - "|===== | 25.0%\n", - "|======= | 37.5%\n", - "|========== | 50.0%\n", - "|============ | 62.5%\n", - "|=============== | 75.0%\n", - "|================= | 87.5%\n", - "|====================| 100.0%\nTransfer complete!\n" - ]; /** * Creates a multipart upload for an S3 object. @@ -82,12 +70,8 @@ public function __construct( 'key' => null, 'exception_class' => S3MultipartUploadException::class, ]); - $totalSize = $this->source->getSize(); - $this->progressThresholds = []; - for ($i=1;$i<=8;$i++) { - $this->progressThresholds []= round($totalSize*($i/8)); - } - echo array_shift($this->progressBar); + + $this->createProgressThresholds($this->source->getSize()); } protected function loadUploadWorkflowInfo() @@ -152,22 +136,10 @@ protected function createPart($seekable, $number) } $data['ContentLength'] = $contentLength; - $this->uploadedBytes += $contentLength; - $this->displayProgress(); return $data; } - protected function displayProgress() - { - $threshold = $this->progressThresholds; - - if (!empty($threshold) and !empty($this->progressBar) and $this->uploadedBytes >= $threshold[0]) { - array_shift($this->progressThresholds); - echo array_shift($this->progressBar); - } - } - protected function extractETag(ResultInterface $result) { return $result['ETag']; diff --git a/src/S3/MultipartUploadingTrait.php b/src/S3/MultipartUploadingTrait.php index baccf58c51..39d9e9917d 100644 --- a/src/S3/MultipartUploadingTrait.php +++ b/src/S3/MultipartUploadingTrait.php @@ -7,6 +7,8 @@ trait MultipartUploadingTrait { + private $uploadedBytes = 0; + /** * Creates an UploadState object for a multipart upload by querying the * service for the specified upload's information. @@ -49,12 +51,24 @@ public static function getStateFromService( return $state; } + protected function createProgressThresholds($totalSize) + { + $this->progressThresholds = []; + for ($i=1;$i<=8;$i++) { + $this->progressThresholds []= round($totalSize*($i/8)); + } + $this->getState()->setProgressThresholds($this->progressThresholds); + } + protected function handleResult(CommandInterface $command, ResultInterface $result) { $this->getState()->markPartAsUploaded($command['PartNumber'], [ 'PartNumber' => $command['PartNumber'], 'ETag' => $this->extractETag($result), ]); + + $this->uploadedBytes += $command["ContentLength"]; + $this->getState()->displayProgress($this->uploadedBytes); } abstract protected function extractETag(ResultInterface $result); diff --git a/src/S3/ObjectUploader.php b/src/S3/ObjectUploader.php index 2b899b97a3..4bbc583a71 100644 --- a/src/S3/ObjectUploader.php +++ b/src/S3/ObjectUploader.php @@ -92,16 +92,12 @@ public function promise() if (is_callable($this->options['before_upload'])) { $this->options['before_upload']($command); } -// return $this->client->executeAsync($command); - $test = $this->client->executeAsync($command); - return $test; - + return $this->client->executeAsync($command); } public function upload() { - $result = $this->promise()->wait(); - return $result; + return $this->promise()->wait(); } /** From 6dc3597ceb3cfa5ed20e5fabc39ef6ccffd93d4b Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Tue, 18 Apr 2023 23:46:43 -0400 Subject: [PATCH 05/31] Update UploadState.php --- src/Multipart/UploadState.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 807a685521..179d8d1018 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -126,7 +126,6 @@ public function markPartAsUploaded($partNumber, array $partData = []) */ public function hasPartBeenUploaded($partNumber) { -// echo __METHOD__ . " | checking if uploaded: " . $partNumber . "\n"; return isset($this->uploadedParts[$partNumber]); } From 7f05ff1c527ca0eff59757299b3a859d7509b19a Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Wed, 19 Apr 2023 15:18:37 -0400 Subject: [PATCH 06/31] Update AbstractUploader.php --- src/Multipart/AbstractUploader.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Multipart/AbstractUploader.php b/src/Multipart/AbstractUploader.php index dc1724f7a0..f0fd6e56e6 100644 --- a/src/Multipart/AbstractUploader.php +++ b/src/Multipart/AbstractUploader.php @@ -147,7 +147,4 @@ protected function getNumberOfParts($partSize) } return null; } - -} - - +} \ No newline at end of file From 657ab9a242f71760c10d4eae48a0f609e337aaf2 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Wed, 19 Apr 2023 18:17:11 -0400 Subject: [PATCH 07/31] MultipartUploader and MultipartCopy now sends total size directly to UploadState --- src/Multipart/UploadState.php | 8 ++++++-- src/S3/MultipartCopy.php | 4 +++- src/S3/MultipartUploader.php | 2 +- src/S3/MultipartUploadingTrait.php | 9 --------- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 179d8d1018..0339929cfa 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -37,6 +37,8 @@ class UploadState /** @var int Identifies the status the upload. */ private $status = self::CREATED; + private $progressThresholds = []; + /** * @param array $id Params used to identity the upload. */ @@ -89,9 +91,11 @@ public function setPartSize($partSize) $this->partSize = $partSize; } - public function setProgressThresholds($thresholds) + public function setProgressThresholds($totalSize) { - $this->progressThresholds = $thresholds; + for ($i=1;$i<=8;$i++) { + $this->progressThresholds []= round($totalSize*($i/8)); + } } public function displayProgress($totalUploaded) diff --git a/src/S3/MultipartCopy.php b/src/S3/MultipartCopy.php index 7383910d3b..00e4d73b02 100644 --- a/src/S3/MultipartCopy.php +++ b/src/S3/MultipartCopy.php @@ -76,7 +76,9 @@ public function __construct( array_change_key_case($config) + ['source_metadata' => null] ); - $this->createProgressThresholds($this->sourceMetadata["ContentLength"]); + $this->getState()->setProgressThresholds( + $this->sourceMetadata["ContentLength"] + ); } /** diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index 0c9a49bf1c..7570fa4538 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -71,7 +71,7 @@ public function __construct( 'exception_class' => S3MultipartUploadException::class, ]); - $this->createProgressThresholds($this->source->getSize()); + $this->getState()->setProgressThresholds($this->source->getSize()); } protected function loadUploadWorkflowInfo() diff --git a/src/S3/MultipartUploadingTrait.php b/src/S3/MultipartUploadingTrait.php index 39d9e9917d..9e1eedacc7 100644 --- a/src/S3/MultipartUploadingTrait.php +++ b/src/S3/MultipartUploadingTrait.php @@ -51,15 +51,6 @@ public static function getStateFromService( return $state; } - protected function createProgressThresholds($totalSize) - { - $this->progressThresholds = []; - for ($i=1;$i<=8;$i++) { - $this->progressThresholds []= round($totalSize*($i/8)); - } - $this->getState()->setProgressThresholds($this->progressThresholds); - } - protected function handleResult(CommandInterface $command, ResultInterface $result) { $this->getState()->markPartAsUploaded($command['PartNumber'], [ From 89c95138504d102f32092bc56129c81cad93edc2 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Mon, 24 Apr 2023 15:52:39 -0400 Subject: [PATCH 08/31] Update UploadStateTest.php --- tests/Multipart/UploadStateTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 64ac615f30..876ae94623 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -70,4 +70,17 @@ public function testSerializationWorks() $this->assertTrue($newState->isInitiated()); $this->assertArrayHasKey('foo', $newState->getId()); } + + public function testDisplayProgressPrints() + { + ob_start(); + + $state = new UploadState([]); + $state->setProgressThresholds(100); + $state->displayProgress(13); + + $output = ob_end_clean(); +// echo $output; + $this->assertEquals("Transfer initiated...\n| | 0.0%\n|== | 12.5%\n", $output); + } } From d68597cc81329197531e2d72373ef6af938c374b Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Mon, 24 Apr 2023 16:42:37 -0400 Subject: [PATCH 09/31] Added assert using expectOutputString and structure for data provider --- tests/Multipart/UploadStateTest.php | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 876ae94623..2ffd26db08 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -70,17 +70,29 @@ public function testSerializationWorks() $this->assertTrue($newState->isInitiated()); $this->assertArrayHasKey('foo', $newState->getId()); } - - public function testDisplayProgressPrints() + /** + * @dataProvider getDisplayProgressCases + */ + public function testDisplayProgressPrints($totalSize, $totalUploaded, $progressBar) { - ob_start(); +// ob_start(); $state = new UploadState([]); - $state->setProgressThresholds(100); - $state->displayProgress(13); + $state->setProgressThresholds($totalSize); + $state->displayProgress($totalUploaded); +// +// $output = ob_get_contents(); +// ob_end_clean(); +// $this->assertEquals($progressBar, $output); + $this->expectOutputString($progressBar); + } - $output = ob_end_clean(); -// echo $output; - $this->assertEquals("Transfer initiated...\n| | 0.0%\n|== | 12.5%\n", $output); + public function getDisplayProgressCases() + { + return [ + [100, 10, "Transfer initiated...\n| | 0.0%\n"], + [100, 0, "Transfer initiated...\n| | 0.0%\n"], + [100, 13, "Transfer initiated...\n| | 0.0%\n|== | 12.5%\n"] + ]; } } From 99738f88dd6ed6c17e0b2a9009265c2908224186 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Wed, 26 Apr 2023 12:47:45 -0400 Subject: [PATCH 10/31] added two tests --- src/Multipart/UploadState.php | 5 +-- tests/Multipart/UploadStateTest.php | 55 ++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 0339929cfa..d2f487711d 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -42,7 +42,7 @@ class UploadState /** * @param array $id Params used to identity the upload. */ - public function __construct(array $id) + public function __construct(array $id, $config=[]) { $this->id = $id; echo array_shift($this->progressBar); @@ -96,6 +96,7 @@ public function setProgressThresholds($totalSize) for ($i=1;$i<=8;$i++) { $this->progressThresholds []= round($totalSize*($i/8)); } + return $this->progressThresholds; } public function displayProgress($totalUploaded) @@ -175,4 +176,4 @@ public function isCompleted() { return $this->status === self::COMPLETED; } -} +} \ No newline at end of file diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 2ffd26db08..8886ad39c6 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -73,26 +73,57 @@ public function testSerializationWorks() /** * @dataProvider getDisplayProgressCases */ - public function testDisplayProgressPrints($totalSize, $totalUploaded, $progressBar) - { -// ob_start(); - + public function testDisplayProgressPrints( + $totalSize, + $totalUploaded, + $progressBar + ) { $state = new UploadState([]); $state->setProgressThresholds($totalSize); $state->displayProgress($totalUploaded); -// -// $output = ob_get_contents(); -// ob_end_clean(); -// $this->assertEquals($progressBar, $output); + $this->expectOutputString($progressBar); } public function getDisplayProgressCases() + { + $progressBar = [ + "Transfer initiated...\n| | 0.0%\n", + "|== | 12.5%\n", + "|===== | 25.0%\n", + "|======= | 37.5%\n", + "|========== | 50.0%\n", + "|============ | 62.5%\n", + "|=============== | 75.0%\n", + "|================= | 87.5%\n", + "|====================| 100.0%\nTransfer complete!\n" + ]; + return [ + [100000, 0, $progressBar[0]], + [100000, 12499, $progressBar[0]], + [100000, 12500, "$progressBar[0]$progressBar[1]"], + [100000, 100000, implode($progressBar)] + ]; + } + + /** + * @dataProvider getThresholdCases + */ + public function testUploadThresholds($totalSize) + { + $state = new UploadState([]); + $threshold = $state->setProgressThresholds($totalSize); + + $this->assertIsArray($threshold); + $this->assertCount(8, $threshold); + } + + public function getThresholdCases() { return [ - [100, 10, "Transfer initiated...\n| | 0.0%\n"], - [100, 0, "Transfer initiated...\n| | 0.0%\n"], - [100, 13, "Transfer initiated...\n| | 0.0%\n|== | 12.5%\n"] + [0], + [100000], + [100001] ]; } -} +} \ No newline at end of file From 604df58291a03d6adb5db041ff3a11077f019587 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Wed, 26 Apr 2023 12:48:37 -0400 Subject: [PATCH 11/31] Update UploadState.php --- src/Multipart/UploadState.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index d2f487711d..6786202c64 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -42,7 +42,7 @@ class UploadState /** * @param array $id Params used to identity the upload. */ - public function __construct(array $id, $config=[]) + public function __construct(array $id) { $this->id = $id; echo array_shift($this->progressBar); From 12e328d0d2488dbdb36285eb142570d739eed31a Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Fri, 28 Apr 2023 14:55:19 -0400 Subject: [PATCH 12/31] Update UploadStateTest.php The strings in the return statement were over the 80 char limit --- tests/Multipart/UploadStateTest.php | 77 +++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 14 deletions(-) diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 8886ad39c6..4439c0ebf6 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -73,7 +73,7 @@ public function testSerializationWorks() /** * @dataProvider getDisplayProgressCases */ - public function testDisplayProgressPrints( + public function testDisplayProgressPrintsProgress( $totalSize, $totalUploaded, $progressBar @@ -87,22 +87,71 @@ public function testDisplayProgressPrints( public function getDisplayProgressCases() { - $progressBar = [ - "Transfer initiated...\n| | 0.0%\n", - "|== | 12.5%\n", - "|===== | 25.0%\n", - "|======= | 37.5%\n", - "|========== | 50.0%\n", - "|============ | 62.5%\n", - "|=============== | 75.0%\n", - "|================= | 87.5%\n", - "|====================| 100.0%\nTransfer complete!\n" - ]; + $progressBar = array( + 0 => "Transfer initiated...\n| | 0.0%\n", + 12.5 => "Transfer initiated...\n| | 0.0%\n" . + "|== | 12.5%\n", + 25.0 => "Transfer initiated...\n| | 0.0%\n" . + "|== | 12.5%\n" . + "|===== | 25.0%\n", + 37.5 => "Transfer initiated...\n| | 0.0%\n" . + "|== | 12.5%\n" . + "|===== | 25.0%\n" . + "|======= | 37.5%\n", + 50.0 => "Transfer initiated...\n| | 0.0%\n" . + "|== | 12.5%\n" . + "|===== | 25.0%\n" . + "|======= | 37.5%\n" . + "|========== | 50.0%\n", + 62.5 => "Transfer initiated...\n| | 0.0%\n" . + "|== | 12.5%\n" . + "|===== | 25.0%\n" . + "|======= | 37.5%\n" . + "|========== | 50.0%\n" . + "|============ | 62.5%\n", + 75.0 => "Transfer initiated...\n| | 0.0%\n" . + "|== | 12.5%\n" . + "|===== | 25.0%\n" . + "|======= | 37.5%\n" . + "|========== | 50.0%\n" . + "|============ | 62.5%\n" . + "|=============== | 75.0%\n", + 87.5 => "Transfer initiated...\n| | 0.0%\n" . + "|== | 12.5%\n" . + "|===== | 25.0%\n" . + "|======= | 37.5%\n" . + "|========== | 50.0%\n" . + "|============ | 62.5%\n" . + "|=============== | 75.0%\n" . + "|================= | 87.5%\n", + 100 => "Transfer initiated...\n| | 0.0%\n" . + "|== | 12.5%\n" . + "|===== | 25.0%\n" . + "|======= | 37.5%\n" . + "|========== | 50.0%\n" . + "|============ | 62.5%\n" . + "|=============== | 75.0%\n" . + "|================= | 87.5%\n" . + "|====================| 100.0%\nTransfer complete!\n" + ); return [ [100000, 0, $progressBar[0]], [100000, 12499, $progressBar[0]], - [100000, 12500, "$progressBar[0]$progressBar[1]"], - [100000, 100000, implode($progressBar)] + [100000, 12500, $progressBar[12.5]], + [100000, 24999, $progressBar[12.5]], + [100000, 25000, $progressBar[25.0]], + [100000, 37499, $progressBar[25.0]], + [100000, 37500, $progressBar[37.5]], + [100000, 49999, $progressBar[37.5]], + [100000, 50000, $progressBar[50.0]], + [100000, 62499, $progressBar[50.0]], + [100000, 62500, $progressBar[62.5]], + [100000, 74999, $progressBar[62.5]], + [100000, 75000, $progressBar[75.0]], + [100000, 87499, $progressBar[75.0]], + [100000, 87500, $progressBar[87.5]], + [100000, 99999, $progressBar[87.5]], + [100000, 100000, $progressBar[100]] ]; } From e42ab910a92af06edb9b0af11346f311bf92e708 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Mon, 1 May 2023 12:57:19 -0400 Subject: [PATCH 13/31] Update UploadStateTest.php --- tests/Multipart/UploadStateTest.php | 132 +++++++++++++++------------- 1 file changed, 70 insertions(+), 62 deletions(-) diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 4439c0ebf6..d85afce867 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -87,71 +87,79 @@ public function testDisplayProgressPrintsProgress( public function getDisplayProgressCases() { - $progressBar = array( - 0 => "Transfer initiated...\n| | 0.0%\n", - 12.5 => "Transfer initiated...\n| | 0.0%\n" . - "|== | 12.5%\n", - 25.0 => "Transfer initiated...\n| | 0.0%\n" . - "|== | 12.5%\n" . - "|===== | 25.0%\n", - 37.5 => "Transfer initiated...\n| | 0.0%\n" . - "|== | 12.5%\n" . - "|===== | 25.0%\n" . - "|======= | 37.5%\n", - 50.0 => "Transfer initiated...\n| | 0.0%\n" . - "|== | 12.5%\n" . - "|===== | 25.0%\n" . - "|======= | 37.5%\n" . - "|========== | 50.0%\n", - 62.5 => "Transfer initiated...\n| | 0.0%\n" . - "|== | 12.5%\n" . - "|===== | 25.0%\n" . - "|======= | 37.5%\n" . - "|========== | 50.0%\n" . - "|============ | 62.5%\n", - 75.0 => "Transfer initiated...\n| | 0.0%\n" . - "|== | 12.5%\n" . - "|===== | 25.0%\n" . - "|======= | 37.5%\n" . - "|========== | 50.0%\n" . - "|============ | 62.5%\n" . - "|=============== | 75.0%\n", - 87.5 => "Transfer initiated...\n| | 0.0%\n" . - "|== | 12.5%\n" . - "|===== | 25.0%\n" . - "|======= | 37.5%\n" . - "|========== | 50.0%\n" . - "|============ | 62.5%\n" . - "|=============== | 75.0%\n" . - "|================= | 87.5%\n", - 100 => "Transfer initiated...\n| | 0.0%\n" . - "|== | 12.5%\n" . - "|===== | 25.0%\n" . - "|======= | 37.5%\n" . - "|========== | 50.0%\n" . - "|============ | 62.5%\n" . - "|=============== | 75.0%\n" . - "|================= | 87.5%\n" . - "|====================| 100.0%\nTransfer complete!\n" - ); + $progressBar = ["Transfer initiated...\n| | 0.0%\n", + "|== | 12.5%\n", + "|===== | 25.0%\n", + "|======= | 37.5%\n", + "|========== | 50.0%\n", + "|============ | 62.5%\n", + "|=============== | 75.0%\n", + "|================= | 87.5%\n", + "|====================| 100.0%\nTransfer complete!\n"]; return [ [100000, 0, $progressBar[0]], [100000, 12499, $progressBar[0]], - [100000, 12500, $progressBar[12.5]], - [100000, 24999, $progressBar[12.5]], - [100000, 25000, $progressBar[25.0]], - [100000, 37499, $progressBar[25.0]], - [100000, 37500, $progressBar[37.5]], - [100000, 49999, $progressBar[37.5]], - [100000, 50000, $progressBar[50.0]], - [100000, 62499, $progressBar[50.0]], - [100000, 62500, $progressBar[62.5]], - [100000, 74999, $progressBar[62.5]], - [100000, 75000, $progressBar[75.0]], - [100000, 87499, $progressBar[75.0]], - [100000, 87500, $progressBar[87.5]], - [100000, 99999, $progressBar[87.5]], - [100000, 100000, $progressBar[100]] + [100000, 12500, "{$progressBar[0]}{$progressBar[1]}"], + [100000, 24999, "{$progressBar[0]}{$progressBar[1]}"], + [100000, 25000, "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}"], + [100000, 37499, "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}"], + [ + 100000, + 37500, + "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}{$progressBar[3]}" + ], + [ + 100000, + 49999, + "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}{$progressBar[3]}" + ], + [ + 100000, + 50000, + "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}{$progressBar[3]}{$progressBar[4]}" + ], + [ + 100000, + 62499, + "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}{$progressBar[3]}{$progressBar[4]}" + ], + [ + 100000, + 62500, + "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}{$progressBar[3]}{$progressBar[4]}" . + "{$progressBar[5]}" + ], + [ + 100000, + 74999, + "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}{$progressBar[3]}{$progressBar[4]}" . + "{$progressBar[5]}" + ], + [ + 100000, + 75000, + "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}{$progressBar[3]}{$progressBar[4]}" . + "{$progressBar[5]}{$progressBar[6]}" + ], + [ + 100000, + 87499, + "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}{$progressBar[3]}{$progressBar[4]}" . + "{$progressBar[5]}{$progressBar[6]}" + ], + [ + 100000, + 87500, + "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}{$progressBar[3]}{$progressBar[4]}" . + "{$progressBar[5]}{$progressBar[6]}{$progressBar[7]}" + ], + [ + 100000, + 99999, + "{$progressBar[0]}{$progressBar[1]}{$progressBar[2]}{$progressBar[3]}{$progressBar[4]}" . + "{$progressBar[5]}{$progressBar[6]}{$progressBar[7]}" + ], + [100000, 100000, implode($progressBar)] ]; } From 470627a6309093098e77e5b17b926bf1117ad4c8 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Mon, 1 May 2023 15:20:04 -0400 Subject: [PATCH 14/31] Combine progressThresholds and progressBar into one array (1) Set progressThresholds[0] so that I could use array_combine on equal sized arrays (2) I redefined progressBar in setProgressThresholds after the threshold bar is created. progressBar includes the thresholds as keys and the bar as values (3) Removed the array_shift in the construct and now that logic is done by displayProgress (4) Changed assertCount to check for 9 elements instead of 8 --- src/Multipart/UploadState.php | 3 ++- tests/Multipart/UploadStateTest.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 6786202c64..8094cda24e 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -45,7 +45,6 @@ class UploadState public function __construct(array $id) { $this->id = $id; - echo array_shift($this->progressBar); } /** @@ -93,9 +92,11 @@ public function setPartSize($partSize) public function setProgressThresholds($totalSize) { + $this->progressThresholds[0] = 0; for ($i=1;$i<=8;$i++) { $this->progressThresholds []= round($totalSize*($i/8)); } + $this->progressBar = array_combine($this->progressThresholds, $this->progressBar); return $this->progressThresholds; } diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index d85afce867..85cd821f36 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -172,7 +172,7 @@ public function testUploadThresholds($totalSize) $threshold = $state->setProgressThresholds($totalSize); $this->assertIsArray($threshold); - $this->assertCount(8, $threshold); + $this->assertCount(9, $threshold); } public function getThresholdCases() From 8cfe7e992c3b473da32cb744c290cb7eb4ebd051 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Mon, 1 May 2023 16:50:26 -0400 Subject: [PATCH 15/31] Updated displayProgress to use progressBar --- src/Multipart/UploadState.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 8094cda24e..bc19cfe139 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -103,11 +103,10 @@ public function setProgressThresholds($totalSize) public function displayProgress($totalUploaded) { while (!empty($this->progressThresholds) - && !empty($this->progressBar) && $totalUploaded >= $this->progressThresholds[0]) { + echo $this->progressBar[$this->progressThresholds[0]]; array_shift($this->progressThresholds); - echo array_shift($this->progressBar); } } From 9ade47371ca9b1f468e073aec8515a6b5340e17c Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Tue, 2 May 2023 12:54:36 -0400 Subject: [PATCH 16/31] Update UploadState.php --- src/Multipart/UploadState.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index bc19cfe139..569b3b1a63 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -102,11 +102,11 @@ public function setProgressThresholds($totalSize) public function displayProgress($totalUploaded) { - while (!empty($this->progressThresholds) - && $totalUploaded >= $this->progressThresholds[0]) + while (!empty($this->progressBar) + && $totalUploaded >= array_key_first($this->progressBar)) { - echo $this->progressBar[$this->progressThresholds[0]]; - array_shift($this->progressThresholds); + echo $this->progressBar[array_key_first($this->progressBar)]; + unset($this->progressBar[array_key_first($this->progressBar)]); } } From 74e7938b01bfb3923909b4eeb10ee9148e549cfa Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Tue, 2 May 2023 22:06:50 -0400 Subject: [PATCH 17/31] added exceptions for non-int arguments --- src/Multipart/UploadState.php | 8 ++++++++ tests/Multipart/UploadStateTest.php | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 569b3b1a63..bb67749019 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -92,6 +92,10 @@ public function setPartSize($partSize) public function setProgressThresholds($totalSize) { + if(!is_numeric($totalSize)) { + throw new \InvalidArgumentException('The total size of the upload must be an int.'); + } + $this->progressThresholds[0] = 0; for ($i=1;$i<=8;$i++) { $this->progressThresholds []= round($totalSize*($i/8)); @@ -102,6 +106,10 @@ public function setProgressThresholds($totalSize) public function displayProgress($totalUploaded) { + if(!is_numeric($totalUploaded)) { + throw new \InvalidArgumentException('The size of the bytes being uploaded must be an int.'); + } + while (!empty($this->progressBar) && $totalUploaded >= array_key_first($this->progressBar)) { diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 85cd821f36..40feaf9b4d 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -183,4 +183,22 @@ public function getThresholdCases() [100001] ]; } + + public function testSetProgressThresholdsThrowsException() + { + $state = new UploadState([]); + $this->expectExceptionMessage('The total size of the upload must be an int.'); + $this->expectException(\InvalidArgumentException::class); + + $state->setProgressThresholds(''); + } + + public function testDisplayProgressThrowsException() + { + $state = new UploadState([]); + $this->expectExceptionMessage('The size of the bytes being uploaded must be an int.'); + $this->expectException(\InvalidArgumentException::class); + + $state->displayProgress(''); + } } \ No newline at end of file From 5eded672182389f9d000c661279926a16880db6e Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Wed, 3 May 2023 16:04:04 -0400 Subject: [PATCH 18/31] Added unit tests for failed upload and type checking (1) testFailedUploadPrintsPartialProgressBar - upload fails at 25% then throws exception (2) testSetProgressThresholdsThrowsException - checks non-ints (3) testDisplayProgressThrowsException - checks non-ints --- src/Multipart/UploadState.php | 4 +-- tests/Multipart/UploadStateTest.php | 24 +++++++++++++--- tests/S3/MultipartUploaderTest.php | 44 +++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index bb67749019..0d7ddd5aee 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -92,7 +92,7 @@ public function setPartSize($partSize) public function setProgressThresholds($totalSize) { - if(!is_numeric($totalSize)) { + if(!is_int($totalSize)) { throw new \InvalidArgumentException('The total size of the upload must be an int.'); } @@ -106,7 +106,7 @@ public function setProgressThresholds($totalSize) public function displayProgress($totalUploaded) { - if(!is_numeric($totalUploaded)) { + if(!is_int($totalUploaded)) { throw new \InvalidArgumentException('The size of the bytes being uploaded must be an int.'); } diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 40feaf9b4d..70565951a7 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -184,21 +184,37 @@ public function getThresholdCases() ]; } - public function testSetProgressThresholdsThrowsException() + /** + * @dataProvider getInvalidIntCases + */ + public function testSetProgressThresholdsThrowsException($totalSize) { $state = new UploadState([]); $this->expectExceptionMessage('The total size of the upload must be an int.'); $this->expectException(\InvalidArgumentException::class); - $state->setProgressThresholds(''); + $state->setProgressThresholds($totalSize); } - public function testDisplayProgressThrowsException() + /** + * @dataProvider getInvalidIntCases + */ + public function testDisplayProgressThrowsException($totalUploaded) { $state = new UploadState([]); $this->expectExceptionMessage('The size of the bytes being uploaded must be an int.'); $this->expectException(\InvalidArgumentException::class); - $state->displayProgress(''); + $state->displayProgress($totalUploaded); + } + + public function getInvalidIntCases() + { + return [ + [''], + [null], + ['1234'], + ['aws'], + ]; } } \ No newline at end of file diff --git a/tests/S3/MultipartUploaderTest.php b/tests/S3/MultipartUploaderTest.php index c5beefae92..3f93c826c9 100644 --- a/tests/S3/MultipartUploaderTest.php +++ b/tests/S3/MultipartUploaderTest.php @@ -316,4 +316,48 @@ public function testAppliesAmbiguousSuccessParsing() ); $uploader->upload(); } + + public function testFailedUploadPrintsPartialProgressBar($counterLimit) + { + $partialBar = [ "Transfer initiated...\n| | 0.0%\n", + "|== | 12.5%\n", + "|===== | 25.0%\n"]; + $this->expectOutputString("{$partialBar[0]}{$partialBar[1]}{$partialBar[2]}"); + + $this->expectExceptionMessage("An exception occurred while uploading parts to a multipart upload"); + $this->expectException(\Aws\S3\Exception\S3MultipartUploadException::class); + $counter = 0; + + $httpHandler = function ($request, array $options) use (&$counter) { + if ($counter < 4) { + $body = "baz"; + } else { + $body = "\n\n\n"; + } + $counter++; + + return Promise\Create::promiseFor( + new Psr7\Response(200, [], $body) + ); + }; + + $s3 = new S3Client([ + 'version' => 'latest', + 'region' => 'us-east-1', + 'http_handler' => $httpHandler + ]); + + $data = str_repeat('.', 50 * self::MB); + $source = Psr7\Utils::streamFor($data); + + $uploader = new MultipartUploader( + $s3, + $source, + [ + 'bucket' => 'test-bucket', + 'key' => 'test-key' + ] + ); + $uploader->upload(); + } } From 735c9c4758056e3fbe53dcab821e0e34960f67cb Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Fri, 5 May 2023 15:41:40 -0400 Subject: [PATCH 19/31] Added config option --- src/Multipart/UploadState.php | 11 +++++++++-- tests/Multipart/UploadStateTest.php | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 0d7ddd5aee..b3fe0628dc 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -39,12 +39,19 @@ class UploadState private $progressThresholds = []; + private $displayUploadProgress; + /** * @param array $id Params used to identity the upload. */ - public function __construct(array $id) + public function __construct(array $id, array $config = []) { $this->id = $id; + if (isset($config['track_upload']) && $config['track_upload']) { + $this->displayUploadProgress = true; + } else { + $this->displayUploadProgress = false; + } } /** @@ -110,7 +117,7 @@ public function displayProgress($totalUploaded) throw new \InvalidArgumentException('The size of the bytes being uploaded must be an int.'); } - while (!empty($this->progressBar) + while (!empty($this->progressBar && $this->displayUploadProgress) && $totalUploaded >= array_key_first($this->progressBar)) { echo $this->progressBar[array_key_first($this->progressBar)]; diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 70565951a7..525e5c5d30 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -70,6 +70,16 @@ public function testSerializationWorks() $this->assertTrue($newState->isInitiated()); $this->assertArrayHasKey('foo', $newState->getId()); } + + public function testEmptyUploadStateOutputWithConfigFalse() + { + $config['track_upload'] = false; + $state = new UploadState([], $config); + $state->setProgressThresholds(100); + $state->displayProgress(13); + $this->expectOutputString(''); + } + /** * @dataProvider getDisplayProgressCases */ @@ -78,7 +88,8 @@ public function testDisplayProgressPrintsProgress( $totalUploaded, $progressBar ) { - $state = new UploadState([]); + $config['track_upload'] = true; + $state = new UploadState([], $config); $state->setProgressThresholds($totalSize); $state->displayProgress($totalUploaded); @@ -168,7 +179,8 @@ public function getDisplayProgressCases() */ public function testUploadThresholds($totalSize) { - $state = new UploadState([]); + $config['track_upload'] = true; + $state = new UploadState([], $config); $threshold = $state->setProgressThresholds($totalSize); $this->assertIsArray($threshold); From cff396ae8d78d43407e2596281be7013df2fcd9c Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Wed, 10 May 2023 14:53:28 -0400 Subject: [PATCH 20/31] added config option to multipartUploader and multipartCopy - If config option is true, the total size is sent to upload state and a threshold array is created. DisplayProgress now relies on there being a threshold array, if there is no threshold array (if config = no), nothing prints. - the progress bar for multipart copy prints halfway for some reason, but the item is still copied (i'll fix this in the next commit) --- src/Multipart/UploadState.php | 12 ++++-------- src/S3/MultipartCopy.php | 8 +++++--- src/S3/MultipartUploader.php | 5 +++-- tests/Multipart/UploadStateTest.php | 4 ++-- tests/S3/MultipartUploaderTest.php | 5 +++-- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index b3fe0628dc..94c1354aaf 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -39,19 +39,14 @@ class UploadState private $progressThresholds = []; - private $displayUploadProgress; +// private $displayUploadProgress; /** * @param array $id Params used to identity the upload. */ - public function __construct(array $id, array $config = []) + public function __construct(array $id) { $this->id = $id; - if (isset($config['track_upload']) && $config['track_upload']) { - $this->displayUploadProgress = true; - } else { - $this->displayUploadProgress = false; - } } /** @@ -117,7 +112,8 @@ public function displayProgress($totalUploaded) throw new \InvalidArgumentException('The size of the bytes being uploaded must be an int.'); } - while (!empty($this->progressBar && $this->displayUploadProgress) + while ($this->progressThresholds + && !empty($this->progressBar) && $totalUploaded >= array_key_first($this->progressBar)) { echo $this->progressBar[array_key_first($this->progressBar)]; diff --git a/src/S3/MultipartCopy.php b/src/S3/MultipartCopy.php index 00e4d73b02..c79a135e75 100644 --- a/src/S3/MultipartCopy.php +++ b/src/S3/MultipartCopy.php @@ -76,9 +76,11 @@ public function __construct( array_change_key_case($config) + ['source_metadata' => null] ); - $this->getState()->setProgressThresholds( - $this->sourceMetadata["ContentLength"] - ); + if (isset($config['track_upload']) && $config['track_upload']) { + $this->getState()->setProgressThresholds( + $this->sourceMetadata["ContentLength"] + ); + } } /** diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index 7570fa4538..ab7426607e 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -70,8 +70,9 @@ public function __construct( 'key' => null, 'exception_class' => S3MultipartUploadException::class, ]); - - $this->getState()->setProgressThresholds($this->source->getSize()); + if (isset($config['track_upload']) && $config['track_upload']) { + $this->getState()->setProgressThresholds($this->source->getSize()); + } } protected function loadUploadWorkflowInfo() diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 525e5c5d30..27a359e854 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -89,7 +89,7 @@ public function testDisplayProgressPrintsProgress( $progressBar ) { $config['track_upload'] = true; - $state = new UploadState([], $config); + $state = new UploadState([]); $state->setProgressThresholds($totalSize); $state->displayProgress($totalUploaded); @@ -180,7 +180,7 @@ public function getDisplayProgressCases() public function testUploadThresholds($totalSize) { $config['track_upload'] = true; - $state = new UploadState([], $config); + $state = new UploadState([]); $threshold = $state->setProgressThresholds($totalSize); $this->assertIsArray($threshold); diff --git a/tests/S3/MultipartUploaderTest.php b/tests/S3/MultipartUploaderTest.php index 3f93c826c9..d5a53a40be 100644 --- a/tests/S3/MultipartUploaderTest.php +++ b/tests/S3/MultipartUploaderTest.php @@ -317,7 +317,7 @@ public function testAppliesAmbiguousSuccessParsing() $uploader->upload(); } - public function testFailedUploadPrintsPartialProgressBar($counterLimit) + public function testFailedUploadPrintsPartialProgressBar() { $partialBar = [ "Transfer initiated...\n| | 0.0%\n", "|== | 12.5%\n", @@ -355,7 +355,8 @@ public function testFailedUploadPrintsPartialProgressBar($counterLimit) $source, [ 'bucket' => 'test-bucket', - 'key' => 'test-key' + 'key' => 'test-key', + 'track_upload' => 'true' ] ); $uploader->upload(); From 776cc4d2d67fd7199a77e95b53dab7db1bc7f032 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Mon, 15 May 2023 15:45:02 -0400 Subject: [PATCH 21/31] added another parameter to markPartAsUploaded for possible Glacier support --- src/Multipart/UploadState.php | 14 +++++++++----- src/S3/MultipartUploadingTrait.php | 9 +++++---- tests/Multipart/UploadStateTest.php | 7 ++----- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 94c1354aaf..c90fab6347 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -39,7 +39,7 @@ class UploadState private $progressThresholds = []; -// private $displayUploadProgress; + private $uploadedBytes = 0; /** * @param array $id Params used to identity the upload. @@ -61,7 +61,7 @@ public function getId() } /** - * Set's the "upload_id", or 3rd part of the upload's ID. This typically + * Sets the "upload_id", or 3rd part of the upload's ID. This typically * only needs to be done after initiating an upload. * * @param string $key The param key of the upload_id. @@ -108,8 +108,8 @@ public function setProgressThresholds($totalSize) public function displayProgress($totalUploaded) { - if(!is_int($totalUploaded)) { - throw new \InvalidArgumentException('The size of the bytes being uploaded must be an int.'); + if(!is_numeric($totalUploaded)) { + throw new \InvalidArgumentException('The size of the bytes being uploaded must be a number.'); } while ($this->progressThresholds @@ -128,9 +128,13 @@ public function displayProgress($totalUploaded) * @param array $partData Data from the upload operation that needs to be * recalled during the complete operation. */ - public function markPartAsUploaded($partNumber, array $partData = []) + public function markPartAsUploaded($partNumber, $contentBytes, array $partData = []) { $this->uploadedParts[$partNumber] = $partData; +// change contentBytes to contentLength depending on whether glacier has that too + $this->uploadedBytes += $contentBytes; + $this->displayProgress($this->uploadedBytes); + } /** diff --git a/src/S3/MultipartUploadingTrait.php b/src/S3/MultipartUploadingTrait.php index 9e1eedacc7..84f1ab6409 100644 --- a/src/S3/MultipartUploadingTrait.php +++ b/src/S3/MultipartUploadingTrait.php @@ -7,7 +7,7 @@ trait MultipartUploadingTrait { - private $uploadedBytes = 0; +// private $uploadedBytes = 0; /** * Creates an UploadState object for a multipart upload by querying the @@ -53,13 +53,14 @@ public static function getStateFromService( protected function handleResult(CommandInterface $command, ResultInterface $result) { - $this->getState()->markPartAsUploaded($command['PartNumber'], [ + $this->getState()->markPartAsUploaded($command['PartNumber'], $command["ContentLength"], + [ 'PartNumber' => $command['PartNumber'], 'ETag' => $this->extractETag($result), ]); - $this->uploadedBytes += $command["ContentLength"]; - $this->getState()->displayProgress($this->uploadedBytes); +// $this->uploadedBytes += $command["ContentLength"]; +// $this->getState()->displayProgress($this->uploadedBytes); } abstract protected function extractETag(ResultInterface $result); diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 27a359e854..976b25576f 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -88,7 +88,6 @@ public function testDisplayProgressPrintsProgress( $totalUploaded, $progressBar ) { - $config['track_upload'] = true; $state = new UploadState([]); $state->setProgressThresholds($totalSize); $state->displayProgress($totalUploaded); @@ -179,7 +178,6 @@ public function getDisplayProgressCases() */ public function testUploadThresholds($totalSize) { - $config['track_upload'] = true; $state = new UploadState([]); $threshold = $state->setProgressThresholds($totalSize); @@ -214,7 +212,7 @@ public function testSetProgressThresholdsThrowsException($totalSize) public function testDisplayProgressThrowsException($totalUploaded) { $state = new UploadState([]); - $this->expectExceptionMessage('The size of the bytes being uploaded must be an int.'); + $this->expectExceptionMessage('The size of the bytes being uploaded must be a number.'); $this->expectException(\InvalidArgumentException::class); $state->displayProgress($totalUploaded); @@ -225,8 +223,7 @@ public function getInvalidIntCases() return [ [''], [null], - ['1234'], - ['aws'], + ['aws'] ]; } } \ No newline at end of file From 465fc6145e5cfa2b4e689c6ab7c2ed64fb1a0d91 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Wed, 24 May 2023 12:23:06 -0400 Subject: [PATCH 22/31] added descriptions for methods, cleaned up tests --- src/Multipart/UploadState.php | 26 +++++++++++++++++--------- src/S3/MultipartCopy.php | 2 ++ src/S3/MultipartUploader.php | 3 +++ src/S3/MultipartUploadingTrait.php | 11 ++++++----- tests/Multipart/UploadStateTest.php | 6 ++---- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index c90fab6347..131d106019 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -37,10 +37,9 @@ class UploadState /** @var int Identifies the status the upload. */ private $status = self::CREATED; + /** @var array Thresholds for progress of the upload. */ private $progressThresholds = []; - private $uploadedBytes = 0; - /** * @param array $id Params used to identity the upload. */ @@ -92,10 +91,18 @@ public function setPartSize($partSize) $this->partSize = $partSize; } + /** + * Sets the 1/8th thresholds array. $totalSize is only sent if + * 'track_upload' is true. + * + * @param $totalSize numeric Size of object to upload. + * + * @return array + */ public function setProgressThresholds($totalSize) { - if(!is_int($totalSize)) { - throw new \InvalidArgumentException('The total size of the upload must be an int.'); + if(!is_numeric($totalSize)) { + throw new \InvalidArgumentException('The total size of the upload must be a number.'); } $this->progressThresholds[0] = 0; @@ -106,6 +113,11 @@ public function setProgressThresholds($totalSize) return $this->progressThresholds; } + /** + * Prints progress of upload. + * + * @param $totalUploaded numeric Size of upload so far. + */ public function displayProgress($totalUploaded) { if(!is_numeric($totalUploaded)) { @@ -128,13 +140,9 @@ public function displayProgress($totalUploaded) * @param array $partData Data from the upload operation that needs to be * recalled during the complete operation. */ - public function markPartAsUploaded($partNumber, $contentBytes, array $partData = []) + public function markPartAsUploaded($partNumber, array $partData = []) { $this->uploadedParts[$partNumber] = $partData; -// change contentBytes to contentLength depending on whether glacier has that too - $this->uploadedBytes += $contentBytes; - $this->displayProgress($this->uploadedBytes); - } /** diff --git a/src/S3/MultipartCopy.php b/src/S3/MultipartCopy.php index c79a135e75..e50839e43e 100644 --- a/src/S3/MultipartCopy.php +++ b/src/S3/MultipartCopy.php @@ -52,6 +52,8 @@ class MultipartCopy extends AbstractUploadManager * options are ignored. * - source_metadata: (Aws\ResultInterface) An object that represents the * result of executing a HeadObject command on the copy source. + * - track_upload: (boolean) Tracks status in 1/8th increments of upload. + * Prints progress when truthy value provided. * * @param S3ClientInterface $client Client used for the upload. * @param string|array $source Location of the data to be copied (in the diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index ab7426607e..4dca7f3711 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -55,6 +55,8 @@ class MultipartUploader extends AbstractUploader * of the multipart upload and that is used to resume a previous upload. * When this option is provided, the `bucket`, `key`, and `part_size` * options are ignored. + * - track_upload: (boolean) Tracks status in 1/8th increments of upload. + * Prints progress when truthy value provided. * * @param S3ClientInterface $client Client used for the upload. * @param mixed $source Source of the data to upload. @@ -70,6 +72,7 @@ public function __construct( 'key' => null, 'exception_class' => S3MultipartUploadException::class, ]); + if (isset($config['track_upload']) && $config['track_upload']) { $this->getState()->setProgressThresholds($this->source->getSize()); } diff --git a/src/S3/MultipartUploadingTrait.php b/src/S3/MultipartUploadingTrait.php index 84f1ab6409..cb14c0a9dd 100644 --- a/src/S3/MultipartUploadingTrait.php +++ b/src/S3/MultipartUploadingTrait.php @@ -7,7 +7,7 @@ trait MultipartUploadingTrait { -// private $uploadedBytes = 0; + private $uploadedBytes = 0; /** * Creates an UploadState object for a multipart upload by querying the @@ -53,14 +53,15 @@ public static function getStateFromService( protected function handleResult(CommandInterface $command, ResultInterface $result) { - $this->getState()->markPartAsUploaded($command['PartNumber'], $command["ContentLength"], - [ + $this->getState()->markPartAsUploaded($command['PartNumber'], [ 'PartNumber' => $command['PartNumber'], 'ETag' => $this->extractETag($result), ]); -// $this->uploadedBytes += $command["ContentLength"]; -// $this->getState()->displayProgress($this->uploadedBytes); + // Updates counter for uploaded bytes. + $this->uploadedBytes += $command["ContentLength"]; + // Sends uploaded bytes to progress tracker. + $this->getState()->displayProgress($this->uploadedBytes); } abstract protected function extractETag(ResultInterface $result); diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 976b25576f..23b159d164 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -73,9 +73,7 @@ public function testSerializationWorks() public function testEmptyUploadStateOutputWithConfigFalse() { - $config['track_upload'] = false; - $state = new UploadState([], $config); - $state->setProgressThresholds(100); + $state = new UploadState([]); $state->displayProgress(13); $this->expectOutputString(''); } @@ -200,7 +198,7 @@ public function getThresholdCases() public function testSetProgressThresholdsThrowsException($totalSize) { $state = new UploadState([]); - $this->expectExceptionMessage('The total size of the upload must be an int.'); + $this->expectExceptionMessage('The total size of the upload must be a number.'); $this->expectException(\InvalidArgumentException::class); $state->setProgressThresholds($totalSize); From e2d4e81d25cecc33a7f00fbc435cbb241a22b0ee Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Fri, 26 May 2023 10:47:35 -0400 Subject: [PATCH 23/31] PHPUnit Polyfills on UploadStateTest --- tests/Multipart/UploadStateTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 23b159d164..5c32768694 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -2,7 +2,7 @@ namespace Aws\Test\Multipart; use Aws\Multipart\UploadState; -use PHPUnit\Framework\TestCase; +use Yoast\PHPUnitPolyfills\TestCases\TestCase; /** * @covers Aws\Multipart\UploadState From 1a0a58080d7a174452c7a11cb29eaf95904d49f6 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Fri, 26 May 2023 17:55:30 -0400 Subject: [PATCH 24/31] removed array_key_first to support php 5.5 progressBar and progressThresholds are no longer combined, so that array_key_first could be removed. progressThresholds[0] and array_shift are now used instead. --- src/Multipart/UploadState.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 131d106019..a9b46a1471 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -109,7 +109,6 @@ public function setProgressThresholds($totalSize) for ($i=1;$i<=8;$i++) { $this->progressThresholds []= round($totalSize*($i/8)); } - $this->progressBar = array_combine($this->progressThresholds, $this->progressBar); return $this->progressThresholds; } @@ -124,12 +123,12 @@ public function displayProgress($totalUploaded) throw new \InvalidArgumentException('The size of the bytes being uploaded must be a number.'); } - while ($this->progressThresholds + while (!empty($this->progressThresholds) && !empty($this->progressBar) - && $totalUploaded >= array_key_first($this->progressBar)) + && $totalUploaded >= $this->progressThresholds[0]) { - echo $this->progressBar[array_key_first($this->progressBar)]; - unset($this->progressBar[array_key_first($this->progressBar)]); + echo array_shift($this->progressBar); + array_shift($this->progressThresholds); } } From eee6a6757d2a7198014cec72c4d7296c7f847187 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Tue, 30 May 2023 13:05:31 -0400 Subject: [PATCH 25/31] dev guide description added --- src/S3/MultipartCopy.php | 4 ++-- src/S3/MultipartUploader.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/S3/MultipartCopy.php b/src/S3/MultipartCopy.php index e50839e43e..85ef2f53a9 100644 --- a/src/S3/MultipartCopy.php +++ b/src/S3/MultipartCopy.php @@ -52,8 +52,8 @@ class MultipartCopy extends AbstractUploadManager * options are ignored. * - source_metadata: (Aws\ResultInterface) An object that represents the * result of executing a HeadObject command on the copy source. - * - track_upload: (boolean) Tracks status in 1/8th increments of upload. - * Prints progress when truthy value provided. + * - track_upload: (boolean) Set true to track status in 1/8th increments + * for upload. * * @param S3ClientInterface $client Client used for the upload. * @param string|array $source Location of the data to be copied (in the diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index 4dca7f3711..fbddd95249 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -55,8 +55,8 @@ class MultipartUploader extends AbstractUploader * of the multipart upload and that is used to resume a previous upload. * When this option is provided, the `bucket`, `key`, and `part_size` * options are ignored. - * - track_upload: (boolean) Tracks status in 1/8th increments of upload. - * Prints progress when truthy value provided. + * - track_upload: (boolean) Set true to track status in 1/8th increments + * for upload. * * @param S3ClientInterface $client Client used for the upload. * @param mixed $source Source of the data to upload. From 1156b751761a25f062e7e9f3a7eb9e1d44d67c4f Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Fri, 9 Jun 2023 13:00:56 -0400 Subject: [PATCH 26/31] added method setDisplayProgress, changelog --- .changes/nextrelease/test.json | 7 +++++++ src/Multipart/UploadState.php | 30 +++++++++++++++++++---------- src/S3/MultipartCopy.php | 2 +- src/S3/MultipartUploader.php | 2 +- src/S3/MultipartUploadingTrait.php | 6 ++++-- tests/Multipart/UploadStateTest.php | 12 ++++++------ 6 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 .changes/nextrelease/test.json diff --git a/.changes/nextrelease/test.json b/.changes/nextrelease/test.json new file mode 100644 index 0000000000..e5a1f1b5dc --- /dev/null +++ b/.changes/nextrelease/test.json @@ -0,0 +1,7 @@ +[ + { + "type": "enhancement", + "category": "S3", + "description": "'track_upload' configuration option added for MultipartUpload and MultipartCopy" + } +] \ No newline at end of file diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index a9b46a1471..207f764ad1 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -91,6 +91,17 @@ public function setPartSize($partSize) $this->partSize = $partSize; } + /** + * Set displayProgress. Sends object size to setProgressThresholds. + * + * @param $totalSize int Size of object to upload. + */ + public function setDisplayProgress($totalSize) + { + $this->setProgressThresholds($totalSize); + $this->displayProgress = true; + } + /** * Sets the 1/8th thresholds array. $totalSize is only sent if * 'track_upload' is true. @@ -117,18 +128,18 @@ public function setProgressThresholds($totalSize) * * @param $totalUploaded numeric Size of upload so far. */ - public function displayProgress($totalUploaded) + public function getDisplayProgress($totalUploaded) { - if(!is_numeric($totalUploaded)) { + if (!is_numeric($totalUploaded)) { throw new \InvalidArgumentException('The size of the bytes being uploaded must be a number.'); } - while (!empty($this->progressThresholds) - && !empty($this->progressBar) - && $totalUploaded >= $this->progressThresholds[0]) - { - echo array_shift($this->progressBar); - array_shift($this->progressThresholds); + if ($this->displayProgress) { + while (!empty($this->progressBar) + && $totalUploaded >= $this->progressThresholds[0]) { + echo array_shift($this->progressBar); + array_shift($this->progressThresholds); + } } } @@ -173,7 +184,6 @@ public function getUploadedParts() * @param int $status Status is an integer code defined by the constants * CREATED, INITIATED, and COMPLETED on this class. */ - public function setStatus($status) { $this->status = $status; @@ -198,4 +208,4 @@ public function isCompleted() { return $this->status === self::COMPLETED; } -} \ No newline at end of file +} diff --git a/src/S3/MultipartCopy.php b/src/S3/MultipartCopy.php index 85ef2f53a9..feb38e3185 100644 --- a/src/S3/MultipartCopy.php +++ b/src/S3/MultipartCopy.php @@ -79,7 +79,7 @@ public function __construct( ); if (isset($config['track_upload']) && $config['track_upload']) { - $this->getState()->setProgressThresholds( + $this->getState()->setDisplayProgress( $this->sourceMetadata["ContentLength"] ); } diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index fbddd95249..0838d1f702 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -74,7 +74,7 @@ public function __construct( ]); if (isset($config['track_upload']) && $config['track_upload']) { - $this->getState()->setProgressThresholds($this->source->getSize()); + $this->getState()->setDisplayProgress($this->source->getSize()); } } diff --git a/src/S3/MultipartUploadingTrait.php b/src/S3/MultipartUploadingTrait.php index cb14c0a9dd..049ebbeafc 100644 --- a/src/S3/MultipartUploadingTrait.php +++ b/src/S3/MultipartUploadingTrait.php @@ -60,8 +60,10 @@ protected function handleResult(CommandInterface $command, ResultInterface $resu // Updates counter for uploaded bytes. $this->uploadedBytes += $command["ContentLength"]; - // Sends uploaded bytes to progress tracker. - $this->getState()->displayProgress($this->uploadedBytes); + // Sends uploaded bytes to progress tracker if getDisplayProgress set + if ($this->getState()->displayProgress) { + $this->getState()->getDisplayProgress($this->uploadedBytes); + } } abstract protected function extractETag(ResultInterface $result); diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index 5c32768694..ba9095327f 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -74,21 +74,22 @@ public function testSerializationWorks() public function testEmptyUploadStateOutputWithConfigFalse() { $state = new UploadState([]); - $state->displayProgress(13); + $state->displayProgress = false; + $state->getDisplayProgress(13); $this->expectOutputString(''); } /** * @dataProvider getDisplayProgressCases */ - public function testDisplayProgressPrintsProgress( + public function testGetDisplayProgressPrintsProgress( $totalSize, $totalUploaded, $progressBar ) { $state = new UploadState([]); - $state->setProgressThresholds($totalSize); - $state->displayProgress($totalUploaded); + $state->setDisplayProgress($totalSize); + $state->getDisplayProgress($totalUploaded); $this->expectOutputString($progressBar); } @@ -212,8 +213,7 @@ public function testDisplayProgressThrowsException($totalUploaded) $state = new UploadState([]); $this->expectExceptionMessage('The size of the bytes being uploaded must be a number.'); $this->expectException(\InvalidArgumentException::class); - - $state->displayProgress($totalUploaded); + $state->getDisplayProgress($totalUploaded); } public function getInvalidIntCases() From da296ce0db6e53309ca7a6a913ece91a02d2a104 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Fri, 9 Jun 2023 13:21:23 -0400 Subject: [PATCH 27/31] $displayProgress not declared dynamically --- src/Multipart/UploadState.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 207f764ad1..cfe62658e7 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -40,6 +40,9 @@ class UploadState /** @var array Thresholds for progress of the upload. */ private $progressThresholds = []; + /** @var boolean Determines status for tracking the upload */ + public $displayProgress = false; + /** * @param array $id Params used to identity the upload. */ From 9ea4f208f6c05fc3b32ad5832dacab679ff7d43e Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Fri, 9 Jun 2023 22:07:37 -0400 Subject: [PATCH 28/31] line-wrap in upload state --- src/Multipart/UploadState.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index cfe62658e7..30137e34c2 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -97,7 +97,7 @@ public function setPartSize($partSize) /** * Set displayProgress. Sends object size to setProgressThresholds. * - * @param $totalSize int Size of object to upload. + * @param $totalSize numeric Size of object to upload. */ public function setDisplayProgress($totalSize) { @@ -116,7 +116,9 @@ public function setDisplayProgress($totalSize) public function setProgressThresholds($totalSize) { if(!is_numeric($totalSize)) { - throw new \InvalidArgumentException('The total size of the upload must be a number.'); + throw new \InvalidArgumentException( + 'The total size of the upload must be a number.' + ); } $this->progressThresholds[0] = 0; @@ -134,7 +136,9 @@ public function setProgressThresholds($totalSize) public function getDisplayProgress($totalUploaded) { if (!is_numeric($totalUploaded)) { - throw new \InvalidArgumentException('The size of the bytes being uploaded must be a number.'); + throw new \InvalidArgumentException( + 'The size of the bytes being uploaded must be a number.' + ); } if ($this->displayProgress) { From 367bc01214f1757aaca878412d9dffdab26c8bb6 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Tue, 27 Jun 2023 00:27:28 -0400 Subject: [PATCH 29/31] === comparison operator --- .../nextrelease/{test.json => feature-progress-tracker.json} | 0 src/Multipart/AbstractUploader.php | 2 +- src/S3/MultipartCopy.php | 2 +- src/S3/MultipartUploader.php | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename .changes/nextrelease/{test.json => feature-progress-tracker.json} (100%) diff --git a/.changes/nextrelease/test.json b/.changes/nextrelease/feature-progress-tracker.json similarity index 100% rename from .changes/nextrelease/test.json rename to .changes/nextrelease/feature-progress-tracker.json diff --git a/src/Multipart/AbstractUploader.php b/src/Multipart/AbstractUploader.php index f0fd6e56e6..75e6794660 100644 --- a/src/Multipart/AbstractUploader.php +++ b/src/Multipart/AbstractUploader.php @@ -147,4 +147,4 @@ protected function getNumberOfParts($partSize) } return null; } -} \ No newline at end of file +} diff --git a/src/S3/MultipartCopy.php b/src/S3/MultipartCopy.php index feb38e3185..fd9ee7d839 100644 --- a/src/S3/MultipartCopy.php +++ b/src/S3/MultipartCopy.php @@ -78,7 +78,7 @@ public function __construct( array_change_key_case($config) + ['source_metadata' => null] ); - if (isset($config['track_upload']) && $config['track_upload']) { + if (isset($config['track_upload']) && $config['track_upload'] === true) { $this->getState()->setDisplayProgress( $this->sourceMetadata["ContentLength"] ); diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index 0838d1f702..fd71ba633e 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -73,7 +73,7 @@ public function __construct( 'exception_class' => S3MultipartUploadException::class, ]); - if (isset($config['track_upload']) && $config['track_upload']) { + if (isset($config['track_upload']) && $config['track_upload'] === true) { $this->getState()->setDisplayProgress($this->source->getSize()); } } From d81c87bf6c7527a6b8d679332a1b9937e235f721 Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Tue, 27 Jun 2023 10:46:11 -0400 Subject: [PATCH 30/31] Update MultipartUploaderTest.php --- tests/S3/MultipartUploaderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/S3/MultipartUploaderTest.php b/tests/S3/MultipartUploaderTest.php index d5a53a40be..169828cb1c 100644 --- a/tests/S3/MultipartUploaderTest.php +++ b/tests/S3/MultipartUploaderTest.php @@ -356,7 +356,7 @@ public function testFailedUploadPrintsPartialProgressBar() [ 'bucket' => 'test-bucket', 'key' => 'test-key', - 'track_upload' => 'true' + 'track_upload' => true ] ); $uploader->upload(); From 39b10448a51f23d551123d07f34bf48db875f00c Mon Sep 17 00:00:00 2001 From: Cintia <81837982+cintiashamsu@users.noreply.github.com> Date: Tue, 27 Jun 2023 13:53:44 -0400 Subject: [PATCH 31/31] displayprogress set in multipartuploader/copy --- src/Multipart/UploadState.php | 11 ----------- src/S3/MultipartCopy.php | 3 ++- src/S3/MultipartUploader.php | 3 ++- tests/Multipart/UploadStateTest.php | 3 ++- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/Multipart/UploadState.php b/src/Multipart/UploadState.php index 30137e34c2..a9b523b024 100644 --- a/src/Multipart/UploadState.php +++ b/src/Multipart/UploadState.php @@ -94,17 +94,6 @@ public function setPartSize($partSize) $this->partSize = $partSize; } - /** - * Set displayProgress. Sends object size to setProgressThresholds. - * - * @param $totalSize numeric Size of object to upload. - */ - public function setDisplayProgress($totalSize) - { - $this->setProgressThresholds($totalSize); - $this->displayProgress = true; - } - /** * Sets the 1/8th thresholds array. $totalSize is only sent if * 'track_upload' is true. diff --git a/src/S3/MultipartCopy.php b/src/S3/MultipartCopy.php index fd9ee7d839..cbbc15097e 100644 --- a/src/S3/MultipartCopy.php +++ b/src/S3/MultipartCopy.php @@ -79,9 +79,10 @@ public function __construct( ); if (isset($config['track_upload']) && $config['track_upload'] === true) { - $this->getState()->setDisplayProgress( + $this->getState()->setProgressThresholds( $this->sourceMetadata["ContentLength"] ); + $this->getState()->displayProgress = true; } } diff --git a/src/S3/MultipartUploader.php b/src/S3/MultipartUploader.php index fd71ba633e..3ad7110136 100644 --- a/src/S3/MultipartUploader.php +++ b/src/S3/MultipartUploader.php @@ -74,7 +74,8 @@ public function __construct( ]); if (isset($config['track_upload']) && $config['track_upload'] === true) { - $this->getState()->setDisplayProgress($this->source->getSize()); + $this->getState()->setProgressThresholds($this->source->getSize()); + $this->getState()->displayProgress = true; } } diff --git a/tests/Multipart/UploadStateTest.php b/tests/Multipart/UploadStateTest.php index ba9095327f..b579b76a02 100644 --- a/tests/Multipart/UploadStateTest.php +++ b/tests/Multipart/UploadStateTest.php @@ -88,7 +88,8 @@ public function testGetDisplayProgressPrintsProgress( $progressBar ) { $state = new UploadState([]); - $state->setDisplayProgress($totalSize); + $state->displayProgress = true; + $state->setProgressThresholds($totalSize); $state->getDisplayProgress($totalUploaded); $this->expectOutputString($progressBar);