Mediathek: zentrale Bildverwaltung mit Verwendungsnachweis und Bildauswahl
- 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>
This commit is contained in:
parent
3b72b5327e
commit
694ccf6ae0
46 changed files with 3101 additions and 89 deletions
189
app/Media/MediaLibrary.php
Normal file
189
app/Media/MediaLibrary.php
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
<?php
|
||||
|
||||
namespace App\Media;
|
||||
|
||||
use App\Models\Media;
|
||||
use App\Models\User;
|
||||
use App\Support\Images;
|
||||
use Illuminate\Database\UniqueConstraintViolationException;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Mediathek: jedes hochgeladene Bild wird hier als App\Models\Media erfasst.
|
||||
* Inhalte speichern weiterhin den Pfad im Speicher „uploads“ – deshalb
|
||||
* ersetzt replace() die Datei am selben Pfad, und jede Einbindung zeigt sofort
|
||||
* das neue Bild.
|
||||
*/
|
||||
class MediaLibrary
|
||||
{
|
||||
public const DISK = 'uploads';
|
||||
|
||||
/** Verzeichnis für Uploads direkt in der Mediathek bzw. über die Bildauswahl. */
|
||||
public const DIRECTORY = 'mediathek';
|
||||
|
||||
public function __construct(private readonly Images $images) {}
|
||||
|
||||
/**
|
||||
* Vorhandene Datei erfassen (idempotent über den Pfad).
|
||||
*
|
||||
* @param array{original_name?: string|null, alt?: string|null, caption?: string|null, tags?: list<string>|null, created_at?: mixed} $meta
|
||||
*/
|
||||
public function register(string $path, array $meta = [], ?User $user = null): Media
|
||||
{
|
||||
$existing = Media::query()->where('path', $path)->first();
|
||||
|
||||
if ($existing) {
|
||||
return $existing;
|
||||
}
|
||||
|
||||
$attributes = [
|
||||
...$this->fileInfo($path),
|
||||
'disk' => self::DISK,
|
||||
'path' => $path,
|
||||
'original_name' => isset($meta['original_name']) ? Str::limit((string) $meta['original_name'], 250, '') : null,
|
||||
'alt' => $meta['alt'] ?? null,
|
||||
'caption' => $meta['caption'] ?? null,
|
||||
'tags' => $meta['tags'] ?? null,
|
||||
'uploaded_by' => $user?->id,
|
||||
];
|
||||
|
||||
try {
|
||||
$media = new Media($attributes);
|
||||
|
||||
if (isset($meta['created_at'])) {
|
||||
$media->created_at = Carbon::parse($meta['created_at']);
|
||||
}
|
||||
|
||||
$media->save();
|
||||
|
||||
return $media;
|
||||
} catch (UniqueConstraintViolationException) {
|
||||
// Gleichzeitig von einer anderen Anfrage erfasst.
|
||||
return Media::query()->where('path', $path)->firstOrFail();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pfade auf Mediathek-IDs abbilden; vorhandene, noch unbekannte Bilddateien
|
||||
* werden dabei erfasst (z. B. aus Admin-Formularen oder älteren Versionen).
|
||||
*
|
||||
* @param list<string> $paths
|
||||
* @return array<string, int> Pfad => Media-ID
|
||||
*/
|
||||
public function ensureRegistered(array $paths): array
|
||||
{
|
||||
$paths = array_values(array_filter(array_unique($paths), fn ($path) => ImagePaths::isCandidate($path)));
|
||||
|
||||
if ($paths === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$known = Media::query()->whereIn('path', $paths)->pluck('id', 'path')->all();
|
||||
$disk = Storage::disk(self::DISK);
|
||||
$user = Auth::user();
|
||||
|
||||
foreach (array_diff($paths, array_keys($known)) as $path) {
|
||||
if (! $disk->exists($path)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$modified = @filemtime($disk->path($path));
|
||||
$known[$path] = $this->register($path, ['created_at' => $modified ? Carbon::createFromTimestamp($modified) : null], $user instanceof User ? $user : null)->id;
|
||||
} catch (Throwable $e) {
|
||||
report($e);
|
||||
}
|
||||
}
|
||||
|
||||
return $known;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload speichern (höchstens 2000 px breit, WebP; GIFs unverändert) und erfassen.
|
||||
*/
|
||||
public function store(UploadedFile $file, string $directory = self::DIRECTORY, ?User $user = null, array $meta = []): Media
|
||||
{
|
||||
$name = $file->getClientOriginalName();
|
||||
$path = $this->images->storeContentImage($file, trim($directory, '/'));
|
||||
|
||||
// Vorschaubild für Mediathek und Bildauswahl
|
||||
$this->images->createVariants($path, ['thumb']);
|
||||
|
||||
return $this->register($path, ['original_name' => $name, ...$meta], $user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Datei austauschen – Pfad, Alternativtext und alle Einbindungen bleiben.
|
||||
*/
|
||||
public function replace(Media $media, UploadedFile $file): Media
|
||||
{
|
||||
$name = $file->getClientOriginalName();
|
||||
$this->images->overwrite($media->path, $file);
|
||||
|
||||
if (! Storage::disk(self::DISK)->exists($this->images->variantPath($media->path, 'thumb'))) {
|
||||
$this->images->createVariants($media->path, ['thumb']);
|
||||
}
|
||||
|
||||
$media->forceFill([...$this->fileInfo($media->path), 'original_name' => Str::limit($name, 250, ''), 'updated_at' => now()])->save();
|
||||
|
||||
return $media;
|
||||
}
|
||||
|
||||
/**
|
||||
* Bild samt Varianten löschen. Einbindungen zeigen danach ihren Platzhalter.
|
||||
*/
|
||||
public function delete(Media $media): void
|
||||
{
|
||||
$this->images->deleteWithVariants($media->path);
|
||||
$media->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Datei über ihren Pfad entfernen – mit Eintrag in der Mediathek, falls vorhanden.
|
||||
*/
|
||||
public function deletePath(string $path): void
|
||||
{
|
||||
$media = Media::query()->where('path', $path)->first();
|
||||
$media ? $this->delete($media) : $this->images->deleteWithVariants($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bereits verwendete Bilder übernehmen und alle Verwendungen neu erfassen.
|
||||
*
|
||||
* @return array{sources: int, media: int}
|
||||
*/
|
||||
public function rebuild(): array
|
||||
{
|
||||
@set_time_limit(0);
|
||||
|
||||
$sources = app(MediaUsages::class)->rebuild();
|
||||
|
||||
return ['sources' => $sources, 'media' => Media::query()->count()];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{mime: string|null, size: int|null, width: int|null, height: int|null}
|
||||
*/
|
||||
private function fileInfo(string $path): array
|
||||
{
|
||||
$disk = Storage::disk(self::DISK);
|
||||
|
||||
if (! $disk->exists($path)) {
|
||||
return ['mime' => null, 'size' => null, 'width' => null, 'height' => null];
|
||||
}
|
||||
|
||||
$size = @getimagesize($disk->path($path));
|
||||
|
||||
return [
|
||||
'mime' => $size['mime'] ?? ($disk->mimeType($path) ?: null),
|
||||
'size' => $disk->size($path),
|
||||
'width' => $size[0] ?? null,
|
||||
'height' => $size[1] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue