AbstractUploadManager.php
11 months ago
AbstractUploader.php
11 months ago
UploadState.php
11 months ago
UploadState.php
146 lines
| 1 | <?php |
| 2 | namespace Aws\Multipart; |
| 3 | |
| 4 | /** |
| 5 | * Representation of the multipart upload. |
| 6 | * |
| 7 | * This object keeps track of the state of the upload, including the status and |
| 8 | * which parts have been uploaded. |
| 9 | */ |
| 10 | class UploadState |
| 11 | { |
| 12 | const CREATED = 0; |
| 13 | const INITIATED = 1; |
| 14 | const COMPLETED = 2; |
| 15 | |
| 16 | /** @var array Params used to identity the upload. */ |
| 17 | private $id; |
| 18 | |
| 19 | /** @var int Part size being used by the upload. */ |
| 20 | private $partSize; |
| 21 | |
| 22 | /** @var array Parts that have been uploaded. */ |
| 23 | private $uploadedParts = []; |
| 24 | |
| 25 | /** @var int Identifies the status the upload. */ |
| 26 | private $status = self::CREATED; |
| 27 | |
| 28 | /** |
| 29 | * @param array $id Params used to identity the upload. |
| 30 | */ |
| 31 | public function __construct(array $id) |
| 32 | { |
| 33 | $this->id = $id; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get the upload's ID, which is a tuple of parameters that can uniquely |
| 38 | * identify the upload. |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | public function getId() |
| 43 | { |
| 44 | return $this->id; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Set's the "upload_id", or 3rd part of the upload's ID. This typically |
| 49 | * only needs to be done after initiating an upload. |
| 50 | * |
| 51 | * @param string $key The param key of the upload_id. |
| 52 | * @param string $value The param value of the upload_id. |
| 53 | */ |
| 54 | public function setUploadId($key, $value) |
| 55 | { |
| 56 | $this->id[$key] = $value; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get the part size. |
| 61 | * |
| 62 | * @return int |
| 63 | */ |
| 64 | public function getPartSize() |
| 65 | { |
| 66 | return $this->partSize; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Set the part size. |
| 71 | * |
| 72 | * @param $partSize int Size of upload parts. |
| 73 | */ |
| 74 | public function setPartSize($partSize) |
| 75 | { |
| 76 | $this->partSize = $partSize; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Marks a part as being uploaded. |
| 81 | * |
| 82 | * @param int $partNumber The part number. |
| 83 | * @param array $partData Data from the upload operation that needs to be |
| 84 | * recalled during the complete operation. |
| 85 | */ |
| 86 | public function markPartAsUploaded($partNumber, array $partData = []) |
| 87 | { |
| 88 | $this->uploadedParts[$partNumber] = $partData; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns whether a part has been uploaded. |
| 93 | * |
| 94 | * @param int $partNumber The part number. |
| 95 | * |
| 96 | * @return bool |
| 97 | */ |
| 98 | public function hasPartBeenUploaded($partNumber) |
| 99 | { |
| 100 | return isset($this->uploadedParts[$partNumber]); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns a sorted list of all the uploaded parts. |
| 105 | * |
| 106 | * @return array |
| 107 | */ |
| 108 | public function getUploadedParts() |
| 109 | { |
| 110 | ksort($this->uploadedParts); |
| 111 | |
| 112 | return $this->uploadedParts; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Set the status of the upload. |
| 117 | * |
| 118 | * @param int $status Status is an integer code defined by the constants |
| 119 | * CREATED, INITIATED, and COMPLETED on this class. |
| 120 | */ |
| 121 | public function setStatus($status) |
| 122 | { |
| 123 | $this->status = $status; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Determines whether the upload state is in the INITIATED status. |
| 128 | * |
| 129 | * @return bool |
| 130 | */ |
| 131 | public function isInitiated() |
| 132 | { |
| 133 | return $this->status === self::INITIATED; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Determines whether the upload state is in the COMPLETED status. |
| 138 | * |
| 139 | * @return bool |
| 140 | */ |
| 141 | public function isCompleted() |
| 142 | { |
| 143 | return $this->status === self::COMPLETED; |
| 144 | } |
| 145 | } |
| 146 |