| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Multipart; |
| 4 |
|
| 5 |
/** |
| 6 |
* Representation of the multipart upload. |
| 7 |
* |
| 8 |
* This object keeps track of the state of the upload, including the status and |
| 9 |
* which parts have been uploaded. |
| 10 |
*/ |
| 11 |
class UploadState |
| 12 |
{ |
| 13 |
const CREATED = 0; |
| 14 |
const INITIATED = 1; |
| 15 |
const COMPLETED = 2; |
| 16 |
const PROGRESS_THRESHOLD_SIZE = 8; |
| 17 |
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"]; |
| 18 |
/** @var array Params used to identity the upload. */ |
| 19 |
private $id; |
| 20 |
/** @var int Part size being used by the upload. */ |
| 21 |
private $partSize; |
| 22 |
/** @var array Parts that have been uploaded. */ |
| 23 |
private $uploadedParts = []; |
| 24 |
/** @var int Identifies the status the upload. */ |
| 25 |
private $status = self::CREATED; |
| 26 |
/** @var array Thresholds for progress of the upload. */ |
| 27 |
private $progressThresholds = []; |
| 28 |
/** @var boolean Determines status for tracking the upload */ |
| 29 |
private $displayProgress = \false; |
| 30 |
/** |
| 31 |
* @param array $id Params used to identity the upload. |
| 32 |
*/ |
| 33 |
public function __construct(array $id, array $config = []) |
| 34 |
{ |
| 35 |
$this->id = $id; |
| 36 |
if (isset($config['display_progress']) && \is_bool($config['display_progress'])) { |
| 37 |
$this->displayProgress = $config['display_progress']; |
| 38 |
} |
| 39 |
} |
| 40 |
/** |
| 41 |
* Get the upload's ID, which is a tuple of parameters that can uniquely |
| 42 |
* identify the upload. |
| 43 |
* |
| 44 |
* @return array |
| 45 |
*/ |
| 46 |
public function getId() |
| 47 |
{ |
| 48 |
return $this->id; |
| 49 |
} |
| 50 |
/** |
| 51 |
* Sets the "upload_id", or 3rd part of the upload's ID. This typically |
| 52 |
* only needs to be done after initiating an upload. |
| 53 |
* |
| 54 |
* @param string $key The param key of the upload_id. |
| 55 |
* @param string $value The param value of the upload_id. |
| 56 |
*/ |
| 57 |
public function setUploadId($key, $value) |
| 58 |
{ |
| 59 |
$this->id[$key] = $value; |
| 60 |
} |
| 61 |
/** |
| 62 |
* Get the part size. |
| 63 |
* |
| 64 |
* @return int |
| 65 |
*/ |
| 66 |
public function getPartSize() |
| 67 |
{ |
| 68 |
return $this->partSize; |
| 69 |
} |
| 70 |
/** |
| 71 |
* Set the part size. |
| 72 |
* |
| 73 |
* @param $partSize int Size of upload parts. |
| 74 |
*/ |
| 75 |
public function setPartSize($partSize) |
| 76 |
{ |
| 77 |
$this->partSize = $partSize; |
| 78 |
} |
| 79 |
/** |
| 80 |
* Sets the 1/8th thresholds array. $totalSize is only sent if |
| 81 |
* 'track_upload' is true. |
| 82 |
* |
| 83 |
* @param $totalSize numeric Size of object to upload. |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function setProgressThresholds($totalSize) : array |
| 88 |
{ |
| 89 |
if (!\is_numeric($totalSize)) { |
| 90 |
throw new \InvalidArgumentException('The total size of the upload must be a number.'); |
| 91 |
} |
| 92 |
$this->progressThresholds[0] = 0; |
| 93 |
for ($i = 1; $i <= self::PROGRESS_THRESHOLD_SIZE; $i++) { |
| 94 |
$this->progressThresholds[] = \round($totalSize * ($i / self::PROGRESS_THRESHOLD_SIZE)); |
| 95 |
} |
| 96 |
return $this->progressThresholds; |
| 97 |
} |
| 98 |
/** |
| 99 |
* Prints progress of upload. |
| 100 |
* |
| 101 |
* @param $totalUploaded numeric Size of upload so far. |
| 102 |
*/ |
| 103 |
public function getDisplayProgress($totalUploaded) : void |
| 104 |
{ |
| 105 |
if (!\is_numeric($totalUploaded)) { |
| 106 |
throw new \InvalidArgumentException('The size of the bytes being uploaded must be a number.'); |
| 107 |
} |
| 108 |
if ($this->displayProgress) { |
| 109 |
while (!empty($this->progressBar) && $totalUploaded >= $this->progressThresholds[0]) { |
| 110 |
echo \array_shift($this->progressBar); |
| 111 |
\array_shift($this->progressThresholds); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
/** |
| 116 |
* Marks a part as being uploaded. |
| 117 |
* |
| 118 |
* @param int $partNumber The part number. |
| 119 |
* @param array $partData Data from the upload operation that needs to be |
| 120 |
* recalled during the complete operation. |
| 121 |
*/ |
| 122 |
public function markPartAsUploaded($partNumber, array $partData = []) |
| 123 |
{ |
| 124 |
$this->uploadedParts[$partNumber] = $partData; |
| 125 |
} |
| 126 |
/** |
| 127 |
* Returns whether a part has been uploaded. |
| 128 |
* |
| 129 |
* @param int $partNumber The part number. |
| 130 |
* |
| 131 |
* @return bool |
| 132 |
*/ |
| 133 |
public function hasPartBeenUploaded($partNumber) |
| 134 |
{ |
| 135 |
return isset($this->uploadedParts[$partNumber]); |
| 136 |
} |
| 137 |
/** |
| 138 |
* Returns a sorted list of all the uploaded parts. |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function getUploadedParts() |
| 143 |
{ |
| 144 |
\ksort($this->uploadedParts); |
| 145 |
return $this->uploadedParts; |
| 146 |
} |
| 147 |
/** |
| 148 |
* Set the status of the upload. |
| 149 |
* |
| 150 |
* @param int $status Status is an integer code defined by the constants |
| 151 |
* CREATED, INITIATED, and COMPLETED on this class. |
| 152 |
*/ |
| 153 |
public function setStatus($status) |
| 154 |
{ |
| 155 |
$this->status = $status; |
| 156 |
} |
| 157 |
/** |
| 158 |
* Determines whether the upload state is in the INITIATED status. |
| 159 |
* |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
public function isInitiated() |
| 163 |
{ |
| 164 |
return $this->status === self::INITIATED; |
| 165 |
} |
| 166 |
/** |
| 167 |
* Determines whether the upload state is in the COMPLETED status. |
| 168 |
* |
| 169 |
* @return bool |
| 170 |
*/ |
| 171 |
public function isCompleted() |
| 172 |
{ |
| 173 |
return $this->status === self::COMPLETED; |
| 174 |
} |
| 175 |
} |
| 176 |
|