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>
63 lines
2.2 KiB
PHP
63 lines
2.2 KiB
PHP
<?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;
|
|
}
|
|
}
|