Files
fewo-stommer/app/Models/Photo.php
T
LarsandClaude Opus 5.5 1871c04507
Deploy / test (push) Failing after 2s
Deploy / deploy (push) Skipped
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>
2026-10-23 17:12:32 +02:00

99 lines
2.9 KiB
PHP

<?php
namespace App\Models;
use App\Enums\PhotoCategory;
use App\Models\Concerns\BelongsToTenant;
use Database\Factories\PhotoFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Spatie\Image\Enums\Fit;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
/**
* A website photo. The uploaded original is kept; web sizes are generated as WebP on upload.
*/
#[Fillable(['property_id', 'category', 'alt_text', 'caption', 'is_published', 'sort_order'])]
class Photo extends Model implements HasMedia
{
/** @use HasFactory<PhotoFactory> */
use BelongsToTenant, HasFactory, InteractsWithMedia;
public const COLLECTION = 'image';
/** Generated widths in px, used for srcset. */
public const WIDTHS = ['sm' => 640, 'md' => 1280, 'lg' => 2048];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'category' => PhotoCategory::class,
'is_published' => 'boolean',
];
}
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']);
}
public function registerMediaConversions(?Media $media = null): void
{
foreach (self::WIDTHS as $name => $width) {
$this->addMediaConversion($name)
->fit(Fit::Max, $width, $width)
->format('webp')
->quality(80)
->performOnCollections(self::COLLECTION);
}
}
/**
* @return BelongsTo<Property, $this>
*/
public function property(): BelongsTo
{
return $this->belongsTo(Property::class);
}
/**
* @param Builder<Photo> $query
* @return Builder<Photo>
*/
#[Scope]
protected function published(Builder $query): Builder
{
return $query->where('is_published', true)->orderBy('sort_order');
}
public function url(string $size = 'md'): string
{
return $this->getFirstMediaUrl(self::COLLECTION, $size);
}
/**
* srcset for <img>, e.g. "…-sm.webp 640w, …-md.webp 1280w, …-lg.webp 2048w".
*/
public function srcset(): string
{
return collect(self::WIDTHS)
->map(fn (int $width, string $name): string => $this->url($name).' '.$width.'w')
->implode(', ');
}
}