Biete Tabellen und Bilder im gemeinsamen Texteditor an und setze sie in Ansicht und PDF
Tabellen und Bilder gehören zur festen Auswahl von FormattedText. Ein Bild ist ein Verweis auf eine freigegebene Bildfassung und kommt per Picker aus dem Medienbestand; der Stand schreibt die Bildfassung mit fest, die Rechte entscheidet DecideAccess. Der PDF-Satz (PdfFlow) ist für Übung und kommende Dokumente gemeinsam; PdfDocument kann zusätzlich Bilder, Flächen, Verweise und Lesezeichen, Belege bleiben byte-gleich. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
219962daff
commit
c57a506f85
27 files changed
+1729
-219
No files matched your search
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Catalog;
|
||||
|
||||
use App\Domain\Entitlement\DecideAccess;
|
||||
use App\Domain\Entitlement\Grant;
|
||||
use App\Domain\Media\PdfImage;
|
||||
use App\Http\SignedLink;
|
||||
use App\Models\User;
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
/**
|
||||
* Bilder in formatierten Texten (ADR-51). Ein Text verweist auf eine Bildfassung; ob und wie sie erscheint, entscheidet
|
||||
* hier wie überall `DecideAccess` für den Stand des Mitglieds: Die Bildfassung gehört zum festgeschriebenen Inhalt des
|
||||
* Stands ({@see ContentResolver}), eine Sperre wirkt, die Berechtigung des Kontos für Bilder gilt.
|
||||
*
|
||||
* - Ansicht: ein kurzlebiger, signierter Link auf die Ausgabe des Bildes (`member.media`, Aktion `ansehen`).
|
||||
* - PDF: das Bild wird mit `laden` entschieden, aus dem abgeleiteten Bestand gelesen und für das PDF aufbereitet.
|
||||
*
|
||||
* Ein Bild ohne Berechtigung ersetzt ein Hinweis an seiner Stelle; der übrige Text bleibt unverändert lesbar.
|
||||
*/
|
||||
final class EmbeddedImages
|
||||
{
|
||||
public function __construct(private readonly DecideAccess $access) {}
|
||||
|
||||
/**
|
||||
* Auflösung für die Vorschau der Redaktion: Bilder zeigt die Verwaltung über ihre eigene Dateiausgabe.
|
||||
*
|
||||
* @return Closure(int): ?array{src: string}
|
||||
*/
|
||||
public static function staff(): Closure
|
||||
{
|
||||
return fn (int $id): array => ['src' => self::staffSource($id)];
|
||||
}
|
||||
|
||||
/** Adresse einer Bildfassung für den Editor und die Vorschau der Verwaltung. */
|
||||
public static function staffSource(int $id): string
|
||||
{
|
||||
return route('content.file', ['version' => $id], false);
|
||||
}
|
||||
|
||||
/** Text für einen Editor der Verwaltung: Bilder tragen ihre Adresse, damit der Editor sie zeigt. */
|
||||
public static function forEditor(?string $value): string
|
||||
{
|
||||
return FormattedText::forEditor(FormattedText::canonical($value), fn (int $id): string => self::staffSource($id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Auflösung für {@see FormattedText::html()}.
|
||||
*
|
||||
* @return Closure(int): ?array{src?: string, alt?: string, note?: string}
|
||||
*/
|
||||
public function forView(User $user, LicenseArticle $article, ArticleRelease $release): Closure
|
||||
{
|
||||
return function (int $id) use ($user, $article, $release): ?array {
|
||||
$decision = $this->decide($user, $article, $release, $id, 'ansehen');
|
||||
|
||||
if ($decision === null) {
|
||||
return ['note' => 'Dieses Bild gibt es nicht mehr.'];
|
||||
}
|
||||
|
||||
if (! $decision['allowed']) {
|
||||
return ['note' => 'Dieses Bild ist derzeit nicht verfügbar.'];
|
||||
}
|
||||
|
||||
return ['src' => SignedLink::temporary('member.media', now()->addMinutes(10), [
|
||||
'article' => $article->id,
|
||||
'release' => $release->id,
|
||||
'version' => $id,
|
||||
'action' => 'ansehen',
|
||||
'reader' => $user->id,
|
||||
])];
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Auflösung für das PDF. Die Bilder, die tatsächlich im PDF stehen, sammelt {@see PdfImages::used()} für den
|
||||
* Ausgabenachweis.
|
||||
*/
|
||||
public function forPdf(User $user, LicenseArticle $article, ArticleRelease $release): PdfImages
|
||||
{
|
||||
return new PdfImages(function (int $id) use ($user, $article, $release): ?array {
|
||||
$decision = $this->decide($user, $article, $release, $id, 'laden');
|
||||
|
||||
if ($decision === null) {
|
||||
return ['note' => 'Bild nicht mehr vorhanden', 'grant' => null, 'version' => null];
|
||||
}
|
||||
|
||||
if (! $decision['allowed']) {
|
||||
return ['note' => 'Bild nicht verfügbar', 'grant' => null, 'version' => null];
|
||||
}
|
||||
|
||||
$file = $decision['version']->metadata['file'] ?? null;
|
||||
$disk = Storage::disk('media_derived');
|
||||
$prepared = is_string($file) && $file !== '' && ! str_contains($file, '..') && $disk->exists($file)
|
||||
? PdfImage::fromBytes((string) $disk->get($file))
|
||||
: null;
|
||||
|
||||
if ($prepared === null) {
|
||||
return ['note' => 'Bild kann nicht gedruckt werden', 'grant' => null, 'version' => null];
|
||||
}
|
||||
|
||||
return $prepared + ['grant' => $decision['grant'], 'version' => $decision['version']];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{allowed: bool, version: ContentElementVersion, grant: ?Grant}|null
|
||||
*/
|
||||
private function decide(User $user, LicenseArticle $article, ArticleRelease $release, int $id, string $action): ?array
|
||||
{
|
||||
$version = ContentElementVersion::query()->with('element')->find($id);
|
||||
|
||||
if (! $version instanceof ContentElementVersion || $version->element === null || $version->element->output_class !== 'bild') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$decision = ($this->access)($user, $article, $release, $version, $action);
|
||||
|
||||
return ['allowed' => $decision->allowed, 'version' => $version, 'grant' => $decision->grant];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user