Die Verwaltungsrouten lieferten bei fehlender Fassung nur die volle Datei und stießen nichts an; Bestand blieb dadurch ohne kleine Fassungen. Jetzt legt der erste Abruf (höchstens einmal je Tag und Bild) einen Auftrag an. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
72 lines
2.8 KiB
PHP
72 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Concerns;
|
|
|
|
use App\Domain\Media\ImageVariants;
|
|
use App\Jobs\DeriveImageVariants;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Cache;
|
|
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');
|
|
$resolved = ImageVariants::resolve(Storage::disk($diskName), $path, is_string($size) ? $size : null);
|
|
|
|
// Bestand ohne Fassungen: Die Hauptdatei geht hinaus, und die Warteschlange legt die Fassungen an (der Web-Prozess
|
|
// schreibt nie). Höchstens einmal je Tag und Bild; ist die Quelle schmaler als die Stufe, bleibt es bei der Hauptdatei.
|
|
if ($resolved === $path && ImageVariants::isSize($size) && Cache::add('bildvarianten.'.md5($path), true, now()->addDay())) {
|
|
DeriveImageVariants::dispatch($path);
|
|
}
|
|
|
|
return $resolved;
|
|
}
|
|
|
|
/** `?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;
|
|
}
|
|
}
|