122 lines
3.7 KiB
PHP
122 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Domain\Media;
|
|
|
|
use App\Domain\Catalog\ContentElement;
|
|
use App\Domain\Catalog\ContentVersionStatus;
|
|
use App\Domain\Catalog\EditorialContentKind;
|
|
use App\Domain\Cms\Catalog\PublicMaterialReader;
|
|
use App\Jobs\PublishWebsiteMedia;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* Gibt eine Ableitung für die Website frei oder nimmt sie zurück.
|
|
* Legt keinen Lizenzartikel an und ruft DecideAccess nicht auf.
|
|
* Der Web-Prozess schreibt die öffentliche Datei nicht selbst; das tut die Warteschlange.
|
|
*/
|
|
class WebsiteRelease
|
|
{
|
|
public function release(ContentElement $element, ?User $actor = null): string
|
|
{
|
|
$refusal = $this->refusal($element);
|
|
|
|
if ($refusal !== null) {
|
|
return $refusal;
|
|
}
|
|
|
|
PublishWebsiteMedia::dispatch($element->id, 'release', $actor?->id);
|
|
|
|
return 'Für die Website freigegeben. Öffentliche Seiten lesen nur diese Ableitung.';
|
|
}
|
|
|
|
public function withdraw(ContentElement $element, ?User $actor = null): string
|
|
{
|
|
PublishWebsiteMedia::dispatch($element->id, 'withdraw', $actor?->id);
|
|
|
|
return 'Die Website-Freigabe ist zurückgenommen.';
|
|
}
|
|
|
|
public function refusal(ContentElement $element): ?string
|
|
{
|
|
if ($this->isConfidential($element)) {
|
|
return 'Traineranleitungen werden nicht für die Website freigegeben.';
|
|
}
|
|
|
|
if ($this->derivedPath($element) === null) {
|
|
return 'Für die Website fehlt eine freigebbare Ableitung. Beim Bild ist das das Vorschaubild, beim Film eine Teaserfassung unter teaser/. Die Originaldatei bleibt geschützt.';
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public function derivedPath(ContentElement $element): ?string
|
|
{
|
|
if ($this->isConfidential($element)) {
|
|
return null;
|
|
}
|
|
|
|
if ($element->output_class === 'bild') {
|
|
return $this->allowedDerivedPath($element->thumbnail_path, 'vorschau/');
|
|
}
|
|
|
|
if ($element->output_class !== 'film') {
|
|
return null;
|
|
}
|
|
|
|
$version = $element->versions()
|
|
->where('status', ContentVersionStatus::Bereit->value)
|
|
->orderByDesc('number')
|
|
->first();
|
|
|
|
if ($version === null) {
|
|
return null;
|
|
}
|
|
|
|
$path = MediaRendition::query()
|
|
->where('content_element_version_id', $version->id)
|
|
->where('role', 'preview')
|
|
->orderByDesc('id')
|
|
->value('path');
|
|
|
|
return $this->allowedDerivedPath($path, 'teaser/');
|
|
}
|
|
|
|
public function publicPath(ContentElement $element, string $derivedPath): string
|
|
{
|
|
$extension = strtolower(pathinfo($derivedPath, PATHINFO_EXTENSION));
|
|
|
|
return 'website/'.$element->id.'/datei.'.$extension;
|
|
}
|
|
|
|
private function isConfidential(ContentElement $element): bool
|
|
{
|
|
$slug = $element->editorialContentKind?->slug;
|
|
|
|
if ($slug === null && $element->editorial_content_kind_id !== null) {
|
|
$slug = EditorialContentKind::query()->whereKey($element->editorial_content_kind_id)->value('slug');
|
|
}
|
|
|
|
return in_array($slug, PublicMaterialReader::CONFIDENTIAL_KINDS, true);
|
|
}
|
|
|
|
private function allowedDerivedPath(mixed $path, string $prefix): ?string
|
|
{
|
|
if (! is_string($path) || ! str_starts_with($path, $prefix) || str_contains($path, '..')) {
|
|
return null;
|
|
}
|
|
|
|
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
|
|
|
|
if (! in_array($extension, ['jpg', 'jpeg', 'png', 'webp', 'gif', 'mp4', 'webm'], true)) {
|
|
return null;
|
|
}
|
|
|
|
if (! Storage::disk('media_derived')->exists($path)) {
|
|
return null;
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
}
|