Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace Aws\S3\S3Transfer\Models;
4
5 use Aws\S3\S3Transfer\Exception\S3TransferException;
6
7 abstract class AbstractResumableTransfer
8 {
9 protected const VERSION = '1.0';
10 protected const SIGNATURE_CHECKSUM_ALGORITHM = 'sha256';
11
12 /** @var string */
13 protected string $resumeFilePath;
14
15 /** @var array */
16 protected array $requestArgs;
17
18 /** @var array */
19 protected array $config;
20
21 /** @var array */
22 protected array $currentSnapshot;
23
24 /**
25 * @param string $resumeFilePath
26 * @param array $requestArgs The request arguments used for the transfer
27 * @param array $config The config used in the request
28 * @param array $currentSnapshot The current progress snapshot
29 */
30 public function __construct(
31 string $resumeFilePath,
32 array $requestArgs,
33 array $config,
34 array $currentSnapshot,
35 ) {
36 // Resume files must end in .resume
37 if (!str_ends_with($resumeFilePath, '.resume')) {
38 $resumeFilePath .= '.resume';
39 }
40
41 $this->resumeFilePath = $resumeFilePath;
42 $this->requestArgs = $requestArgs;
43 $this->config = $config;
44 $this->currentSnapshot = $currentSnapshot;
45 }
46
47 /**
48 * Serialize the resumable state to JSON format.
49 *
50 * @return string JSON-encoded state
51 */
52 public abstract function toJson(): string;
53
54 /**
55 * Deserialize a resumable state from JSON format.
56 *
57 * @param string $json JSON-encoded state
58 * @return self
59 * @throws S3TransferException If the JSON is invalid or missing required fields
60 */
61 public static abstract function fromJson(string $json): self;
62
63 /**
64 * Load a resumable state from a file.
65 *
66 * @param string $filePath Path to the resume file
67 * @return self
68 * @throws S3TransferException If the file cannot be read or is invalid
69 */
70 public static abstract function fromFile(string $filePath): self;
71
72 /**
73 * Save the resumable state to a file.
74 * When a file path is not provided by default it will use
75 * the `resumeFilePath` property.
76 *
77 * @param string|null $filePath Path where the resume file should be saved
78 */
79 public function toFile(?string $filePath = null): void
80 {
81 $saveFileToPath = $filePath ?? $this->resumeFilePath;
82
83 // Ensure directory exists
84 $resumeDir = dirname($saveFileToPath);
85 if (!is_dir($resumeDir)
86 && !mkdir($resumeDir, 0755, true)) {
87 throw new S3TransferException(
88 "Failed to create resume directory: $resumeDir"
89 );
90 }
91
92 $json = $this->toJson();
93 $signature = hash(self::SIGNATURE_CHECKSUM_ALGORITHM, $json);
94 $dataWithSignature = json_encode([
95 'signature' => $signature,
96 'data' => json_decode($json, true)
97 ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
98
99 $result = file_put_contents($saveFileToPath, $dataWithSignature, LOCK_EX);
100 if ($result === false) {
101 throw new S3TransferException(
102 "Failed to write resume file: $saveFileToPath"
103 );
104 }
105 }
106
107 /**
108 * @param string|null $filePath
109 *
110 * @return void
111 */
112 public function deleteResumeFile(?string $filePath = null): void
113 {
114 $resumeFilePath = $filePath ?? $this->resumeFilePath;
115 if (file_exists($resumeFilePath)) {
116 unlink($resumeFilePath);
117 }
118 }
119
120 /**
121 * @return string
122 */
123 public function getResumeFilePath(): string
124 {
125 return $this->resumeFilePath;
126 }
127
128 /**
129 * @return array
130 */
131 public function getRequestArgs(): array
132 {
133 return $this->requestArgs;
134 }
135
136 /**
137 * @return array
138 */
139 public function getConfig(): array
140 {
141 return $this->config;
142 }
143
144 /**
145 * @return string
146 */
147 public function getBucket(): string
148 {
149 return $this->requestArgs['Bucket'];
150 }
151
152 /**
153 * @return string
154 */
155 public function getKey(): string
156 {
157 return $this->requestArgs['Key'];
158 }
159
160 /**
161 * @return array
162 */
163 public function getCurrentSnapshot(): array
164 {
165 return $this->currentSnapshot;
166 }
167
168 /**
169 * Update the current snapshot.
170 *
171 * @param array $snapshot The new snapshot data
172 */
173 public function updateCurrentSnapshot(array $snapshot): void
174 {
175 $this->currentSnapshot = $snapshot;
176 }
177
178 /**
179 * Check if a file path is a valid resume file.
180 *
181 * @param string $filePath
182 * @return bool
183 */
184 public static function isResumeFile(string $filePath): bool
185 {
186 // Check file extension
187 if (!str_ends_with($filePath, '.resume')) {
188 return false;
189 }
190
191 // Check if file exists and is readable
192 if (!file_exists($filePath) || !is_readable($filePath)) {
193 return false;
194 }
195
196 // Validate file content by attempting to parse it
197 try {
198 $json = file_get_contents($filePath);
199 if ($json === false) {
200 return false;
201 }
202
203 $data = json_decode($json, true);
204 if (json_last_error() !== JSON_ERROR_NONE) {
205 return false;
206 }
207
208 // Check for required version field
209 return isset($data['data']) && isset($data['signature']);
210 } catch (\Exception $e) {
211 return false;
212 }
213 }
214 }