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>
71 lines
2.5 KiB
PHP
71 lines
2.5 KiB
PHP
<?php
|
|
|
|
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 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): Response
|
|
{
|
|
$presentation = $media->present($element);
|
|
abort_if($presentation === null, 404);
|
|
|
|
$path = $element->website_public_path;
|
|
abort_if(! is_string($path), 404);
|
|
|
|
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;
|
|
}
|
|
}
|