['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()} eines Textes im Stand. * * @return Closure(int): ?array{src?: string, alt?: string, note?: string} */ public function forView(User $user, LicenseArticle $article, ArticleRelease $release, ContentElementVersion $text): Closure { return function (int $id) use ($user, $article, $release, $text): ?array { $found = $this->decide($user, $article, $release, $text, $id, 'ansehen'); if ($found === null) { return ['note' => 'Dieses Bild gibt es nicht mehr.']; } if (! $found['allowed']) { return ['note' => 'Dieses Bild ist derzeit nicht verfügbar.']; } return ['src' => route('member.stand.image', ['article' => $article->id, 'release' => $release->id, 'text' => $text->id, 'version' => $id])]; }; } /** * Die Bildfassung für die Auslieferung: nur wenn sie im Text vorkommt, zum Stand gehört, nicht gesperrt ist und der Text * für `ansehen` zulässig ist. * * @return array{version: ContentElementVersion, grant: Grant}|null */ public function serve(User $user, LicenseArticle $article, ArticleRelease $release, ContentElementVersion $text, int $id): ?array { $found = $this->decide($user, $article, $release, $text, $id, 'ansehen'); return $found !== null && $found['allowed'] && $found['grant'] instanceof Grant ? ['version' => $found['version'], 'grant' => $found['grant']] : null; } /** * Auflösung für das PDF. Ein Bild darf ins PDF, wenn ein Text, der es enthält, für `laden` zulässig ist. Die Bilder, die * tatsächlich im PDF stehen, sammelt {@see PdfImages::used()} für den Ausgabenachweis. * * @param list $texts die Texte des PDF */ public function forPdf(User $user, LicenseArticle $article, ArticleRelease $release, array $texts): PdfImages { return new PdfImages(function (int $id) use ($user, $article, $release, $texts): ?array { $decision = null; $exists = false; foreach ($texts as $text) { if (! in_array($id, FormattedText::imageIds($text->body), true)) { continue; } $found = $this->decide($user, $article, $release, $text, $id, 'laden'); $exists = $exists || $found !== null; if ($found !== null && $found['allowed']) { $decision = $found; break; } } if ($decision === null) { return ['note' => $exists ? 'Bild nicht verfügbar' : 'Bild nicht mehr vorhanden', 'grant' => null, 'version' => null]; } $prepared = self::prepared($decision['version']); if ($prepared === null) { return ['note' => 'Bild kann nicht gedruckt werden', 'grant' => null, 'version' => null]; } return $prepared + ['grant' => $decision['grant'], 'version' => $decision['version']]; }); } /** * Auflösung für die PDF-Vorschau der Redaktion: ohne Rechteprüfung, ohne Ausgabenachweis. */ public static function forStaffPdf(): PdfImages { return new PdfImages(function (int $id): ?array { $version = ContentElementVersion::query()->with('element')->find($id); if (! $version instanceof ContentElementVersion || $version->element === null || $version->element->output_class !== 'bild') { return ['note' => 'Bild nicht mehr vorhanden', 'grant' => null, 'version' => null]; } $prepared = self::prepared($version); return $prepared === null ? ['note' => 'Bild kann nicht gedruckt werden', 'grant' => null, 'version' => null] : $prepared + ['grant' => null, 'version' => null]; }); } /** Pfad der Bilddatei im abgeleiteten Bestand, wenn sie vorliegt. */ public static function file(ContentElementVersion $version): ?string { $file = $version->metadata['file'] ?? null; return is_string($file) && $file !== '' && ! str_contains($file, '..') && Storage::disk('media_derived')->exists($file) ? $file : null; } /** * Die Bilddatei aus dem abgeleiteten Bestand (der Web-Prozess liest nur dort), für das PDF aufbereitet. * * @return array{jpeg: string, width: int, height: int}|null */ private static function prepared(ContentElementVersion $version): ?array { $file = self::file($version); return $file === null ? null : PdfImage::fromBytes((string) Storage::disk('media_derived')->get($file)); } /** * @return array{allowed: bool, version: ContentElementVersion, grant: ?Grant}|null null = es gibt kein solches Bild im Text */ private function decide(User $user, LicenseArticle $article, ArticleRelease $release, ContentElementVersion $text, int $id, string $action): ?array { if (! in_array($id, FormattedText::imageIds($text->body), true)) { return null; } $version = ContentElementVersion::query()->with('element')->find($id); if (! $version instanceof ContentElementVersion || $version->element === null || $version->element->output_class !== 'bild') { return null; } $inStand = $release->contents()->where('content_element_version_id', $id)->exists(); $decision = ($this->access)($user, $article, $release, $text, $action); return [ 'allowed' => $inStand && $decision->allowed && $version->status === ContentVersionStatus::Bereit, 'version' => $version, 'grant' => $decision->grant, ]; } }