- Weitere Adressen einer Website (z. B. www.stommer.info) leiten per 301 auf die Hauptadresse um; vorher blockierte der Browser Skripte, Schriften und Videos, die von der Hauptadresse kamen (kein Titelvideo, Ersatzschrift) - Site::origin() nimmt verbindlich das Feld domain - Videos: Abspielgeschwindigkeit 0,5x bis 1x, gilt im Titelbereich und Rundgang Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
5.0 KiB
PHP
159 lines
5.0 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use App\Models\Concerns\BelongsToTenant;
|
||
use Database\Factories\VideoFactory;
|
||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||
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 video: a short silent loop for the hero section or a tour with chapters (TourSection).
|
||
* Chapters link a moment to a floor plan room by its name, so the floor plan can jump into the video.
|
||
*
|
||
* @property list<array{at?: string, label?: string, room?: string|null}>|null $chapters
|
||
*/
|
||
#[Fillable(['property_id', 'title', 'chapters', 'playback_rate'])]
|
||
class Video extends Model implements HasMedia
|
||
{
|
||
/** @use HasFactory<VideoFactory> */
|
||
use BelongsToTenant, HasFactory, InteractsWithMedia;
|
||
|
||
public const COLLECTION = 'video';
|
||
|
||
public const POSTER = 'poster';
|
||
|
||
/** Speeds offered in the admin (rate => label); the browser plays the file at this speed everywhere. */
|
||
public const PLAYBACK_RATES = ['0.5' => 'Halb so schnell (0,5×)', '0.6' => 'Sehr ruhig (0,6×)', '0.75' => 'Ruhig (0,75×)', '0.85' => 'Etwas langsamer (0,85×)', '1' => 'Original (1×)'];
|
||
|
||
/** Largest accepted video file in KB (Strato allows uploads up to 128 MB). */
|
||
public const MAX_KB = 100 * 1024;
|
||
|
||
/**
|
||
* Get the attributes that should be cast.
|
||
*
|
||
* @return array<string, string>
|
||
*/
|
||
protected function casts(): array
|
||
{
|
||
return [
|
||
'chapters' => 'array',
|
||
'playback_rate' => 'float',
|
||
];
|
||
}
|
||
|
||
public function registerMediaCollections(): void
|
||
{
|
||
$this->addMediaCollection(self::COLLECTION)
|
||
->useDisk('public')
|
||
->singleFile()
|
||
->acceptsMimeTypes(['video/mp4', 'application/mp4', 'video/webm']);
|
||
|
||
$this->addMediaCollection(self::POSTER)
|
||
->useDisk('public')
|
||
->singleFile()
|
||
->acceptsMimeTypes(['image/jpeg', 'image/png', 'image/webp']);
|
||
}
|
||
|
||
public function registerMediaConversions(?Media $media = null): void
|
||
{
|
||
$this->addMediaConversion('poster')
|
||
->fit(Fit::Max, 1920, 1920)
|
||
->format('webp')
|
||
->quality(80)
|
||
->performOnCollections(self::POSTER);
|
||
}
|
||
|
||
/**
|
||
* @return BelongsTo<Property, $this>
|
||
*/
|
||
public function property(): BelongsTo
|
||
{
|
||
return $this->belongsTo(Property::class);
|
||
}
|
||
|
||
public function url(): ?string
|
||
{
|
||
return $this->getFirstMediaUrl(self::COLLECTION) ?: null;
|
||
}
|
||
|
||
/**
|
||
* Playback speed for the browser, limited to 0.25–2; chapter times stay in video time.
|
||
*/
|
||
public function playbackRate(): float
|
||
{
|
||
return min(2.0, max(0.25, (float) ($this->playback_rate ?: 1)));
|
||
}
|
||
|
||
public function mimeType(): string
|
||
{
|
||
return $this->getFirstMedia(self::COLLECTION)?->mime_type ?? 'video/mp4';
|
||
}
|
||
|
||
public function posterUrl(): ?string
|
||
{
|
||
return $this->getFirstMediaUrl(self::POSTER, 'poster') ?: null;
|
||
}
|
||
|
||
/**
|
||
* Chapters in time order with the start in seconds; entries without a valid time or label are left out.
|
||
*
|
||
* @return list<array{seconds: int, time: string, label: string, room: string|null}>
|
||
*/
|
||
public function chapterList(): array
|
||
{
|
||
$chapters = [];
|
||
foreach ($this->chapters ?? [] as $chapter) {
|
||
$seconds = self::seconds($chapter['at'] ?? null);
|
||
if ($seconds === null || blank($chapter['label'] ?? null)) {
|
||
continue;
|
||
}
|
||
$chapters[] = [
|
||
'seconds' => $seconds,
|
||
'time' => sprintf('%d:%02d', intdiv($seconds, 60), $seconds % 60),
|
||
'label' => (string) $chapter['label'],
|
||
'room' => filled($chapter['room'] ?? null) ? (string) $chapter['room'] : null,
|
||
];
|
||
}
|
||
|
||
usort($chapters, fn (array $a, array $b): int => $a['seconds'] <=> $b['seconds']);
|
||
|
||
return $chapters;
|
||
}
|
||
|
||
/**
|
||
* The chapter showing a floor plan room (matched by name), if any.
|
||
*
|
||
* @return array{seconds: int, time: string, label: string, room: string|null}|null
|
||
*/
|
||
public function chapterForRoom(string $room): ?array
|
||
{
|
||
foreach ($this->chapterList() as $chapter) {
|
||
if ($chapter['room'] !== null && mb_strtolower($chapter['room']) === mb_strtolower($room)) {
|
||
return $chapter;
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* "1:05" or "65" → 65; anything else → null.
|
||
*/
|
||
public static function seconds(mixed $value): ?int
|
||
{
|
||
$value = trim((string) $value);
|
||
if (preg_match('/^(\d{1,3}):([0-5]\d)$/', $value, $match)) {
|
||
return (int) $match[1] * 60 + (int) $match[2];
|
||
}
|
||
|
||
return preg_match('/^\d{1,4}$/', $value) ? (int) $value : null;
|
||
}
|
||
}
|