AbstractBackupMetadata.php
5 months ago
BackupMetadata.php
6 months ago
FileBeingExtracted.php
10 months ago
ListableBackup.php
4 months ago
MultipartMetadata.php
1 year ago
FileBeingExtracted.php
216 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup\Entity; |
| 4 | |
| 5 | use WPStaging\Backup\Interfaces\IndexLineInterface; |
| 6 | use WPStaging\Framework\Filesystem\PathIdentifier; |
| 7 | |
| 8 | /** |
| 9 | * Class FileBeingExtracted |
| 10 | * |
| 11 | * This is a OOP representation of a file being extracted. |
| 12 | * @todo Add strict types in separate PR |
| 13 | * |
| 14 | * @see \WPStaging\Backup\Service\Extractor |
| 15 | * |
| 16 | * @package WPStaging\Backup\Entity\Service |
| 17 | */ |
| 18 | class FileBeingExtracted |
| 19 | { |
| 20 | /** |
| 21 | * A string that uniquely identifies the file being extracted, e.g. wpstg_c_debug.log, wpstg_p_yoast.json, etc. |
| 22 | * @var string |
| 23 | */ |
| 24 | |
| 25 | private $identifiablePath; |
| 26 | |
| 27 | /** @var string */ |
| 28 | private $relativePath; |
| 29 | |
| 30 | /** @var int */ |
| 31 | private $start; |
| 32 | |
| 33 | /** @var int */ |
| 34 | private $totalBytes; |
| 35 | |
| 36 | /** @var int */ |
| 37 | private $writtenBytes = 0; |
| 38 | |
| 39 | /** @var int */ |
| 40 | private $readBytes = 0; |
| 41 | |
| 42 | /** @var string */ |
| 43 | protected $extractFolder; |
| 44 | |
| 45 | /** @var PathIdentifier */ |
| 46 | protected $pathIdentifier; |
| 47 | |
| 48 | /** @var int 1 if yes, 0 if no. */ |
| 49 | protected $isCompressed; |
| 50 | |
| 51 | /** @var int */ |
| 52 | protected $headerBytesRemoved = 0; |
| 53 | |
| 54 | public function __construct(string $identifiablePath, string $extractFolder, PathIdentifier $pathIdentifier, IndexLineInterface $backupFileIndex) |
| 55 | { |
| 56 | $this->identifiablePath = $identifiablePath; |
| 57 | $this->extractFolder = rtrim($extractFolder, '/') . '/'; |
| 58 | $this->start = $backupFileIndex->getContentStartOffset(); |
| 59 | $this->totalBytes = $backupFileIndex->getUnCompressedSize(); |
| 60 | $this->pathIdentifier = $pathIdentifier; |
| 61 | $this->isCompressed = (int)$backupFileIndex->getIsCompressed(); |
| 62 | $this->relativePath = $this->pathIdentifier->getPathWithoutIdentifier($this->identifiablePath); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Absolute destination path of the current file to extract. |
| 67 | * @todo rename this method to getExtractFilePath() or similar. |
| 68 | * @return string |
| 69 | */ |
| 70 | public function getBackupPath() |
| 71 | { |
| 72 | return $this->extractFolder . $this->relativePath; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Find the maximum remaining number of bytes that will be read from the file. |
| 77 | * This is limited to 512 KB to avoid reading too much data at once to prevent memory issues. |
| 78 | * |
| 79 | * @return int |
| 80 | */ |
| 81 | public function findReadTo() |
| 82 | { |
| 83 | $maxLengthToRead = 512 * KB_IN_BYTES; |
| 84 | |
| 85 | $remainingBytesToRead = $this->totalBytes - $this->readBytes; |
| 86 | |
| 87 | return max(0, min($remainingBytesToRead, $maxLengthToRead)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return string |
| 92 | */ |
| 93 | public function getPath() |
| 94 | { |
| 95 | return $this->identifiablePath; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @return string |
| 100 | */ |
| 101 | public function getRelativePath() |
| 102 | { |
| 103 | return $this->relativePath; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @return int |
| 108 | */ |
| 109 | public function getStart() |
| 110 | { |
| 111 | return $this->start; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return int |
| 116 | */ |
| 117 | public function getTotalBytes() |
| 118 | { |
| 119 | return $this->totalBytes; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @return int |
| 124 | */ |
| 125 | public function getWrittenBytes() |
| 126 | { |
| 127 | return $this->writtenBytes; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param int $writtenBytes |
| 132 | */ |
| 133 | public function setWrittenBytes($writtenBytes) |
| 134 | { |
| 135 | $this->writtenBytes = $writtenBytes; |
| 136 | } |
| 137 | |
| 138 | public function addWrittenBytes($writtenBytes) |
| 139 | { |
| 140 | $this->writtenBytes += $writtenBytes; |
| 141 | } |
| 142 | |
| 143 | public function getReadBytes(): int |
| 144 | { |
| 145 | return $this->readBytes; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @param int $readBytes |
| 150 | * @return void |
| 151 | */ |
| 152 | public function setReadBytes(int $readBytes) |
| 153 | { |
| 154 | $this->readBytes = $readBytes; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @param int $readBytes |
| 159 | * @return void |
| 160 | */ |
| 161 | public function addReadBytes(int $readBytes) |
| 162 | { |
| 163 | $this->readBytes += $readBytes; |
| 164 | } |
| 165 | |
| 166 | public function isFinished() |
| 167 | { |
| 168 | if (!$this->areHeaderBytesRemoved()) { |
| 169 | return $this->writtenBytes >= $this->totalBytes; |
| 170 | } |
| 171 | |
| 172 | return $this->writtenBytes >= ($this->totalBytes - $this->headerBytesRemoved); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @return bool|int |
| 177 | */ |
| 178 | public function getIsCompressed() |
| 179 | { |
| 180 | return $this->isCompressed; |
| 181 | } |
| 182 | |
| 183 | public function getCurrentOffset(): int |
| 184 | { |
| 185 | return $this->start + $this->readBytes; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @param int $headerBytesRemoved |
| 190 | * @return void |
| 191 | */ |
| 192 | public function addHeaderBytesRemoved(int $headerBytesRemoved) |
| 193 | { |
| 194 | $this->headerBytesRemoved += $headerBytesRemoved; |
| 195 | } |
| 196 | |
| 197 | public function getHeaderBytesRemoved(): int |
| 198 | { |
| 199 | return $this->headerBytesRemoved; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @param int $headerBytesRemoved |
| 204 | * @return void |
| 205 | */ |
| 206 | public function setHeaderBytesRemoved(int $headerBytesRemoved) |
| 207 | { |
| 208 | $this->headerBytesRemoved = $headerBytesRemoved; |
| 209 | } |
| 210 | |
| 211 | public function areHeaderBytesRemoved(): bool |
| 212 | { |
| 213 | return $this->headerBytesRemoved > 0; |
| 214 | } |
| 215 | } |
| 216 |