| 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 |
|
| 13 |
protected string $resumeFilePath; |
| 14 |
|
| 15 |
|
| 16 |
protected array $requestArgs; |
| 17 |
|
| 18 |
|
| 19 |
protected array $config; |
| 20 |
|
| 21 |
|
| 22 |
protected array $currentSnapshot; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function __construct( |
| 31 |
string $resumeFilePath, |
| 32 |
array $requestArgs, |
| 33 |
array $config, |
| 34 |
array $currentSnapshot, |
| 35 |
) { |
| 36 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public abstract function toJson(): string; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static abstract function fromJson(string $json): self; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static abstract function fromFile(string $filePath): self; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function toFile(?string $filePath = null): void |
| 80 |
{ |
| 81 |
$saveFileToPath = $filePath ?? $this->resumeFilePath; |
| 82 |
|
| 83 |
|
| 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 |
|
| 109 |
|
| 110 |
|
| 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 |
|
| 122 |
|
| 123 |
public function getResumeFilePath(): string |
| 124 |
{ |
| 125 |
return $this->resumeFilePath; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function getRequestArgs(): array |
| 132 |
{ |
| 133 |
return $this->requestArgs; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function getConfig(): array |
| 140 |
{ |
| 141 |
return $this->config; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function getBucket(): string |
| 148 |
{ |
| 149 |
return $this->requestArgs['Bucket']; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function getKey(): string |
| 156 |
{ |
| 157 |
return $this->requestArgs['Key']; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function getCurrentSnapshot(): array |
| 164 |
{ |
| 165 |
return $this->currentSnapshot; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function updateCurrentSnapshot(array $snapshot): void |
| 174 |
{ |
| 175 |
$this->currentSnapshot = $snapshot; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public static function isResumeFile(string $filePath): bool |
| 185 |
{ |
| 186 |
|
| 187 |
if (!str_ends_with($filePath, '.resume')) { |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
if (!file_exists($filePath) || !is_readable($filePath)) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 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 |
|
| 209 |
return isset($data['data']) && isset($data['signature']); |
| 210 |
} catch (\Exception $e) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
} |
| 214 |
} |