Merge remote-tracking branch 'origin/develop' into feature/bildausschnitt
CI Feature / test (push) Successful in 7m24s
Deploy Test / test (push) Successful in 7m35s
Deploy Test / deploy (push) Successful in 44s

This commit is contained in:
Lars committed 2026-12-01 12:15:39 +01:00
commit 05dc1170aa
8 files changed
+293 -60

No files matched your search

+2 -1
View File
@@ -53,6 +53,7 @@ public function writeUnlessRecent(
string $action,
string $format,
string $marking,
string|\Closure|null $checksum = null,
): bool {
$recent = MediaDelivery::query()
->where('grant_id', $grant->id)
@@ -65,7 +66,7 @@ public function writeUnlessRecent(
return false;
}
$this->write($grant, $article, $release, $version, $action, $format, null, $marking);
$this->write($grant, $article, $release, $version, $action, $format, $checksum instanceof \Closure ? $checksum() : $checksum, $marking);
return true;
}
+48
View File
@@ -0,0 +1,48 @@
<?php
namespace App\Domain\Media;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
/**
* SHA-256 einer Datei der Medienablage für den Auslieferungsnachweis (08 §7), je Datei einmal berechnet.
*
* Die Dateien sind nach dem Speichern unveränderlich. Der gemerkte Wert gilt daher für Pfad, Größe und Änderungszeit;
* ändert sich eine davon, wird neu gerechnet. Ist die Datei nicht lesbar, ist das Ergebnis null (der Nachweis nennt
* dann keine Prüfsumme, statt die Auslieferung zu blockieren).
*/
class FileChecksum
{
public function of(string $diskName, string $path): ?string
{
$disk = Storage::disk($diskName);
try {
if (! $disk->exists($path)) {
return null;
}
$key = 'media-checksum:'.hash('sha256', $diskName.'|'.$path.'|'.$disk->size($path).'|'.$disk->lastModified($path));
return Cache::rememberForever($key, function () use ($disk, $path): ?string {
$stream = $disk->readStream($path);
if (! is_resource($stream)) {
return null;
}
try {
$context = hash_init('sha256');
hash_update_stream($context, $stream);
return hash_final($context);
} finally {
fclose($stream);
}
});
} catch (\Throwable) {
return null;
}
}
}
@@ -4,6 +4,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
@@ -14,17 +15,29 @@ trait ServesByteRanges
{
private const CHUNK = 262144;
protected function respondWithRanges(Request $request, string $diskName, string $path, bool $ranged): StreamedResponse
{
/**
* @param string|null $name Dateiname für Ansicht oder Download (ohne: Name der Datei)
* @param string $disposition inline oder attachment
* @param array<string, string> $extra weitere Kopfzeilen
*/
protected function respondWithRanges(
Request $request,
string $diskName,
string $path,
bool $ranged,
?string $name = null,
string $disposition = 'inline',
array $extra = [],
): StreamedResponse {
$headers = [
'Content-Disposition' => 'inline',
'Content-Disposition' => $name === null ? $disposition : $this->disposition($disposition, $name),
'X-Content-Type-Options' => 'nosniff',
'Accept-Ranges' => 'bytes',
];
] + $extra;
$range = $request->header('Range');
if (! is_string($range) || ! $ranged) {
return Storage::disk($diskName)->response($path, null, $headers);
return Storage::disk($diskName)->response($path, $name, $headers, $disposition);
}
$disk = Storage::disk($diskName);
@@ -63,6 +76,13 @@ protected function respondWithRanges(Request $request, string $diskName, string
]);
}
private function disposition(string $type, string $name): string
{
$fallback = (string) preg_replace('/[^ -~]|["\\%]/', '_', $name);
return HeaderUtils::makeDisposition($type, $name, $fallback);
}
/**
* Erster Bereich aus „bytes=a-b“, „bytes=a-“ oder „bytes=-n“; null, wenn er nicht erfüllbar ist.
*
@@ -17,10 +17,12 @@
use App\Domain\Entitlement\Grant;
use App\Domain\Entitlement\GrantStatus;
use App\Domain\Media\DeliveryLog;
use App\Domain\Media\FileChecksum;
use App\Domain\Media\FilmPlayback;
use App\Domain\Media\HlsPlaylist;
use App\Domain\Media\MediaRendition;
use App\Domain\Media\StreamLifetime;
use App\Http\Controllers\Concerns\ServesByteRanges;
use App\Http\Controllers\Concerns\ServesImages;
use App\Http\Controllers\Controller;
use App\Http\CourseOutline;
@@ -40,6 +42,7 @@
class LibraryController extends Controller
{
use ServesByteRanges;
use ServesImages;
/** Inhaltsarten der Bibliothek (Schlüssel der Adresse und des Filters). */
@@ -252,9 +255,6 @@ public function show(Request $request, LicenseArticle $article): View
'action' => $action,
'allowed' => $decision->allowed,
'reason' => $decision->reason,
'decision' => is_numeric($metadata['decision_seconds'] ?? null)
? (int) $metadata['decision_seconds']
: null,
'playable' => $decision->allowed && is_string($file) && $file !== '' && ($class === 'film' || $class === 'audio'),
'playback' => app(FilmPlayback::class)->mode($version->id),
'renew_url' => $decision->allowed && $action === 'streamen' && app(FilmPlayback::class)->mode($version->id) === 'hls'
@@ -336,7 +336,7 @@ public function media(
]);
}
return $this->fileResponse($decision->grant, $article, $release, $version, $action, $file, 'bestand');
return $this->fileResponse($request, $decision->grant, $article, $release, $version, $action, $file, 'bestand');
}
public function rendition(
@@ -505,6 +505,7 @@ private function playlist(
}
private function fileResponse(
Request $request,
Grant $grant,
LicenseArticle $article,
ArticleRelease $release,
@@ -515,24 +516,27 @@ private function fileResponse(
): StreamedResponse {
$disk = Storage::disk('media_derived');
abort_unless($disk->exists($file) && ! str_contains($file, '..'), 404);
$delivery = app(DeliveryLog::class)->write(
$grant,
$article,
$release,
$version,
$action,
$format,
hash_file('sha256', $disk->path($file)) ?: null,
'k0',
);
$log = app(DeliveryLog::class);
$checksum = fn (): ?string => app(FileChecksum::class)->of('media_derived', $file);
// Dateiname aus dem Anzeigetitel statt aus der Adresse („laden.mp4“); Laravel setzt Content-Disposition mit
// ASCII-Rückfall und UTF-8-Name.
return $disk->response(
// Ein vollständiger Abruf ist eine Auslieferung. Teilabrufe (Springen, Fortsetzen) derselben Wiedergabe zählen
// innerhalb der Laufzeit der Adresse nicht erneut; gibt es noch keinen Nachweis, entsteht genau einer.
if (is_string($request->header('Range'))) {
$log->writeUnlessRecent(600, $grant, $article, $release, $version, $action, $format, 'k0', $checksum);
$copy = '';
} else {
$copy = $log->write($grant, $article, $release, $version, $action, $format, $checksum(), 'k0')->copy_id;
}
// Dateiname aus dem Anzeigetitel statt aus der Adresse („laden.mp4“), mit ASCII-Rückfall und UTF-8-Name.
return $this->respondWithRanges(
$request,
'media_derived',
$file,
true,
$this->downloadName($version, $file),
['X-License-Copy' => $delivery->copy_id],
$action === 'laden' ? 'attachment' : 'inline',
$copy === '' ? [] : ['X-License-Copy' => $copy],
);
}
+3
View File
@@ -4,6 +4,7 @@
use App\Domain\Catalog\ContentElementVersion;
use App\Domain\Catalog\ContentVersionStatus;
use App\Domain\Media\FileChecksum;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Storage;
@@ -82,6 +83,8 @@ public function handle(): void
}
$version->save();
// Prüfsumme für den Auslieferungsnachweis jetzt rechnen, damit die erste Wiedergabe nicht darauf wartet.
app(FileChecksum::class)->of('media_derived', $target);
DeriveThumbnail::dispatch('element', $version->content_element_id);
DeriveFilmRenditions::dispatch($version->id);
}