Liefere Filme im Dateimodus und Downloads mit Bereichsabrufen aus und rechne die Prüfsumme einmal
LibraryController::fileResponse nutzt ServesByteRanges (206/416): Safari und iOS spielen ab, Springen und Fortsetzen eines Downloads gehen. Dateiname und attachment bleiben. Der Nachweis zählt einen vollständigen Abruf als eine Auslieferung; Teilabrufe schreiben nur, wenn noch kein Eintrag der letzten zehn Minuten steht. FileChecksum merkt die SHA-256 je Datei (Pfad, Größe, Zeit) und wird beim Speichern vorgewärmt, damit die erste Wiedergabe nicht auf das Hashen einer großen Datei wartet. Der entfernte decision-Schlüssel der Artikelseite war ungenutzt. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9e8d0020b3
commit
f07fbae940
6 files changed
+281
-24
No files matched your search
@@ -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],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user