Files
laraundfelix/app/Http/Controllers/ContentPreviewController.php
T
LarsandCursor af85a94cd5
Deploy Test / test (push) Successful in 15s
Deploy Test / deploy (push) Successful in 22s
Zeige Medienfassungen in der Verwaltung und halte Beschreibungen davon getrennt.
Eine Fassung bleibt nummeriert und kann deaktiviert werden. Beschreibung und fachliche Säulen gehören zum Medium und legen keine neue Fassung an.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-15 11:25:08 +01:00

74 lines
2.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Identity\StaffRole;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\StreamedResponse;
class ContentPreviewController extends Controller
{
public function show(ContentElementVersion $version): View
{
$this->authorizeStaff();
$version->loadMissing('element');
return view('content.preview', [
'version' => $version,
'fileUrl' => $this->fileUrl($version),
'pending' => $this->isPending($version),
]);
}
public function file(ContentElementVersion $version): StreamedResponse
{
$this->authorizeStaff();
$path = $this->storedPath($version);
abort_unless(is_string($path), 404);
return Storage::disk('media_derived')->response($path, basename($path), [
'Content-Disposition' => 'inline',
]);
}
public function fileUrl(ContentElementVersion $version): ?string
{
return $this->storedPath($version) === null ? null : route('content.file', $version);
}
public function isPending(ContentElementVersion $version): bool
{
if ($this->storedPath($version) !== null) {
return false;
}
$metadata = is_array($version->metadata) ? $version->metadata : [];
$incoming = $metadata['incoming'] ?? null;
return (is_string($incoming) && $incoming !== '') || is_array($incoming);
}
private function storedPath(ContentElementVersion $version): ?string
{
$file = is_array($version->metadata) ? ($version->metadata['file'] ?? null) : null;
if (! is_string($file) || $file === '' || str_contains($file, '..') || ! str_starts_with($file, 'inhalte/')) {
return null;
}
return Storage::disk('media_derived')->exists($file) ? $file : null;
}
private function authorizeStaff(): void
{
$user = auth('staff')->user();
abort_unless(
$user instanceof User && ($user->hasStaffRole(StaffRole::Redakteur) || $user->hasStaffRole(StaffRole::Katalogverwalter)),
403,
);
}
}