| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\S3\S3Transfer\Models; |
| 4 |
|
| 5 |
use Aws\S3\S3Transfer\Exception\S3TransferException; |
| 6 |
use Aws\S3\S3Transfer\Progress\AbstractTransferListener; |
| 7 |
use Aws\S3\S3Transfer\S3TransferManager; |
| 8 |
use Aws\S3\S3Transfer\Utils\AbstractDownloadHandler; |
| 9 |
use Aws\S3\S3Transfer\Utils\FileDownloadHandler; |
| 10 |
use Aws\S3\S3Transfer\Utils\StreamDownloadHandler; |
| 11 |
|
| 12 |
final class DownloadRequest extends AbstractTransferRequest |
| 13 |
{ |
| 14 |
public static array $configKeys = [ |
| 15 |
'response_checksum_validation' => 'string', |
| 16 |
'multipart_download_type' => 'string', |
| 17 |
'track_progress' => 'bool', |
| 18 |
'concurrency' => 'int', |
| 19 |
'resume_enabled' => 'bool', |
| 20 |
'resume_file_path' => 'string', |
| 21 |
'target_part_size_bytes' => 'int', |
| 22 |
]; |
| 23 |
|
| 24 |
|
| 25 |
private string|array|null $source; |
| 26 |
|
| 27 |
|
| 28 |
private array $downloadRequestArgs; |
| 29 |
|
| 30 |
|
| 31 |
private ?AbstractDownloadHandler $downloadHandler; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function __construct( |
| 62 |
string|array|null $source, |
| 63 |
array $downloadRequestArgs = [], |
| 64 |
array $config = [], |
| 65 |
?AbstractDownloadHandler $downloadHandler = null, |
| 66 |
array $listeners = [], |
| 67 |
?AbstractTransferListener $progressTracker = null |
| 68 |
) { |
| 69 |
parent::__construct($listeners, $progressTracker, $config); |
| 70 |
$this->source = $source; |
| 71 |
$this->downloadRequestArgs = $downloadRequestArgs; |
| 72 |
$this->config = $config; |
| 73 |
if ($downloadHandler === null) { |
| 74 |
$downloadHandler = new StreamDownloadHandler(); |
| 75 |
} |
| 76 |
$this->downloadHandler = $downloadHandler; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static function fromDownloadRequestAndDownloadHandler( |
| 86 |
DownloadRequest $downloadRequest, |
| 87 |
FileDownloadHandler $downloadHandler |
| 88 |
): self |
| 89 |
{ |
| 90 |
return new self( |
| 91 |
$downloadRequest->getSource(), |
| 92 |
$downloadRequest->getObjectRequestArgs(), |
| 93 |
$downloadRequest->getConfig(), |
| 94 |
$downloadHandler, |
| 95 |
$downloadRequest->getListeners(), |
| 96 |
$downloadRequest->getProgressTracker() |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function getSource(): array|string|null |
| 104 |
{ |
| 105 |
return $this->source; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function getObjectRequestArgs(): array |
| 112 |
{ |
| 113 |
return $this->downloadRequestArgs; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getDownloadHandler(): AbstractDownloadHandler |
| 120 |
{ |
| 121 |
return $this->downloadHandler; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function normalizeSourceAsArray(): array |
| 132 |
{ |
| 133 |
|
| 134 |
$source = $this->getSource() ?? [ |
| 135 |
'Bucket' => $this->downloadRequestArgs['Bucket'] ?? null, |
| 136 |
'Key' => $this->downloadRequestArgs['Key'] ?? null, |
| 137 |
]; |
| 138 |
if (is_string($source)) { |
| 139 |
$sourceAsArray = S3TransferManager::s3UriAsBucketAndKey($source); |
| 140 |
} elseif (is_array($source)) { |
| 141 |
$sourceAsArray = $source; |
| 142 |
} else { |
| 143 |
throw new S3TransferException( |
| 144 |
"Unsupported source type `" . gettype($source) . "`" |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
foreach (['Bucket', 'Key'] as $reqKey) { |
| 149 |
if (empty($sourceAsArray[$reqKey])) { |
| 150 |
throw new \InvalidArgumentException( |
| 151 |
"`$reqKey` is required but not provided in " |
| 152 |
. implode(', ', array_keys($sourceAsArray)) . "." |
| 153 |
); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return $sourceAsArray; |
| 158 |
} |
| 159 |
} |