Refactor FileDisk system with per-disk unique names and disk assignments UI

Major changes to the file disk subsystem:

- Each FileDisk now gets a unique Laravel disk name (disk_{id}) instead
  of temp_{driver}, fixing the bug where multiple local disks with
  different roots overwrote each other's config.

- Move disk registration logic from FileDisk model to FileDiskService
  (registerDisk, getDiskName). Model keeps only getDecodedCredentials
  and a deprecated setConfig() wrapper.

- Add Disk Assignments admin UI (File Disk tab) with three purpose
  dropdowns: Media Storage, PDF Storage, Backup Storage. Stored as
  settings (media_disk_id, pdf_disk_id, backup_disk_id).

- Backup tab now uses the assigned backup disk instead of a per-backup
  dropdown. BackupsController refactored to use BackupService which
  centralizes disk resolution. Removed stale 4-second cache.

- Add local_public disk to config/filesystems.php so system disks
  are properly defined.

- Local disk roots stored relative to storage/app/ with hint text
  in the admin modal explaining the convention.

- Fix BaseModal watchEffect -> watch to prevent infinite request
  loops on the File Disk page.

- Fix string/number comparison for disk purpose IDs from settings.

- Add safeguards: prevent deleting disks with files, warn on
  purpose change, prevent deleting system disks.
This commit is contained in:
Darko Gjorgjijoski
2026-04-07 02:04:57 +02:00
parent ea1fc9b799
commit 20085cab5d
20 changed files with 492 additions and 258 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Models;
use App\Services\FileDiskService;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@@ -31,6 +32,21 @@ class FileDisk extends Model
$this->attributes['credentials'] = json_encode($value);
}
/**
* Decode credentials, handling double-encoded JSON from legacy data.
*/
public function getDecodedCredentials(): Collection
{
$decoded = json_decode($this->credentials, true);
// Handle double-encoded JSON (string inside string)
if (is_string($decoded)) {
$decoded = json_decode($decoded, true);
}
return collect($decoded ?? []);
}
public function scopeWhereOrder($query, $orderByField, $orderBy)
{
$query->orderBy($orderByField, $orderBy);
@@ -83,14 +99,14 @@ class FileDisk extends Model
/**
* Apply this disk's credentials to the filesystem configuration at runtime.
*
* @deprecated Use FileDiskService::registerDisk() instead — setConfig() mutates filesystems.default.
*/
public function setConfig(): void
{
$driver = $this->driver;
$credentials = collect(json_decode($this['credentials']));
self::setFilesystem($credentials, $driver);
$service = app(FileDiskService::class);
$diskName = $service->registerDisk($this);
config(['filesystems.default' => $diskName]);
}
/**
@@ -103,6 +119,8 @@ class FileDisk extends Model
/**
* Register a dynamic filesystem disk in the runtime configuration using the given credentials.
*
* @deprecated Use FileDisk::find($id)->registerDisk() instead.
*/
public static function setFilesystem(Collection $credentials, string $driver): void
{
@@ -118,6 +136,10 @@ class FileDisk extends Model
}
}
if ($driver === 'local' && isset($disks['root']) && ! str_starts_with($disks['root'], '/')) {
$disks['root'] = storage_path('app/'.$disks['root']);
}
config(['filesystems.disks.'.$prefix.$driver => $disks]);
}