- Tabellen media und media_usages, Modelle Media/MediaUsage - App\Media\MediaLibrary (store/register/replace/delete/rebuild) und Registry App\Media\MediaUsages (Quellen: Seiten, Neuigkeiten inkl. künftiger content-Spalte, Termine, Startseiten-Kacheln), ImagePaths-Extraktor - Alle Uploads landen in der Mediathek (Seiten-Editor direkt, übrige über Beobachter beim Speichern); Daten-Migration und media:rebuild übernehmen bestehende Bilder - Recht media.manage, Zugang media.use für alle Redaktionsbereiche - /redaktion/mediathek (Suche, Filter, Upload mit Fortschritt, Details, Eingebunden in, Ersetzen, Löschen mit Warnung, Mehrfachauswahl), Abschnitt in der Redaktion und Menüpunkt im Admin-Bereich - MediaPicker (Hochladen/Mediathek) im Pagebuilder: Bildfelder und Galerie (Mehrfachauswahl), editor.pickMedia im provide-API - Bild-URLs mit ?v=Änderungszeit, null bei fehlender Datei; Modelle löschen geteilte Bilder nicht mehr selbst Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
98 lines
2.9 KiB
PHP
98 lines
2.9 KiB
PHP
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use App\Support\Images;
|
||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||
use Illuminate\Support\Facades\Storage;
|
||
|
||
/**
|
||
* Ein Bild der Mediathek. Eingebunden wird es überall über seinen Pfad im
|
||
* Speicher „uploads“ – Inhalte speichern weiterhin den Pfad, nicht die ID.
|
||
*/
|
||
#[Fillable(['disk', 'path', 'original_name', 'mime', 'size', 'width', 'height', 'alt', 'caption', 'tags', 'uploaded_by'])]
|
||
class Media extends Model
|
||
{
|
||
protected $table = 'media';
|
||
|
||
protected function casts(): array
|
||
{
|
||
return [
|
||
'tags' => 'array',
|
||
'size' => 'integer',
|
||
'width' => 'integer',
|
||
'height' => 'integer',
|
||
];
|
||
}
|
||
|
||
/**
|
||
* @return BelongsTo<User, $this>
|
||
*/
|
||
public function uploader(): BelongsTo
|
||
{
|
||
return $this->belongsTo(User::class, 'uploaded_by');
|
||
}
|
||
|
||
/**
|
||
* @return HasMany<MediaUsage, $this>
|
||
*/
|
||
public function usages(): HasMany
|
||
{
|
||
return $this->hasMany(MediaUsage::class);
|
||
}
|
||
|
||
public function fileExists(): bool
|
||
{
|
||
return Storage::disk($this->disk ?: 'uploads')->exists($this->path);
|
||
}
|
||
|
||
/**
|
||
* Adresse des Bilds – null, wenn die Datei fehlt.
|
||
*
|
||
* @param string|null $variant „thumb“, „large“ oder null für das Original
|
||
*/
|
||
public function url(?string $variant = null): ?string
|
||
{
|
||
// „?v=“ folgt dem Änderungszeitpunkt der Datei (Ersetzen setzt ihn weiter) – so
|
||
// haben Mediathek, Seiten und Beiträge dieselbe Adresse und teilen sich den Cache.
|
||
return app(Images::class)->url($this->path, $variant);
|
||
}
|
||
|
||
public function displayName(): string
|
||
{
|
||
return $this->original_name ?: basename($this->path);
|
||
}
|
||
|
||
/**
|
||
* Für Mediathek und Bildauswahl.
|
||
*
|
||
* @return array<string, mixed>
|
||
*/
|
||
public function toLibraryArray(): array
|
||
{
|
||
$url = $this->url();
|
||
|
||
return [
|
||
'id' => $this->id,
|
||
'path' => $this->path,
|
||
'url' => $url,
|
||
'thumb_url' => $url ? $this->url('thumb') : null,
|
||
'name' => $this->displayName(),
|
||
'alt' => $this->alt ?? '',
|
||
'caption' => $this->caption ?? '',
|
||
'tags' => $this->tags ?? [],
|
||
'mime' => $this->mime,
|
||
'size' => $this->size,
|
||
'width' => $this->width,
|
||
'height' => $this->height,
|
||
'missing' => $url === null,
|
||
'uploader' => $this->relationLoaded('uploader') && $this->uploader ? ['id' => $this->uploader->id, 'name' => $this->uploader->name] : null,
|
||
'usage_count' => $this->usages_count ?? null,
|
||
'created_at' => $this->created_at?->toIso8601String(),
|
||
'updated_at' => $this->updated_at?->toIso8601String(),
|
||
];
|
||
}
|
||
}
|