| 1 |
<?php |
| 2 |
|
| 3 |
namespace Aws\S3\S3Transfer\Models; |
| 4 |
|
| 5 |
use Aws\S3\S3Transfer\Exception\S3TransferException; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
final class ResumableDownload extends AbstractResumableTransfer |
| 12 |
{ |
| 13 |
|
| 14 |
private array $initialRequestResult; |
| 15 |
|
| 16 |
|
| 17 |
private array $partsCompleted; |
| 18 |
|
| 19 |
|
| 20 |
private int $totalNumberOfParts; |
| 21 |
|
| 22 |
|
| 23 |
private ?string $temporaryFile; |
| 24 |
|
| 25 |
|
| 26 |
private string $eTag; |
| 27 |
|
| 28 |
|
| 29 |
private int $objectSizeInBytes; |
| 30 |
|
| 31 |
|
| 32 |
private int $fixedPartSize; |
| 33 |
|
| 34 |
|
| 35 |
private string $destination; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct( |
| 51 |
string $resumeFilePath, |
| 52 |
array $requestArgs, |
| 53 |
array $config, |
| 54 |
array $currentSnapshot, |
| 55 |
array $initialRequestResult, |
| 56 |
array $partsCompleted, |
| 57 |
int $totalNumberOfParts, |
| 58 |
?string $temporaryFile, |
| 59 |
string $eTag, |
| 60 |
int $objectSizeInBytes, |
| 61 |
int $fixedPartSize, |
| 62 |
string $destination |
| 63 |
) { |
| 64 |
parent::__construct( |
| 65 |
$resumeFilePath, |
| 66 |
$requestArgs, |
| 67 |
$config, |
| 68 |
$currentSnapshot, |
| 69 |
); |
| 70 |
$this->initialRequestResult = $initialRequestResult; |
| 71 |
$this->partsCompleted = $partsCompleted; |
| 72 |
$this->totalNumberOfParts = $totalNumberOfParts; |
| 73 |
$this->temporaryFile = $temporaryFile; |
| 74 |
$this->eTag = $eTag; |
| 75 |
$this->objectSizeInBytes = $objectSizeInBytes; |
| 76 |
$this->fixedPartSize = $fixedPartSize; |
| 77 |
$this->destination = $destination; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function toJson(): string |
| 86 |
{ |
| 87 |
$data = [ |
| 88 |
'version' => self::VERSION, |
| 89 |
'resumeFilePath' => $this->resumeFilePath, |
| 90 |
'requestArgs' => $this->requestArgs, |
| 91 |
'config' => $this->config, |
| 92 |
'initialRequestResult' => $this->initialRequestResult, |
| 93 |
'currentSnapshot' => $this->currentSnapshot, |
| 94 |
'partsCompleted' => $this->partsCompleted, |
| 95 |
'totalNumberOfParts' => $this->totalNumberOfParts, |
| 96 |
'temporaryFile' => $this->temporaryFile, |
| 97 |
'eTag' => $this->eTag, |
| 98 |
'objectSizeInBytes' => $this->objectSizeInBytes, |
| 99 |
'fixedPartSize' => $this->fixedPartSize, |
| 100 |
'destination' => $this->destination, |
| 101 |
]; |
| 102 |
|
| 103 |
return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public static function fromJson(string $json): self |
| 114 |
{ |
| 115 |
$data = json_decode($json, true); |
| 116 |
|
| 117 |
if (json_last_error() !== JSON_ERROR_NONE) { |
| 118 |
throw new S3TransferException( |
| 119 |
'Failed to parse resume file: ' . json_last_error_msg() |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
if (!is_array($data)) { |
| 124 |
throw new S3TransferException( |
| 125 |
'Invalid resume file format: expected JSON object' |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
if (!isset($data['version']) || $data['version'] !== self::VERSION) { |
| 131 |
throw new S3TransferException( |
| 132 |
'Invalid or unsupported resume file version' |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
$requiredFields = [ |
| 138 |
'resumeFilePath', |
| 139 |
'requestArgs', |
| 140 |
'config', |
| 141 |
'initialRequestResult', |
| 142 |
'currentSnapshot', |
| 143 |
'partsCompleted', |
| 144 |
'totalNumberOfParts', |
| 145 |
'temporaryFile', |
| 146 |
'eTag', |
| 147 |
'objectSizeInBytes', |
| 148 |
'fixedPartSize', |
| 149 |
'destination', |
| 150 |
]; |
| 151 |
|
| 152 |
foreach ($requiredFields as $field) { |
| 153 |
if (!array_key_exists($field, $data)) { |
| 154 |
throw new S3TransferException( |
| 155 |
"Invalid resume file: missing required field '$field'" |
| 156 |
); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return new self( |
| 161 |
$data['resumeFilePath'], |
| 162 |
$data['requestArgs'], |
| 163 |
$data['config'], |
| 164 |
$data['currentSnapshot'], |
| 165 |
$data['initialRequestResult'], |
| 166 |
$data['partsCompleted'], |
| 167 |
$data['totalNumberOfParts'], |
| 168 |
$data['temporaryFile'], |
| 169 |
$data['eTag'], |
| 170 |
$data['objectSizeInBytes'], |
| 171 |
$data['fixedPartSize'], |
| 172 |
$data['destination'] |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public static function fromFile(string $filePath): self |
| 182 |
{ |
| 183 |
if (!file_exists($filePath)) { |
| 184 |
throw new S3TransferException( |
| 185 |
"Resume file does not exist: $filePath" |
| 186 |
); |
| 187 |
} |
| 188 |
$content = file_get_contents($filePath); |
| 189 |
if ($content === false) { |
| 190 |
throw new S3TransferException( |
| 191 |
"Failed to read resume file: $filePath" |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
$fileData = json_decode($content, true); |
| 196 |
if (json_last_error() !== JSON_ERROR_NONE) { |
| 197 |
throw new S3TransferException( |
| 198 |
'Failed to parse resume file: ' . json_last_error_msg() |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
if (isset($fileData['signature'], $fileData['data'])) { |
| 204 |
$expectedSignature = hash( |
| 205 |
self::SIGNATURE_CHECKSUM_ALGORITHM, |
| 206 |
json_encode( |
| 207 |
$fileData['data'], |
| 208 |
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES |
| 209 |
) |
| 210 |
); |
| 211 |
|
| 212 |
if (!hash_equals($fileData['signature'], $expectedSignature)) { |
| 213 |
throw new S3TransferException( |
| 214 |
'Resume file integrity check failed: signature mismatch' |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
$json = json_encode($fileData['data']); |
| 219 |
} else { |
| 220 |
|
| 221 |
$json = $content; |
| 222 |
} |
| 223 |
|
| 224 |
return self::fromJson($json); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public function getInitialRequestResult(): array |
| 231 |
{ |
| 232 |
return $this->initialRequestResult; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public function getPartsCompleted(): array |
| 239 |
{ |
| 240 |
return $this->partsCompleted; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public function getTotalNumberOfParts(): int |
| 247 |
{ |
| 248 |
return $this->totalNumberOfParts; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function getTemporaryFile(): ?string |
| 255 |
{ |
| 256 |
return $this->temporaryFile; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public function getETag(): string |
| 263 |
{ |
| 264 |
return $this->eTag; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function getObjectSizeInBytes(): int |
| 271 |
{ |
| 272 |
return $this->objectSizeInBytes; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public function getFixedPartSize(): int |
| 279 |
{ |
| 280 |
return $this->fixedPartSize; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public function getDestination(): string |
| 287 |
{ |
| 288 |
return $this->destination; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public function markPartCompleted(int $partNumber): void |
| 297 |
{ |
| 298 |
$this->partsCompleted[$partNumber] = true; |
| 299 |
} |
| 300 |
} |