Fix: photos uploaded in the admin were stored privately and did not show on the website
The admin upload field used Filament's default disk (FILESYSTEM_DISK=local, private), while imports and the website use the public disk – a photo uploaded in the admin appeared in the photo list but its web sizes returned 403 (e.g. as floor plan picture). The photo collection and the upload field now always use the public disk; a data migration moves photos already stored privately (original and web sizes, folder = media id) to the public disk. Tests for the upload and the migration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
dfd3f77a5d
commit
1871c04507
4 files changed
+88
No files matched your search
@@ -21,6 +21,7 @@ public static function configure(Schema $schema): Schema
|
||||
SpatieMediaLibraryFileUpload::make('image')
|
||||
->label('Bild')
|
||||
->collection(Photo::COLLECTION)
|
||||
->disk('public')
|
||||
->image()
|
||||
->imageEditor()
|
||||
->maxSize(25 * 1024)
|
||||
|
||||
@@ -45,7 +45,9 @@ protected function casts(): array
|
||||
|
||||
public function registerMediaCollections(): void
|
||||
{
|
||||
// Website photos are public files; the app's default disk (FILESYSTEM_DISK=local) is private.
|
||||
$this->addMediaCollection(self::COLLECTION)
|
||||
->useDisk('public')
|
||||
->singleFile()
|
||||
->acceptsMimeTypes(['image/jpeg', 'image/png', 'image/webp']);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Photo;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Photos uploaded in the admin landed on the private default disk ("local"), so the website could not
|
||||
* show them. Moves their files (original and web sizes, folder = media id) to the public disk.
|
||||
* Safe to run again: only media not yet on the public disk are touched.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$media = DB::table('media')
|
||||
->where('model_type', Photo::class)
|
||||
->where(fn ($query) => $query->where('disk', '!=', 'public')->orWhere('conversions_disk', '!=', 'public'))
|
||||
->get(['id', 'disk', 'conversions_disk']);
|
||||
|
||||
foreach ($media as $item) {
|
||||
$folder = (string) $item->id;
|
||||
$public = Storage::disk('public');
|
||||
$disks = array_values(array_diff(array_unique([$item->disk, $item->conversions_disk ?: $item->disk]), ['public']));
|
||||
|
||||
if (array_filter($disks, fn (string $diskName): bool => config("filesystems.disks.{$diskName}") === null) !== []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($disks as $diskName) {
|
||||
|
||||
$source = Storage::disk($diskName);
|
||||
foreach ($source->allFiles($folder) as $file) {
|
||||
$public->writeStream($file, $source->readStream($file));
|
||||
}
|
||||
$source->deleteDirectory($folder);
|
||||
}
|
||||
|
||||
DB::table('media')->where('id', $item->id)->update(['disk' => 'public', 'conversions_disk' => 'public']);
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
// Files stay public; moving them back to a private disk would hide them from the website again.
|
||||
}
|
||||
};
|
||||
@@ -8,6 +8,7 @@
|
||||
use App\Filament\Resources\Amenities\AmenityResource;
|
||||
use App\Filament\Resources\Faqs\FaqResource;
|
||||
use App\Filament\Resources\Notices\NoticeResource;
|
||||
use App\Filament\Resources\Photos\Pages\CreatePhoto;
|
||||
use App\Filament\Resources\Photos\Pages\ListPhotos;
|
||||
use App\Filament\Resources\Photos\PhotoResource;
|
||||
use App\Filament\Resources\Properties\PropertyResource;
|
||||
@@ -87,6 +88,42 @@ public function test_several_photos_are_uploaded_at_once(): void
|
||||
$this->assertSame(['Wohn zimmer', 'Kueche'], Photo::orderBy('sort_order')->pluck('alt_text')->all());
|
||||
}
|
||||
|
||||
public function test_photo_uploaded_in_the_admin_is_stored_on_the_public_disk(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
|
||||
Livewire::test(CreatePhoto::class)
|
||||
->fillForm([
|
||||
'image' => [UploadedFile::fake()->image('grundriss.png', 1600, 900)],
|
||||
'alt_text' => 'Grundriss mit Teppichen',
|
||||
'category' => PhotoCategory::Apartment,
|
||||
])
|
||||
->call('create')
|
||||
->assertHasNoFormErrors();
|
||||
|
||||
$media = Photo::query()->where('alt_text', 'Grundriss mit Teppichen')->sole()->getFirstMedia(Photo::COLLECTION);
|
||||
$this->assertSame(['public', 'public'], [$media->disk, $media->conversions_disk]);
|
||||
Storage::disk('public')->assertExists($media->id.'/'.$media->file_name);
|
||||
$this->assertSame([], Storage::disk('local')->allFiles());
|
||||
}
|
||||
|
||||
public function test_migration_moves_photos_from_the_private_disk_to_the_public_disk(): void
|
||||
{
|
||||
Storage::fake('local');
|
||||
$photo = Photo::factory()->create();
|
||||
$media = $photo->addMedia(UploadedFile::fake()->image('alt.jpg', 1200, 800))->toMediaCollection(Photo::COLLECTION, 'local');
|
||||
Storage::disk('local')->assertExists($media->id.'/alt.jpg');
|
||||
|
||||
(require database_path('migrations/2026_10_23_171001_move_photo_media_to_the_public_disk.php'))->up();
|
||||
|
||||
$media->refresh();
|
||||
$this->assertSame(['public', 'public'], [$media->disk, $media->conversions_disk]);
|
||||
Storage::disk('public')->assertExists($media->id.'/alt.jpg');
|
||||
Storage::disk('public')->assertExists($media->id.'/conversions/alt-lg.webp');
|
||||
Storage::disk('local')->assertMissing($media->id.'/alt.jpg');
|
||||
$this->assertStringEndsWith('/conversions/alt-lg.webp', $photo->fresh()->url('lg'));
|
||||
}
|
||||
|
||||
public function test_notices_are_visible_only_within_their_dates(): void
|
||||
{
|
||||
$this->travelTo(CarbonImmutable::parse('2026-11-10'));
|
||||
|
||||
Reference in new issue
Block a user