Files
laraundfelix/app/Http/Controllers/ContentPreviewController.php
LarsandClaude Sonnet 5.5 5e28538551
CI Feature / test (push) Successful in 1m57s
Deploy Test / test (push) Successful in 1m53s
Deploy Test / deploy (push) Successful in 30s
Wähle das Vorschaubild auch aus der Website-Datei eines Films ohne Fassung
Die Filmstelle konnte nur aus der Datei einer Inhaltsfassung gezogen werden; ein Medium
mit reiner Website-Datei hatte keine Quelle. Jetzt zieht die Auswahl die Stelle aus der
Website-Datei (Herkunft frame:0@Sekunde, bestehende Leser bleiben gültig). Die Vorschau
des Einzelbilds liegt hinter einer Redaktions-Route.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-22 07:07:32 +01:00

177 lines
5.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Domain\Catalog\ContentElement;
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Identity\StaffRole;
use App\Domain\Media\FilmPlayback;
use App\Domain\Media\HlsPlaylist;
use App\Domain\Media\MediaRendition;
use App\Domain\Media\ThumbnailDeriver;
use App\Http\Controllers\Concerns\ServesByteRanges;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\StreamedResponse;
class ContentPreviewController extends Controller
{
use ServesByteRanges;
/**
* Die Website-Datei eines Films oder Hörstücks, damit die Redaktion sie vor der Freigabe ansehen kann. Nur für
* angemeldete Redakteure; öffentlich ist erst die freigegebene Kopie (`website.media`).
*/
public function websiteFile(Request $request, ContentElement $element): StreamedResponse
{
$this->authorizeStaff();
$path = $element->preview_path;
abort_unless(
is_string($path) && str_starts_with($path, 'teaser/') && ! str_contains($path, '..')
&& in_array($element->output_class, ['film', 'audio'], true)
&& Storage::disk('media_derived')->exists($path),
404,
);
return $this->respondWithRanges($request, 'media_derived', $path, true);
}
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',
]);
}
/**
* Einzelbild einer Stelle der Website-Datei, zur Auswahl des Vorschaubilds.
*/
public function websiteFrame(Request $request, ContentElement $element, ThumbnailDeriver $deriver): Response
{
$this->authorizeStaff();
$seconds = $request->query('sekunde');
abort_unless(is_numeric($seconds), 404);
$bytes = $deriver->jpegAtWebsiteFile($element, (float) $seconds);
abort_unless(is_string($bytes), 404);
return response($bytes, 200, [
'Content-Type' => 'image/jpeg',
'Cache-Control' => 'private, no-store',
]);
}
public function fileUrl(ContentElementVersion $version): ?string
{
return $this->storedPath($version) === null ? null : route('content.file', $version);
}
/**
* @return array{mode: string, url: string}|null
*/
public function playback(ContentElementVersion $version): ?array
{
$version->loadMissing('element');
if ($version->element?->output_class !== 'film') {
return null;
}
$mode = app(FilmPlayback::class)->mode($version->id);
$master = MediaRendition::masterPath($version->id);
if ($mode === 'hls' && is_string($master)) {
return [
'mode' => 'hls',
'url' => route('content.rendition', ['version' => $version, 'datei' => $master]),
];
}
$file = $this->fileUrl($version);
return $file === null ? null : ['mode' => 'file', 'url' => $file];
}
public function rendition(ContentElementVersion $version, string $datei): Response|StreamedResponse
{
$this->authorizeStaff();
abort_unless($this->renditionBelongsTo($version->id, $datei), 404);
$disk = Storage::disk('media_derived');
abort_unless($disk->exists($datei), 404);
if (str_ends_with($datei, '.m3u8')) {
$body = app(HlsPlaylist::class)->rewrite(
$disk->get($datei),
$datei,
fn (string $target): string => route('content.rendition', ['version' => $version, 'datei' => $target]),
);
return response($body, 200, [
'Content-Type' => 'application/vnd.apple.mpegurl',
'Cache-Control' => 'private, no-store',
]);
}
return $disk->response($datei, basename($datei), ['Content-Disposition' => 'inline']);
}
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 renditionBelongsTo(int $versionId, string $path): bool
{
$prefix = 'ableitungen/'.$versionId;
return ($path === $prefix || str_starts_with($path, $prefix.'/')) && ! str_contains($path, '..');
}
private function authorizeStaff(): void
{
$user = auth('staff')->user();
abort_unless(
$user instanceof User && ($user->hasStaffRole(StaffRole::Redakteur) || $user->hasStaffRole(StaffRole::Katalogverwalter)),
403,
);
}
}