134 lines
4.2 KiB
PHP
134 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
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\Models\User;
|
|
use Illuminate\Http\Response;
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* @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,
|
|
);
|
|
}
|
|
}
|