| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\S3\S3Transfer\Utils; |
| 4 |
|
| 5 |
use Aws\S3\S3Transfer\Progress\AbstractTransferListener; |
| 6 |
use GuzzleHttp\Psr7\Utils; |
| 7 |
use Psr\Http\Message\StreamInterface; |
| 8 |
|
| 9 |
final class StreamDownloadHandler extends AbstractDownloadHandler |
| 10 |
{ |
| 11 |
|
| 12 |
private ?StreamInterface $stream; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public function __construct(?StreamInterface $stream = null) |
| 18 |
{ |
| 19 |
if (is_null($stream)) { |
| 20 |
$stream = Utils::streamFor( |
| 21 |
fopen('php://temp', 'r+') |
| 22 |
); |
| 23 |
} else { |
| 24 |
|
| 25 |
$stream->seek($stream->getSize()); |
| 26 |
} |
| 27 |
|
| 28 |
$this->stream = $stream; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function priority(): int |
| 35 |
{ |
| 36 |
return -1; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public function bytesTransferred(array $context): bool |
| 43 |
{ |
| 44 |
$snapshot = $context[AbstractTransferListener::PROGRESS_SNAPSHOT_KEY]; |
| 45 |
$response = $snapshot->getResponse(); |
| 46 |
$partBody = $response['Body']; |
| 47 |
|
| 48 |
if ($partBody->isSeekable()) { |
| 49 |
$partBody->rewind(); |
| 50 |
} |
| 51 |
|
| 52 |
Utils::copyToStream( |
| 53 |
$partBody, |
| 54 |
$this->stream |
| 55 |
); |
| 56 |
|
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function transferComplete(array $context): void |
| 66 |
{ |
| 67 |
$this->stream->rewind(); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function transferFail(array $context): void |
| 76 |
{ |
| 77 |
$this->stream->close(); |
| 78 |
$this->stream = null; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function getHandlerResult(): StreamInterface |
| 87 |
{ |
| 88 |
return $this->stream; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function isConcurrencySupported(): bool |
| 95 |
{ |
| 96 |
return false; |
| 97 |
} |
| 98 |
} |