From 1871c04507cf27324eaf5adce3ebda3c083646a8 Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 23 Oct 2026 17:12:32 +0200 Subject: [PATCH] Fix: photos uploaded in the admin were stored privately and did not show on the website MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../Resources/Photos/Schemas/PhotoForm.php | 1 + app/Models/Photo.php | 2 + ...01_move_photo_media_to_the_public_disk.php | 48 +++++++++++++++++++ tests/Feature/WebsiteContentTest.php | 37 ++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 database/migrations/2026_10_23_171001_move_photo_media_to_the_public_disk.php diff --git a/app/Filament/Resources/Photos/Schemas/PhotoForm.php b/app/Filament/Resources/Photos/Schemas/PhotoForm.php index d4873b5..3862cdc 100644 --- a/app/Filament/Resources/Photos/Schemas/PhotoForm.php +++ b/app/Filament/Resources/Photos/Schemas/PhotoForm.php @@ -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) diff --git a/app/Models/Photo.php b/app/Models/Photo.php index b31747d..1f1201f 100644 --- a/app/Models/Photo.php +++ b/app/Models/Photo.php @@ -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']); } diff --git a/database/migrations/2026_10_23_171001_move_photo_media_to_the_public_disk.php b/database/migrations/2026_10_23_171001_move_photo_media_to_the_public_disk.php new file mode 100644 index 0000000..c6c1d0c --- /dev/null +++ b/database/migrations/2026_10_23_171001_move_photo_media_to_the_public_disk.php @@ -0,0 +1,48 @@ +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. + } +}; diff --git a/tests/Feature/WebsiteContentTest.php b/tests/Feature/WebsiteContentTest.php index 7843661..403a08a 100644 --- a/tests/Feature/WebsiteContentTest.php +++ b/tests/Feature/WebsiteContentTest.php @@ -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'));