Rename local_public disk back to public and store avatars/logos on public disk
The public disk was accidentally removed during the Laravel 11 upgrade and re-added as local_public in the FileDisk refactor. Restoring the standard Laravel name avoids breaking Spatie MediaLibrary expectations and simplifies the v2-to-v3 upgrade path. User avatars and company logos now explicitly use the public disk via registerMediaCollections(), keeping them web-accessible while the default media disk remains private for sensitive documents like PDFs and receipts. The v3 upgrade migration renames the system disk entry and any alpha media records from local_public back to public.
This commit is contained in:
@@ -17,6 +17,13 @@ class Company extends Model implements HasMedia
|
||||
use HasFactory;
|
||||
use InteractsWithMedia;
|
||||
|
||||
public function registerMediaCollections(): void
|
||||
{
|
||||
$this->addMediaCollection('logo')
|
||||
->useDisk('public')
|
||||
->singleFile();
|
||||
}
|
||||
|
||||
protected $guarded = [
|
||||
'id',
|
||||
];
|
||||
|
||||
@@ -28,6 +28,13 @@ class User extends Authenticatable implements HasMedia
|
||||
use InteractsWithMedia;
|
||||
use Notifiable;
|
||||
|
||||
public function registerMediaCollections(): void
|
||||
{
|
||||
$this->addMediaCollection('admin_avatar')
|
||||
->useDisk('public')
|
||||
->singleFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
|
||||
@@ -63,7 +63,7 @@ class FileDiskService
|
||||
public function getDiskName(FileDisk $disk): string
|
||||
{
|
||||
if ($disk->isSystem()) {
|
||||
return $disk->name === 'local_public' ? 'local_public' : 'local';
|
||||
return $disk->name === 'public' ? 'public' : 'local';
|
||||
}
|
||||
|
||||
return 'disk_'.$disk->id;
|
||||
|
||||
@@ -37,7 +37,7 @@ return [
|
||||
'report' => false,
|
||||
],
|
||||
|
||||
'local_public' => [
|
||||
'public' => [
|
||||
'driver' => 'local',
|
||||
'root' => storage_path('app/public'),
|
||||
'url' => env('APP_URL').'/storage',
|
||||
|
||||
@@ -71,7 +71,7 @@ return new class extends Migration
|
||||
|
||||
FileDisk::create([
|
||||
'credentials' => json_encode($publicDisk),
|
||||
'name' => 'local_public',
|
||||
'name' => 'public',
|
||||
'type' => 'SYSTEM',
|
||||
'driver' => 'local',
|
||||
'set_as_default' => false,
|
||||
|
||||
@@ -11,6 +11,7 @@ return new class extends Migration
|
||||
public function up(): void
|
||||
{
|
||||
$this->migrateMediaDiskReferences();
|
||||
$this->renameSystemDisk();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -25,6 +26,12 @@ return new class extends Migration
|
||||
->where('disk', 'temp_local')
|
||||
->update(['disk' => 'local']);
|
||||
|
||||
// Any v3 alpha installs that stored media with the 'local_public' name
|
||||
// need updating to 'public' (the standard Laravel disk name).
|
||||
DB::table('media')
|
||||
->where('disk', 'local_public')
|
||||
->update(['disk' => 'public']);
|
||||
|
||||
// temp_s3, temp_dropbox, etc. for remote disks — map to disk_{id}
|
||||
$remotePrefixes = ['temp_s3', 'temp_dropbox', 'temp_doSpaces', 'temp_s3compat'];
|
||||
|
||||
@@ -43,8 +50,23 @@ return new class extends Migration
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The v4.0.0 migration created the system disk as 'local_public'.
|
||||
* Rename it to 'public' to match the standard Laravel disk name.
|
||||
*/
|
||||
private function renameSystemDisk(): void
|
||||
{
|
||||
DB::table('file_disks')
|
||||
->where('name', 'local_public')
|
||||
->update(['name' => 'public']);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
DB::table('file_disks')
|
||||
->where('name', 'public')
|
||||
->update(['name' => 'local_public']);
|
||||
|
||||
// Reverse: map disk_{id} back to temp_{driver}
|
||||
$fileDiskIds = DB::table('file_disks')
|
||||
->whereNotIn('type', ['SYSTEM'])
|
||||
@@ -56,6 +78,10 @@ return new class extends Migration
|
||||
->update(['disk' => 'temp_'.$disk->driver]);
|
||||
}
|
||||
|
||||
DB::table('media')
|
||||
->where('disk', 'public')
|
||||
->update(['disk' => 'local_public']);
|
||||
|
||||
DB::table('media')
|
||||
->where('disk', 'local')
|
||||
->update(['disk' => 'temp_local']);
|
||||
|
||||
Reference in New Issue
Block a user