Lege Bildfassungen an und liefere sie zwischengespeichert aus (ADR-63)
Vorschaubilder entstehen zusätzlich als klein (360 px) und mittel (960 px), Website-Bilder in den Breiten 640, 1280 und 1920. Auswahl, Listen und Karten laden die kleinen Fassungen; die Website nutzt srcset. Adressen tragen Größe und Version und werden dauerhaft zwischengespeichert (ETag, 304). Die Website-Route läuft ohne Sitzung und Cookies. Bestand füllt sich über die Warteschlange oder mit media:varianten. Die Optionsliste des CMS fragt nicht mehr je Eintrag ab. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0492133201
commit
63b42b366c
54 files changed
+1033
-82
No files matched your search
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Concerns;
|
||||
|
||||
use App\Domain\Media\ImageVariants;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Liefert ein Bild einer Disk mit Zwischenspeicherung aus (docs/architektur/bildvarianten.md).
|
||||
*
|
||||
* Mit Versionsangabe in der Adresse (`?v=`) ändert sich die Adresse, sobald sich das Bild ändert; der Browser darf es
|
||||
* dann ein Jahr behalten. Ohne Version bleibt es bei einer Rückfrage mit ETag und Last-Modified (304 ohne Inhalt).
|
||||
*/
|
||||
trait ServesImages
|
||||
{
|
||||
/**
|
||||
* @param 'public'|'private' $scope `private` für alles, was eine Anmeldung oder Berechtigung voraussetzt
|
||||
*/
|
||||
protected function respondWithImage(Request $request, string $diskName, string $path, string $scope = 'private', bool $versioned = false): Response
|
||||
{
|
||||
$disk = Storage::disk($diskName);
|
||||
$headers = [
|
||||
'Content-Disposition' => 'inline',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
];
|
||||
$response = $disk->response($path, null, $headers);
|
||||
$modified = (int) $disk->lastModified($path);
|
||||
$size = (int) $disk->size($path);
|
||||
|
||||
$response->setEtag(md5($path.'|'.$modified.'|'.$size));
|
||||
$response->setLastModified(Carbon::createFromTimestamp($modified));
|
||||
|
||||
if ($versioned) {
|
||||
$response->headers->set('Cache-Control', $scope.', max-age=31536000, immutable');
|
||||
} else {
|
||||
$response->headers->set('Cache-Control', $scope.', max-age=0, must-revalidate');
|
||||
}
|
||||
|
||||
$response->isNotModified($request);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/** Pfad der angefragten Größe (`?g=klein|mittel`), sonst die Hauptdatei. */
|
||||
protected function imagePathFor(Request $request, string $diskName, string $path): string
|
||||
{
|
||||
$size = $request->query('g');
|
||||
|
||||
return ImageVariants::resolve(Storage::disk($diskName), $path, is_string($size) ? $size : null);
|
||||
}
|
||||
|
||||
/** `?v=` ist da und eine einfache Zahl (Zeitstempel). */
|
||||
protected function hasImageVersion(Request $request): bool
|
||||
{
|
||||
$version = $request->query('v');
|
||||
|
||||
return is_string($version) && preg_match('/^[0-9]{1,12}$/', $version) === 1;
|
||||
}
|
||||
}
|
||||
@@ -12,15 +12,18 @@
|
||||
use App\Domain\Identity\StaffRole;
|
||||
use App\Domain\Media\ThumbnailDeriver;
|
||||
use App\Filament\Support\EditorialCatalog;
|
||||
use App\Http\Controllers\Concerns\ServesImages;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\View\View;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
||||
|
||||
class EditorialPreviewController extends Controller
|
||||
{
|
||||
use ServesImages;
|
||||
|
||||
public function show(Composition $composition, ?CompositionVersion $version = null): View
|
||||
{
|
||||
$this->authorizeStaff();
|
||||
@@ -104,7 +107,7 @@ public function draft(Composition $composition): View
|
||||
);
|
||||
}
|
||||
|
||||
public function card(string $kind, int $subject): StreamedResponse
|
||||
public function card(Request $request, string $kind, int $subject): SymfonyResponse
|
||||
{
|
||||
$this->authorizeStaff();
|
||||
abort_unless(in_array($kind, ['element', 'komposition'], true), 404);
|
||||
@@ -116,7 +119,13 @@ public function card(string $kind, int $subject): StreamedResponse
|
||||
$disk = Storage::disk('media_derived');
|
||||
abort_unless($disk->exists($path), 404);
|
||||
|
||||
return $disk->response($path);
|
||||
return $this->respondWithImage(
|
||||
$request,
|
||||
'media_derived',
|
||||
$this->imagePathFor($request, 'media_derived', $path),
|
||||
'private',
|
||||
$this->hasImageVersion($request),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
use App\Domain\Media\FilmPlayback;
|
||||
use App\Domain\Media\HlsPlaylist;
|
||||
use App\Domain\Media\MediaRendition;
|
||||
use App\Http\Controllers\Concerns\ServesImages;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\CourseOutline;
|
||||
use App\Http\LibraryCatalog;
|
||||
@@ -32,10 +33,13 @@
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\View\View;
|
||||
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
class LibraryController extends Controller
|
||||
{
|
||||
use ServesImages;
|
||||
|
||||
/** Inhaltsarten der Bibliothek (Schlüssel der Adresse und des Filters). */
|
||||
public const TYPES = [
|
||||
'programm' => 'Programme',
|
||||
@@ -361,7 +365,7 @@ public function rendition(
|
||||
return $disk->response($datei, basename($datei), ['Content-Disposition' => 'inline']);
|
||||
}
|
||||
|
||||
public function thumbnail(Request $request, string $kind, int $subject): StreamedResponse
|
||||
public function thumbnail(Request $request, string $kind, int $subject): SymfonyResponse
|
||||
{
|
||||
abort_unless(in_array($kind, ['komposition', 'element'], true), 404);
|
||||
$model = $kind === 'komposition'
|
||||
@@ -373,7 +377,13 @@ public function thumbnail(Request $request, string $kind, int $subject): Streame
|
||||
$disk = Storage::disk('media_derived');
|
||||
abort_unless($disk->exists($path), 404);
|
||||
|
||||
return $disk->response($path);
|
||||
return $this->respondWithImage(
|
||||
$request,
|
||||
'media_derived',
|
||||
$this->imagePathFor($request, 'media_derived', $path),
|
||||
'private',
|
||||
$this->hasImageVersion($request),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,16 +3,22 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Domain\Catalog\ContentElement;
|
||||
use App\Domain\Media\ImageVariants;
|
||||
use App\Domain\Media\WebsiteMedia;
|
||||
use App\Http\Controllers\Concerns\ServesByteRanges;
|
||||
use App\Http\Controllers\Concerns\ServesImages;
|
||||
use App\Jobs\DeriveWebsiteVariants;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class WebsiteMediaController extends Controller
|
||||
{
|
||||
use ServesByteRanges;
|
||||
use ServesImages;
|
||||
|
||||
public function show(Request $request, ContentElement $element, WebsiteMedia $media): StreamedResponse
|
||||
public function show(Request $request, ContentElement $element, WebsiteMedia $media): Response
|
||||
{
|
||||
$presentation = $media->present($element);
|
||||
abort_if($presentation === null, 404);
|
||||
@@ -20,6 +26,45 @@ public function show(Request $request, ContentElement $element, WebsiteMedia $me
|
||||
$path = $element->website_public_path;
|
||||
abort_if(! is_string($path), 404);
|
||||
|
||||
return $this->respondWithRanges($request, 'media_public', $path, $presentation->kind !== 'bild');
|
||||
if ($presentation->kind !== 'bild') {
|
||||
return $this->respondWithRanges($request, 'media_public', $path, true);
|
||||
}
|
||||
|
||||
$path = $this->widthPath($request, $element, $path);
|
||||
// Die Adresse trägt den Zeitpunkt der Freigabe. Gilt er noch, ändert sich der Inhalt unter dieser Adresse nicht mehr
|
||||
// (eine neue Freigabe liefert eine neue Adresse); eine veraltete Adresse wird nur kurz vorgehalten und geprüft.
|
||||
$current = $this->hasImageVersion($request)
|
||||
&& (int) $request->query('v') === $element->website_released_at?->getTimestamp();
|
||||
|
||||
return $this->respondWithImage($request, 'media_public', $path, 'public', $current);
|
||||
}
|
||||
|
||||
/**
|
||||
* Die angefragte Breite (`?w=640|1280|1920`), sonst die volle Datei. Fehlt die Breite noch, geht die volle Datei hinaus,
|
||||
* und die Warteschlange legt die Breiten an (höchstens einmal je Stunde und Bild; der Web-Prozess schreibt nie).
|
||||
*/
|
||||
private function widthPath(Request $request, ContentElement $element, string $path): string
|
||||
{
|
||||
$width = $request->query('w');
|
||||
|
||||
if (! is_string($width) || preg_match('/^[0-9]{3,4}$/', $width) !== 1 || ! ImageVariants::isPublicWidth((int) $width)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
if (! in_array(strtolower(pathinfo($path, PATHINFO_EXTENSION)), ['jpg', 'jpeg'], true)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
$variant = ImageVariants::publicPath($path, (int) $width);
|
||||
|
||||
if (Storage::disk('media_public')->exists($variant)) {
|
||||
return $variant;
|
||||
}
|
||||
|
||||
if (Cache::add('bildvarianten.website.'.$element->id, true, now()->addHour())) {
|
||||
DeriveWebsiteVariants::dispatch($element->id);
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user