Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 namespace Aws\S3\S3Transfer\Models;
3
4 use Aws\S3\S3Transfer\Exception\S3TransferException;
5
6 final class ResumableUpload extends AbstractResumableTransfer
7 {
8 /** @var string */
9 private string $uploadId;
10
11 /** @var array */
12 private array $partsCompleted;
13
14 /** @var string */
15 private string $source;
16
17 /** @var int */
18 private int $objectSize;
19
20 /** @var int */
21 private int $partSize;
22
23 /** @var bool */
24 private bool $isFullObjectChecksum;
25
26 /**
27 * @param string $resumeFilePath
28 * @param array $requestArgs
29 * @param array $config
30 * @param array $currentSnapshot
31 * @param string $uploadId
32 * @param array $partsCompleted
33 * @param string $source
34 * @param int $objectSize
35 * @param int $partSize
36 * @param bool $isFullObjectChecksum
37 */
38 public function __construct(
39 string $resumeFilePath,
40 array $requestArgs,
41 array $config,
42 array $currentSnapshot,
43 string $uploadId,
44 array $partsCompleted,
45 string $source,
46 int $objectSize,
47 int $partSize,
48 bool $isFullObjectChecksum
49 ) {
50 parent::__construct(
51 $resumeFilePath,
52 $requestArgs,
53 $config,
54 $currentSnapshot,
55 );
56 $this->uploadId = $uploadId;
57 $this->partsCompleted = $partsCompleted;
58 $this->source = $source;
59 $this->objectSize = $objectSize;
60 $this->partSize = $partSize;
61 $this->isFullObjectChecksum = $isFullObjectChecksum;
62 }
63
64 /**
65 * @return string
66 */
67 public function toJson(): string
68 {
69 return json_encode([
70 'version' => self::VERSION,
71 'resumeFilePath' => $this->resumeFilePath,
72 'requestArgs' => $this->requestArgs,
73 'config' => $this->config,
74 'uploadId' => $this->uploadId,
75 'partsCompleted' => $this->partsCompleted,
76 'currentSnapshot' => $this->currentSnapshot,
77 'source' => $this->source,
78 'objectSize' => $this->objectSize,
79 'partSize' => $this->partSize,
80 'isFullObjectChecksum' => $this->isFullObjectChecksum,
81 ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
82 }
83
84 /**
85 * @param string $json
86 *
87 * @return self
88 */
89 public static function fromJson(string $json): self
90 {
91 $data = json_decode($json, true);
92 if (json_last_error() !== JSON_ERROR_NONE) {
93 throw new S3TransferException('Failed to parse resume file: ' . json_last_error_msg());
94 }
95
96 $requiredFields = [
97 'version',
98 'resumeFilePath',
99 'requestArgs',
100 'config',
101 'currentSnapshot',
102 'uploadId',
103 'partsCompleted',
104 'source',
105 'objectSize',
106 'partSize',
107 'isFullObjectChecksum',
108 ];
109 foreach ($requiredFields as $field) {
110 if (!array_key_exists($field, $data)) {
111 throw new S3TransferException(
112 "Invalid resume file: missing required field '$field'"
113 );
114 }
115 }
116
117 return new self(
118 $data['resumeFilePath'],
119 $data['requestArgs'],
120 $data['config'],
121 $data['currentSnapshot'],
122 $data['uploadId'],
123 $data['partsCompleted'],
124 $data['source'],
125 $data['objectSize'],
126 $data['partSize'],
127 $data['isFullObjectChecksum'],
128 );
129 }
130
131 /**
132 * @param string $filePath
133 *
134 * @return self
135 */
136 public static function fromFile(string $filePath): self
137 {
138 if (!file_exists($filePath)) {
139 throw new S3TransferException(
140 "Resume file does not exist: $filePath"
141 );
142 }
143 $content = file_get_contents($filePath);
144 if ($content === false) {
145 throw new S3TransferException(
146 "Failed to read resume file: $filePath"
147 );
148 }
149
150 $fileData = json_decode($content, true);
151 if (json_last_error() !== JSON_ERROR_NONE) {
152 throw new S3TransferException(
153 'Failed to parse resume file: ' . json_last_error_msg()
154 );
155 }
156
157 // Validate signature if present
158 if (isset($fileData['signature'], $fileData['data'])) {
159 $expectedSignature = hash(
160 self::SIGNATURE_CHECKSUM_ALGORITHM,
161 json_encode(
162 $fileData['data'],
163 JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
164 )
165 );
166
167 if (!hash_equals($fileData['signature'], $expectedSignature)) {
168 throw new S3TransferException(
169 'Resume file integrity check failed: signature mismatch'
170 );
171 }
172
173 $json = json_encode($fileData['data']);
174 } else {
175 // Legacy format without signature
176 $json = $content;
177 }
178
179 return self::fromJson($json);
180 }
181
182 /**
183 * @return string
184 */
185 public function getUploadId(): string
186 {
187 return $this->uploadId;
188 }
189
190 /**
191 * @return array
192 */
193 public function getPartsCompleted(): array
194 {
195 return $this->partsCompleted;
196 }
197
198 /**
199 * @return string
200 */
201 public function getSource(): string
202 {
203 return $this->source;
204 }
205
206 /**
207 * @return int
208 */
209 public function getObjectSize(): int
210 {
211 return $this->objectSize;
212 }
213
214 /**
215 * @return int
216 */
217 public function getPartSize(): int
218 {
219 return $this->partSize;
220 }
221
222 /**
223 * @return bool
224 */
225 public function isFullObjectChecksum(): bool
226 {
227 return $this->isFullObjectChecksum;
228 }
229
230 /**
231 * Mark a part as completed.
232 *
233 * @param int $partNumber The part number to mark as completed
234 */
235 public function markPartCompleted(int $partNumber, array $part): void
236 {
237 $this->partsCompleted[$partNumber] = $part;
238 }
239 }