123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- <?php
- namespace Aws\Multipart;
- use Aws\AwsClientInterface as Client;
- use Aws\CommandInterface;
- use Aws\CommandPool;
- use Aws\Exception\AwsException;
- use Aws\Exception\MultipartUploadException;
- use Aws\Result;
- use Aws\ResultInterface;
- use GuzzleHttp\Promise;
- use GuzzleHttp\Promise\PromiseInterface;
- use InvalidArgumentException as IAE;
- use Psr\Http\Message\RequestInterface;
- abstract class AbstractUploadManager implements Promise\PromisorInterface
- {
- const DEFAULT_CONCURRENCY = 5;
-
- private static $defaultConfig = [
- 'part_size' => null,
- 'state' => null,
- 'concurrency' => self::DEFAULT_CONCURRENCY,
- 'prepare_data_source' => null,
- 'before_initiate' => null,
- 'before_upload' => null,
- 'before_complete' => null,
- 'exception_class' => MultipartUploadException::class,
- ];
-
- protected $client;
-
- protected $config;
-
- protected $info;
-
- protected $promise;
-
- protected $state;
-
- public function __construct(Client $client, array $config = [])
- {
- $this->client = $client;
- $this->info = $this->loadUploadWorkflowInfo();
- $this->config = $config + self::$defaultConfig;
- $this->state = $this->determineState();
- }
-
- public function getState()
- {
- return $this->state;
- }
-
- public function upload()
- {
- return $this->promise()->wait();
- }
-
- public function promise(): PromiseInterface
- {
- if ($this->promise) {
- return $this->promise;
- }
- return $this->promise = Promise\Coroutine::of(function () {
-
- if ($this->state->isCompleted()) {
- throw new \LogicException('This multipart upload has already '
- . 'been completed or aborted.'
- );
- }
- if (!$this->state->isInitiated()) {
-
- if (is_callable($this->config["prepare_data_source"])) {
- $this->config["prepare_data_source"]();
- }
- $result = (yield $this->execCommand('initiate', $this->getInitiateParams()));
- $this->state->setUploadId(
- $this->info['id']['upload_id'],
- $result[$this->info['id']['upload_id']]
- );
- $this->state->setStatus(UploadState::INITIATED);
- }
-
-
- $resultHandler = $this->getResultHandler($errors);
- $commands = new CommandPool(
- $this->client,
- $this->getUploadCommands($resultHandler),
- [
- 'concurrency' => $this->config['concurrency'],
- 'before' => $this->config['before_upload'],
- ]
- );
-
- yield $commands->promise();
- if ($errors) {
- throw new $this->config['exception_class']($this->state, $errors);
- }
-
- yield $this->execCommand('complete', $this->getCompleteParams());
- $this->state->setStatus(UploadState::COMPLETED);
- })->otherwise($this->buildFailureCatch());
- }
- private function transformException($e)
- {
-
- if ($e instanceof AwsException) {
- $e = new $this->config['exception_class']($this->state, $e);
- }
- throw $e;
- }
- private function buildFailureCatch()
- {
- if (interface_exists("Throwable")) {
- return function (\Throwable $e) {
- return $this->transformException($e);
- };
- } else {
- return function (\Exception $e) {
- return $this->transformException($e);
- };
- }
- }
- protected function getConfig()
- {
- return $this->config;
- }
-
- abstract protected function loadUploadWorkflowInfo();
-
- abstract protected function determinePartSize();
-
- abstract protected function handleResult(
- CommandInterface $command,
- ResultInterface $result
- );
-
- abstract protected function getInitiateParams();
-
- abstract protected function getCompleteParams();
-
- private function determineState(): UploadState
- {
-
- if ($this->config['state'] instanceof UploadState) {
- return $this->config['state'];
- }
-
- $required = $this->info['id'];
- $id = [$required['upload_id'] => null];
- unset($required['upload_id']);
- foreach ($required as $key => $param) {
- if (!$this->config[$key]) {
- throw new IAE('You must provide a value for "' . $key . '" in '
- . 'your config for the MultipartUploader for '
- . $this->client->getApi()->getServiceFullName() . '.');
- }
- $id[$param] = $this->config[$key];
- }
- $state = new UploadState($id);
- $state->setPartSize($this->determinePartSize());
- return $state;
- }
-
- protected function execCommand($operation, array $params)
- {
-
- $command = $this->client->getCommand(
- $this->info['command'][$operation],
- $params + $this->state->getId()
- );
-
- if (is_callable($this->config["before_{$operation}"])) {
- $this->config["before_{$operation}"]($command);
- }
-
- return $this->client->executeAsync($command);
- }
-
- protected function getResultHandler(&$errors = [])
- {
- return function (callable $handler) use (&$errors) {
- return function (
- CommandInterface $command,
- RequestInterface $request = null
- ) use ($handler, &$errors) {
- return $handler($command, $request)->then(
- function (ResultInterface $result) use ($command) {
- $this->handleResult($command, $result);
- return $result;
- },
- function (AwsException $e) use (&$errors) {
- $errors[$e->getCommand()[$this->info['part_num']]] = $e;
- return new Result();
- }
- );
- };
- };
- }
-
- abstract protected function getUploadCommands(callable $resultHandler);
- }
|