| 1 |
<?php |
| 2 |
namespace Aws\Multipart; |
| 3 |
|
| 4 |
use Aws\AwsClientInterface as Client; |
| 5 |
use Aws\CommandInterface; |
| 6 |
use Aws\CommandPool; |
| 7 |
use Aws\Exception\AwsException; |
| 8 |
use Aws\Exception\MultipartUploadException; |
| 9 |
use Aws\Result; |
| 10 |
use Aws\ResultInterface; |
| 11 |
use GuzzleHttp\Promise; |
| 12 |
use GuzzleHttp\Promise\PromiseInterface; |
| 13 |
use InvalidArgumentException as IAE; |
| 14 |
use Psr\Http\Message\RequestInterface; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
abstract class AbstractUploadManager implements Promise\PromisorInterface |
| 22 |
{ |
| 23 |
const DEFAULT_CONCURRENCY = 5; |
| 24 |
|
| 25 |
|
| 26 |
private static $defaultConfig = [ |
| 27 |
'part_size' => null, |
| 28 |
'state' => null, |
| 29 |
'concurrency' => self::DEFAULT_CONCURRENCY, |
| 30 |
'prepare_data_source' => null, |
| 31 |
'before_initiate' => null, |
| 32 |
'before_upload' => null, |
| 33 |
'before_complete' => null, |
| 34 |
'exception_class' => MultipartUploadException::class, |
| 35 |
]; |
| 36 |
|
| 37 |
|
| 38 |
protected $client; |
| 39 |
|
| 40 |
|
| 41 |
protected $config; |
| 42 |
|
| 43 |
|
| 44 |
protected $info; |
| 45 |
|
| 46 |
|
| 47 |
protected $promise; |
| 48 |
|
| 49 |
|
| 50 |
protected $state; |
| 51 |
|
| 52 |
|
| 53 |
protected $displayProgress; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public function __construct(Client $client, array $config = []) |
| 60 |
{ |
| 61 |
$this->client = $client; |
| 62 |
$this->info = $this->loadUploadWorkflowInfo(); |
| 63 |
$this->config = $config + self::$defaultConfig; |
| 64 |
$this->state = $this->determineState(); |
| 65 |
|
| 66 |
if (isset($config['display_progress']) |
| 67 |
&& is_bool($config['display_progress']) |
| 68 |
) { |
| 69 |
$this->displayProgress = $config['display_progress']; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function getState() |
| 79 |
{ |
| 80 |
return $this->state; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function upload() |
| 91 |
{ |
| 92 |
return $this->promise()->wait(); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function promise(): PromiseInterface |
| 101 |
{ |
| 102 |
if ($this->promise) { |
| 103 |
return $this->promise; |
| 104 |
} |
| 105 |
|
| 106 |
return $this->promise = Promise\Coroutine::of(function () { |
| 107 |
|
| 108 |
if ($this->state->isCompleted()) { |
| 109 |
throw new \LogicException('This multipart upload has already ' |
| 110 |
. 'been completed or aborted.' |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
if (!$this->state->isInitiated()) { |
| 115 |
|
| 116 |
if (is_callable($this->config["prepare_data_source"])) { |
| 117 |
$this->config["prepare_data_source"](); |
| 118 |
} |
| 119 |
|
| 120 |
$result = (yield $this->execCommand('initiate', $this->getInitiateParams())); |
| 121 |
$this->state->setUploadId( |
| 122 |
$this->info['id']['upload_id'], |
| 123 |
$result[$this->info['id']['upload_id']] |
| 124 |
); |
| 125 |
$this->state->setStatus(UploadState::INITIATED); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
$resultHandler = $this->getResultHandler($errors); |
| 131 |
$commands = new CommandPool( |
| 132 |
$this->client, |
| 133 |
$this->getUploadCommands($resultHandler), |
| 134 |
[ |
| 135 |
'concurrency' => $this->config['concurrency'], |
| 136 |
'before' => $this->config['before_upload'], |
| 137 |
] |
| 138 |
); |
| 139 |
|
| 140 |
|
| 141 |
yield $commands->promise(); |
| 142 |
if ($errors) { |
| 143 |
throw new $this->config['exception_class']($this->state, $errors); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
yield $this->execCommand('complete', $this->getCompleteParams()); |
| 148 |
$this->state->setStatus(UploadState::COMPLETED); |
| 149 |
})->otherwise($this->buildFailureCatch()); |
| 150 |
} |
| 151 |
|
| 152 |
private function transformException($e) |
| 153 |
{ |
| 154 |
|
| 155 |
if ($e instanceof AwsException) { |
| 156 |
$e = new $this->config['exception_class']($this->state, $e); |
| 157 |
} |
| 158 |
throw $e; |
| 159 |
} |
| 160 |
|
| 161 |
private function buildFailureCatch() |
| 162 |
{ |
| 163 |
if (interface_exists("Throwable")) { |
| 164 |
return function (\Throwable $e) { |
| 165 |
return $this->transformException($e); |
| 166 |
}; |
| 167 |
} else { |
| 168 |
return function (\Exception $e) { |
| 169 |
return $this->transformException($e); |
| 170 |
}; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
protected function getConfig() |
| 175 |
{ |
| 176 |
return $this->config; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
abstract protected function loadUploadWorkflowInfo(); |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
abstract protected function determinePartSize(); |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
abstract protected function handleResult( |
| 209 |
CommandInterface $command, |
| 210 |
ResultInterface $result |
| 211 |
); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
abstract protected function getInitiateParams(); |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
abstract protected function getCompleteParams(); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
private function determineState(): UploadState |
| 232 |
{ |
| 233 |
|
| 234 |
if ($this->config['state'] instanceof UploadState) { |
| 235 |
return $this->config['state']; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
$required = $this->info['id']; |
| 240 |
$id = [$required['upload_id'] => null]; |
| 241 |
unset($required['upload_id']); |
| 242 |
foreach ($required as $key => $param) { |
| 243 |
if (!isset($this->config[$key]) || $this->config[$key] === '') { |
| 244 |
throw new IAE('You must provide a value for "' . $key . '" in ' |
| 245 |
. 'your config for the MultipartUploader for ' |
| 246 |
. $this->client->getApi()->getServiceFullName() . '.'); |
| 247 |
} |
| 248 |
$id[$param] = $this->config[$key]; |
| 249 |
} |
| 250 |
$state = new UploadState($id, $this->config); |
| 251 |
$state->setPartSize($this->determinePartSize()); |
| 252 |
|
| 253 |
return $state; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
protected function execCommand($operation, array $params) |
| 265 |
{ |
| 266 |
|
| 267 |
$command = $this->client->getCommand( |
| 268 |
$this->info['command'][$operation], |
| 269 |
$params + $this->state->getId() |
| 270 |
); |
| 271 |
|
| 272 |
|
| 273 |
if (is_callable($this->config["before_{$operation}"])) { |
| 274 |
$this->config["before_{$operation}"]($command); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
return $this->client->executeAsync($command); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
protected function getResultHandler(&$errors = []) |
| 295 |
{ |
| 296 |
return function (callable $handler) use (&$errors) { |
| 297 |
return function ( |
| 298 |
CommandInterface $command, |
| 299 |
?RequestInterface $request = null |
| 300 |
) use ($handler, &$errors) { |
| 301 |
return $handler($command, $request)->then( |
| 302 |
function (ResultInterface $result) use ($command) { |
| 303 |
$this->handleResult($command, $result); |
| 304 |
return $result; |
| 305 |
}, |
| 306 |
function (AwsException $e) use (&$errors) { |
| 307 |
$errors[$e->getCommand()[$this->info['part_num']]] = $e; |
| 308 |
return new Result(); |
| 309 |
} |
| 310 |
); |
| 311 |
}; |
| 312 |
}; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
abstract protected function getUploadCommands(callable $resultHandler); |
| 328 |
} |