| 1 |
<?php |
| 2 |
namespace Aws\Multipart; |
| 3 |
|
| 4 |
use Aws\AwsClientInterface as Client; |
| 5 |
use Aws\Exception\AwsException; |
| 6 |
use GuzzleHttp\Psr7; |
| 7 |
use InvalidArgumentException as IAE; |
| 8 |
use Psr\Http\Message\StreamInterface as Stream; |
| 9 |
|
| 10 |
abstract class AbstractUploader extends AbstractUploadManager |
| 11 |
{ |
| 12 |
|
| 13 |
protected $source; |
| 14 |
|
| 15 |
|
| 16 |
protected $displayProgress; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public function __construct(Client $client, $source, array $config = []) |
| 24 |
{ |
| 25 |
$this->source = $this->determineSource($source); |
| 26 |
parent::__construct($client, $config); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
protected function limitPartStream(Stream $stream) |
| 38 |
{ |
| 39 |
|
| 40 |
return new Psr7\LimitStream( |
| 41 |
$stream, |
| 42 |
$this->state->getPartSize(), |
| 43 |
$this->source->tell() |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
protected function getUploadCommands(callable $resultHandler) |
| 48 |
{ |
| 49 |
|
| 50 |
$seekable = $this->source->isSeekable() |
| 51 |
&& $this->source->getMetadata('wrapper_type') === 'plainfile'; |
| 52 |
|
| 53 |
for ($partNumber = 1; $this->isEof($seekable); $partNumber++) { |
| 54 |
|
| 55 |
if (!$this->state->hasPartBeenUploaded($partNumber)) { |
| 56 |
$partStartPos = $this->source->tell(); |
| 57 |
if (!($data = $this->createPart($seekable, $partNumber))) { |
| 58 |
break; |
| 59 |
} |
| 60 |
$command = $this->client->getCommand( |
| 61 |
$this->info['command']['upload'], |
| 62 |
$data + $this->state->getId() |
| 63 |
); |
| 64 |
$command->getHandlerList()->appendSign($resultHandler, 'mup'); |
| 65 |
$numberOfParts = $this->getNumberOfParts($this->state->getPartSize()); |
| 66 |
if (isset($numberOfParts) && $partNumber > $numberOfParts) { |
| 67 |
throw new $this->config['exception_class']( |
| 68 |
$this->state, |
| 69 |
new AwsException( |
| 70 |
"Maximum part number for this job exceeded, file has likely been corrupted." . |
| 71 |
" Please restart this upload.", |
| 72 |
$command |
| 73 |
) |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
yield $command; |
| 78 |
if ($this->source->tell() > $partStartPos) { |
| 79 |
continue; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
if ($seekable) { |
| 85 |
$this->source->seek(min( |
| 86 |
$this->source->tell() + $this->state->getPartSize(), |
| 87 |
$this->source->getSize() |
| 88 |
)); |
| 89 |
} else { |
| 90 |
$this->source->read($this->state->getPartSize()); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
abstract protected function createPart($seekable, $number); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
private function isEof($seekable) |
| 114 |
{ |
| 115 |
return $seekable |
| 116 |
? $this->source->tell() < $this->source->getSize() |
| 117 |
: !$this->source->eof(); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
private function determineSource($source) |
| 131 |
{ |
| 132 |
|
| 133 |
if (is_string($source)) { |
| 134 |
$source = Psr7\Utils::tryFopen($source, 'r'); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
$stream = Psr7\Utils::streamFor($source); |
| 139 |
if (!$stream->isReadable()) { |
| 140 |
throw new IAE('Source stream must be readable.'); |
| 141 |
} |
| 142 |
|
| 143 |
return $stream; |
| 144 |
} |
| 145 |
|
| 146 |
protected function getNumberOfParts($partSize) |
| 147 |
{ |
| 148 |
if ($sourceSize = $this->source->getSize()) { |
| 149 |
return ceil($sourceSize/$partSize); |
| 150 |
} |
| 151 |
return null; |
| 152 |
} |
| 153 |
} |