Ajax
1 month ago
BackgroundProcessing
1 year ago
Dto
1 week ago
Entity
1 week ago
Exceptions
1 year ago
FileHeader
1 month ago
Interfaces
6 months ago
Job
1 month ago
Request
1 year ago
Service
1 week ago
Storage
1 month ago
Task
1 week ago
Traits
10 months ago
Utils
3 months ago
AfterRestore.php
5 months ago
BackupDeleter.php
2 months ago
BackupDownload.php
8 months ago
BackupFileIndex.php
1 year ago
BackupGlitchReason.php
1 year ago
BackupHeader.php
1 month ago
BackupRepairer.php
6 months ago
BackupRetentionHandler.php
1 month ago
BackupScheduler.php
1 week ago
BackupServiceProvider.php
1 month ago
BackupValidator.php
6 months ago
FileHeader.php
1 week ago
FileHeaderAttribute.php
2 years ago
WithBackupIdentifier.php
1 year ago
BackupRepairer.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Backup; |
| 4 | |
| 5 | use Exception; |
| 6 | use RuntimeException; |
| 7 | use UnexpectedValueException; |
| 8 | use WPStaging\Backup\Entity\BackupMetadata; |
| 9 | use WPStaging\Framework\Job\Exception\DiskNotWritableException; |
| 10 | use WPStaging\Backup\Service\BackupMetadataEditor; |
| 11 | use WPStaging\Framework\Filesystem\FileObject; |
| 12 | |
| 13 | class BackupRepairer |
| 14 | { |
| 15 | /** @var BackupMetadataEditor */ |
| 16 | private $metadataEditor; |
| 17 | |
| 18 | /** @var string|false */ |
| 19 | private $error = false; |
| 20 | |
| 21 | public function __construct(BackupMetadataEditor $metadataEditor) |
| 22 | { |
| 23 | $this->metadataEditor = $metadataEditor; |
| 24 | } |
| 25 | |
| 26 | /** @return string|false */ |
| 27 | public function getError() |
| 28 | { |
| 29 | return $this->error; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param string $filePath |
| 34 | * @return bool true on success. False on error |
| 35 | */ |
| 36 | public function repairMetadataSize($filePath) |
| 37 | { |
| 38 | $this->error = false; |
| 39 | |
| 40 | $file = null; |
| 41 | try { |
| 42 | $file = new FileObject($filePath, FileObject::MODE_APPEND_AND_READ); |
| 43 | } catch (DiskNotWritableException $ex) { |
| 44 | $this->error = $ex->getMessage(); |
| 45 | return false; |
| 46 | } catch (Exception $ex) { |
| 47 | $this->error = $ex->getMessage(); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | // Early bail if file is not wpstg |
| 52 | if ($file->getExtension() !== 'wpstg') { |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | $backupMetadata = new BackupMetadata(); |
| 57 | try { |
| 58 | $backupMetadata->hydrateByFile($file); |
| 59 | } catch (RuntimeException $ex) { |
| 60 | $this->error = $ex->getMessage(); |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | // Early bail if size is not zero |
| 65 | if ($backupMetadata->getBackupSize() !== 0) { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * The length of the backup file size in bytes is added to the total file size |
| 71 | * |
| 72 | * Before: "backupSize": "" // 2 bytes are already consumed by the string "" |
| 73 | * After: "backupSize": 123456 // 4 additional bytes are added = 6 (4+2) |
| 74 | */ |
| 75 | $fileSize = $file->getSize(); |
| 76 | if ($fileSize === false || $fileSize < 1) { |
| 77 | $this->error = __('Backup size cannot be determined or is zero', 'wp-staging'); |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | $backupSize = $fileSize - 2 + strlen((string)$fileSize); |
| 82 | if ($backupSize < 1) { |
| 83 | $this->error = __('Backup size cannot be zero or less', 'wp-staging'); |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | $backupMetadata->setBackupSize($backupSize); |
| 88 | |
| 89 | try { |
| 90 | $this->metadataEditor->setBackupMetadata($file, $backupMetadata); |
| 91 | } catch (UnexpectedValueException $ex) { |
| 92 | $this->error = $ex->getMessage(); |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | } |
| 99 |