Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/S3/S3Transfer/Models/ResumableDownload.php

raw symbols SCIP
1 <?php
2
3 namespace Aws\S3\S3Transfer\Models;
4
5 use Aws\S3\S3Transfer\Exception\S3TransferException;
6
7 /**
8 * Represents the state of a resumable multipart download.
9 * This class can be serialized to/from JSON to persist download progress.
10 */
11 final class ResumableDownload extends AbstractResumableTransfer
12 {
13 /** @var array */
14 private array $initialRequestResult;
15
16 /** @var array */
17 private array $partsCompleted;
18
19 /** @var int */
20 private int $totalNumberOfParts;
21
22 /** @var string|null */
23 private ?string $temporaryFile;
24
25 /** @var string */
26 private string $eTag;
27
28 /** @var int */
29 private int $objectSizeInBytes;
30
31 /** @var int */
32 private int $fixedPartSize;
33
34 /** @var string */
35 private string $destination;
36
37 /**
38 * @param array $requestArgs The request arguments used for the download
39 * @param array $config The config used in the request
40 * @param array $initialRequestResult The response from the initial request
41 * @param array $currentSnapshot The current progress snapshot
42 * @param array $partsCompleted Map of completed part numbers (partNo => true)
43 * @param int $totalNumberOfParts Total number of parts in the download
44 * @param string|null $temporaryFile Path to the temporary file being downloaded to
45 * @param string $eTag ETag of the S3 object for consistency verification
46 * @param int $objectSizeInBytes Total size of the object in bytes
47 * @param int $fixedPartSize Size of each part in bytes
48 * @param string $destination Final destination path for the downloaded file
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 * Serialize the resumable download state to JSON format.
82 *
83 * @return string JSON-encoded state
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 * Deserialize a resumable download state from JSON format.
108 *
109 * @param string $json JSON-encoded state
110 * @return self
111 * @throws S3TransferException If the JSON is invalid or missing required fields
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 // Validate version
130 if (!isset($data['version']) || $data['version'] !== self::VERSION) {
131 throw new S3TransferException(
132 'Invalid or unsupported resume file version'
133 );
134 }
135
136 // Validate required fields
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 * @param string $filePath
178 *
179 * @return self
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 // Validate signature if present
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 // Legacy format without signature
221 $json = $content;
222 }
223
224 return self::fromJson($json);
225 }
226
227 /**
228 * @return array
229 */
230 public function getInitialRequestResult(): array
231 {
232 return $this->initialRequestResult;
233 }
234
235 /**
236 * @return array
237 */
238 public function getPartsCompleted(): array
239 {
240 return $this->partsCompleted;
241 }
242
243 /**
244 * @return int
245 */
246 public function getTotalNumberOfParts(): int
247 {
248 return $this->totalNumberOfParts;
249 }
250
251 /**
252 * @return string|null
253 */
254 public function getTemporaryFile(): ?string
255 {
256 return $this->temporaryFile;
257 }
258
259 /**
260 * @return string
261 */
262 public function getETag(): string
263 {
264 return $this->eTag;
265 }
266
267 /**
268 * @return int
269 */
270 public function getObjectSizeInBytes(): int
271 {
272 return $this->objectSizeInBytes;
273 }
274
275 /**
276 * @return int
277 */
278 public function getFixedPartSize(): int
279 {
280 return $this->fixedPartSize;
281 }
282
283 /**
284 * @return string
285 */
286 public function getDestination(): string
287 {
288 return $this->destination;
289 }
290
291 /**
292 * Mark a part as completed.
293 *
294 * @param int $partNumber The part number to mark as completed
295 */
296 public function markPartCompleted(int $partNumber): void
297 {
298 $this->partsCompleted[$partNumber] = true;
299 }
300 }