Skip to content

A free open source IT asset/license management system

Added Backups to enum for consistency

snipe1c3b7aba7aef
diff --git a/app/Console/Commands/RestoreFromBackup.php b/app/Console/Commands/RestoreFromBackup.php
index e77920533e..d1861f7996 100644
--- a/app/Console/Commands/RestoreFromBackup.php
+++ b/app/Console/Commands/RestoreFromBackup.php
@@ -294,13 +294,17 @@ class RestoreFromBackup extends Command
}
// Upload directory enumeration lives in App\Enums\FileStorage
- // as the single source of truth.
- $private_dirs = FileStorage::privateDirs();
+ // as the single source of truth. The *ForRestore variants omit
+ // directories in FileStorage::SKIP_IN_RESTORE_PRUNE, so Spatie's
+ // backup archive directory (which may contain the archive we're
+ // currently reading from, plus the user's other rollback points)
+ // is left untouched by the pruning passes below.
+ $private_dirs = FileStorage::privateDirsForRestore();
$private_files = [
'storage/oauth-private.key',
'storage/oauth-public.key',
];
- $public_dirs = FileStorage::publicDirs();
+ $public_dirs = FileStorage::publicDirsForRestore();
$public_files = self::PUBLIC_FILES;
diff --git a/app/Enums/FileStorage.php b/app/Enums/FileStorage.php
index b79270d4ab..286840c37f 100644
--- a/app/Enums/FileStorage.php
+++ b/app/Enums/FileStorage.php
@@ -27,6 +27,7 @@ enum FileStorage: string
case Assets = 'assets';
case Audits = 'audits';
case Avatars = 'avatars';
+ case Backups = 'backups';
case Barcodes = 'barcodes';
case Categories = 'categories';
case Companies = 'companies';
@@ -56,6 +57,19 @@ enum FileStorage: string
'asset_maintenances' => 'maintenances',
];
+ /**
+ * Subdir names enumerated by publicDirs() / privateDirs() that the
+ * restore command must leave untouched. Backups live here because
+ * wiping storage/app/backups during a restore would race against
+ * the archive being read and destroy sibling rollback points.
+ * Entries match the enum backing value.
+ *
+ * @var array<int, string>
+ */
+ private const SKIP_IN_RESTORE_PRUNE = [
+ 'backups',
+ ];
+
public function hasPublicScope(): bool
{
return match ($this) {
@@ -83,6 +97,7 @@ enum FileStorage: string
self::Accessories,
self::Assets,
self::Audits,
+ self::Backups,
self::Components,
self::Consumables,
self::EulaPdfs,
@@ -126,11 +141,20 @@ enum FileStorage: string
/**
* Full private directory, relative to the repo root. For restore
- * extraction + pruning.
+ * extraction + pruning. Backups live under Spatie's normal
+ * storage/app/backups rather than the private_uploads tree, which
+ * every other private case uses.
*/
public function privateDir(): ?string
{
- return $this->hasPrivateScope() ? 'storage/private_uploads/'.$this->value : null;
+ if (!$this->hasPrivateScope()) {
+ return null;
+ }
+
+ return match ($this) {
+ self::Backups => 'storage/app/backups',
+ default => 'storage/private_uploads/' . $this->value,
+ };
}
/**
@@ -177,4 +201,40 @@ enum FileStorage: string
return $out;
}
+
+ /**
+ * Public directories excluding those in SKIP_IN_RESTORE_PRUNE.
+ * Only RestoreFromBackup should reach for this. Any other caller
+ * that wants the full public enumeration uses publicDirs().
+ *
+ * @return list<string>
+ */
+ public static function publicDirsForRestore(): array
+ {
+ return array_values(array_filter(
+ self::publicDirs(),
+ fn(string $dir) => !in_array(basename($dir), self::SKIP_IN_RESTORE_PRUNE, true),
+ ));
+ }
+
+ /**
+ * Private directories excluding those in SKIP_IN_RESTORE_PRUNE.
+ * Alias entries are matched by their alias key, not their
+ * destination value, because the alias key is what carries the
+ * on-disk subdir name of a historical backup.
+ *
+ * @return array<int|string, string>
+ */
+ public static function privateDirsForRestore(): array
+ {
+ return array_filter(
+ self::privateDirs(),
+ fn(string $dir, int|string $key) => !in_array(
+ basename(is_string($key) ? $key : $dir),
+ self::SKIP_IN_RESTORE_PRUNE,
+ true,
+ ),
+ ARRAY_FILTER_USE_BOTH,
+ );
+ }
}
diff --git a/tests/Unit/Enums/FileStorageTest.php b/tests/Unit/Enums/FileStorageTest.php
index 0fe4f06273..796973e11d 100644
--- a/tests/Unit/Enums/FileStorageTest.php
+++ b/tests/Unit/Enums/FileStorageTest.php
@@ -74,4 +74,41 @@ class FileStorageTest extends TestCase
}
}
}
+
+ public function test_backups_resolve_to_spatie_dir_not_private_uploads_tree(): void
+ {
+ $this->assertTrue(FileStorage::Backups->hasPrivateScope());
+ $this->assertFalse(FileStorage::Backups->hasPublicScope());
+ $this->assertSame('storage/app/backups', FileStorage::Backups->privateDir());
+ $this->assertNull(FileStorage::Backups->publicDir());
+ }
+
+ public function test_backups_appear_in_full_private_dirs_enumeration(): void
+ {
+ // publicDirs() / privateDirs() are the authoritative "every dir
+ // of that scope" list. Backups has to be here so a future caller
+ // enumerating private storage locations sees it.
+ $this->assertContains('storage/app/backups', FileStorage::privateDirs());
+ }
+
+ public function test_restore_filtered_enumerations_exclude_backups(): void
+ {
+ // The restore pruning passes must leave Spatie's backup directory
+ // alone. Otherwise the archive being read from, plus every
+ // sibling rollback point, would be wiped mid-restore.
+ $this->assertNotContains('storage/app/backups', FileStorage::privateDirsForRestore());
+ $this->assertNotContains('storage/app/backups', FileStorage::publicDirsForRestore());
+ }
+
+ public function test_restore_filtered_private_enumeration_still_contains_regular_private_cases(): void
+ {
+ $pruneable = FileStorage::privateDirsForRestore();
+ $this->assertContains('storage/private_uploads/signatures', $pruneable);
+ $this->assertContains('storage/private_uploads/users', $pruneable);
+ $this->assertContains('storage/private_uploads/eula-pdfs', $pruneable);
+
+ // Historical alias entries carry through as string-keyed pairs
+ // because older archives still ship the old subdir names.
+ $this->assertArrayHasKey('storage/private_uploads/assetmodels', $pruneable);
+ }
}