Behalte die HLS-Stufen bei einer neuen Filmfassung mit derselben Datei
Eine neue Fassung ohne neuen Upload (Textänderung, Marken) fiel in den Dateimodus, weil ihre media_renditions fehlten. MediaRendition::carryOver übernimmt die Zeilen der letzten Fassung mit derselben Datei, carryForward holt sie nach, wenn die Verarbeitung erst nach dem Speichern fertig wird. Die Auslieferung prüft Pfade über MediaRendition::owns: nur eigene Ableitungen und der Ordner des eigenen Masters. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f11ff89f2d
commit
fd5db90b77
6 files changed
+296
-6
No files matched your search
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Domain\Catalog;
|
namespace App\Domain\Catalog;
|
||||||
|
|
||||||
|
use App\Domain\Media\MediaRendition;
|
||||||
use App\Jobs\StoreContentFile;
|
use App\Jobs\StoreContentFile;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
@@ -73,6 +74,9 @@ public function publishElementVersion(
|
|||||||
if (is_string($incoming) && $incoming !== '') {
|
if (is_string($incoming) && $incoming !== '') {
|
||||||
StoreContentFile::dispatch($version->id, $incoming);
|
StoreContentFile::dispatch($version->id, $incoming);
|
||||||
$version->refresh();
|
$version->refresh();
|
||||||
|
} elseif (is_string($metadata['file'] ?? null) && $metadata['file'] !== '') {
|
||||||
|
// Gleiche Datei, neue Fassung (Text, Marken, Notiz): die HLS-Stufen der Datei bleiben erhalten.
|
||||||
|
MediaRendition::carryOver($element, $version, $metadata['file']);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $version;
|
return $version;
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Domain\Media;
|
namespace App\Domain\Media;
|
||||||
|
|
||||||
|
use App\Domain\Catalog\ContentElement;
|
||||||
|
use App\Domain\Catalog\ContentElementVersion;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
class MediaRendition extends Model
|
class MediaRendition extends Model
|
||||||
@@ -13,6 +15,97 @@ class MediaRendition extends Model
|
|||||||
'path',
|
'path',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Darf diese Fassung die Datei ausliefern? Erlaubt ist das Verzeichnis ihrer eigenen Ableitungen: der Ordner
|
||||||
|
* ableitungen/{Kennung} und der Ordner, in dem ihr Master liegt. Letzterer weicht ab, wenn die Fassung ihre
|
||||||
|
* Stufen von einer früheren Fassung derselben Datei übernommen hat (carryOver). Kein Pfad anderer Fassungen,
|
||||||
|
* kein "..".
|
||||||
|
*/
|
||||||
|
public static function owns(int $versionId, string $path): bool
|
||||||
|
{
|
||||||
|
if ($path === '' || str_contains($path, '..') || str_contains($path, '\\')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$prefixes = ['ableitungen/'.$versionId];
|
||||||
|
$master = static::masterPath($versionId);
|
||||||
|
|
||||||
|
if ($master !== null) {
|
||||||
|
$directory = str_replace('\\', '/', dirname($master));
|
||||||
|
|
||||||
|
if (str_starts_with($directory, 'ableitungen/')) {
|
||||||
|
$prefixes[] = $directory;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($prefixes as $prefix) {
|
||||||
|
if ($path === $prefix || str_starts_with($path, $prefix.'/')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Eine neue Fassung mit derselben Datei (ohne neuen Upload) behält die Stufen der letzten Fassung dieser Datei.
|
||||||
|
* Die Zeilen zeigen auf dieselben, unveränderten Ableitungen; es wird nichts neu erzeugt oder kopiert.
|
||||||
|
*/
|
||||||
|
public static function carryOver(ContentElement $element, ContentElementVersion $version, string $file): void
|
||||||
|
{
|
||||||
|
$source = ContentElementVersion::query()
|
||||||
|
->where('content_element_id', $element->id)
|
||||||
|
->where('id', '<>', $version->id)
|
||||||
|
->orderByDesc('number')
|
||||||
|
->get()
|
||||||
|
->first(fn (ContentElementVersion $candidate): bool => is_array($candidate->metadata)
|
||||||
|
&& ($candidate->metadata['file'] ?? null) === $file
|
||||||
|
&& static::masterPath($candidate->id) !== null);
|
||||||
|
|
||||||
|
if ($source === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (static::query()->where('content_element_version_id', $source->id)->get() as $row) {
|
||||||
|
static::query()->updateOrCreate(
|
||||||
|
['content_element_version_id' => $version->id, 'label' => $row->label],
|
||||||
|
['role' => $row->role, 'path' => $row->path],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gegenstück zu carryOver: Wurde eine Fassung mit derselben Datei schon gespeichert, bevor die Verarbeitung dieser
|
||||||
|
* Fassung fertig war, bekommt sie die Stufen nachträglich. Nur spätere Fassungen desselben Mediums mit derselben
|
||||||
|
* Datei und ohne eigenen Master; eine andere Datei erbt nichts.
|
||||||
|
*/
|
||||||
|
public static function carryForward(ContentElementVersion $source): void
|
||||||
|
{
|
||||||
|
$file = is_array($source->metadata) ? ($source->metadata['file'] ?? null) : null;
|
||||||
|
|
||||||
|
if (! is_string($file) || $file === '' || static::masterPath($source->id) === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows = static::query()->where('content_element_version_id', $source->id)->get();
|
||||||
|
|
||||||
|
ContentElementVersion::query()
|
||||||
|
->where('content_element_id', $source->content_element_id)
|
||||||
|
->where('number', '>', $source->number)
|
||||||
|
->get()
|
||||||
|
->filter(fn (ContentElementVersion $later): bool => is_array($later->metadata)
|
||||||
|
&& ($later->metadata['file'] ?? null) === $file
|
||||||
|
&& static::masterPath($later->id) === null)
|
||||||
|
->each(function (ContentElementVersion $later) use ($rows): void {
|
||||||
|
foreach ($rows as $row) {
|
||||||
|
static::query()->updateOrCreate(
|
||||||
|
['content_element_version_id' => $later->id, 'label' => $row->label],
|
||||||
|
['role' => $row->role, 'path' => $row->path],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
public static function masterPath(int $versionId): ?string
|
public static function masterPath(int $versionId): ?string
|
||||||
{
|
{
|
||||||
$path = static::query()
|
$path = static::query()
|
||||||
|
|||||||
@@ -160,9 +160,7 @@ private function storedPath(ContentElementVersion $version): ?string
|
|||||||
|
|
||||||
private function renditionBelongsTo(int $versionId, string $path): bool
|
private function renditionBelongsTo(int $versionId, string $path): bool
|
||||||
{
|
{
|
||||||
$prefix = 'ableitungen/'.$versionId;
|
return MediaRendition::owns($versionId, $path);
|
||||||
|
|
||||||
return ($path === $prefix || str_starts_with($path, $prefix.'/')) && ! str_contains($path, '..');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function authorizeStaff(): void
|
private function authorizeStaff(): void
|
||||||
|
|||||||
@@ -495,9 +495,7 @@ private function downloadName(ContentElementVersion $version, string $file): str
|
|||||||
|
|
||||||
private function renditionBelongsTo(int $versionId, string $path): bool
|
private function renditionBelongsTo(int $versionId, string $path): bool
|
||||||
{
|
{
|
||||||
$prefix = 'ableitungen/'.$versionId;
|
return MediaRendition::owns($versionId, $path);
|
||||||
|
|
||||||
return ($path === $prefix || str_starts_with($path, $prefix.'/')) && ! str_contains($path, '..');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function maySeeThumbnail(Request $request, string $kind, int $subject): bool
|
private function maySeeThumbnail(Request $request, string $kind, int $subject): bool
|
||||||
|
|||||||
@@ -48,5 +48,8 @@ public function handle(FilmTranscoder $transcoder): void
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fassungen mit derselben Datei, die schon während der Verarbeitung gespeichert wurden, bekommen die Stufen nach.
|
||||||
|
MediaRendition::carryForward($version);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,194 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Domain\Catalog\ArticleService;
|
||||||
|
use App\Domain\Catalog\ContentElement;
|
||||||
|
use App\Domain\Catalog\ContentElementVersion;
|
||||||
|
use App\Domain\Catalog\ContentService;
|
||||||
|
use App\Domain\Catalog\OfferService;
|
||||||
|
use App\Domain\Entitlement\GrantIssuer;
|
||||||
|
use App\Domain\Media\FilmPlayback;
|
||||||
|
use App\Domain\Media\FilmTranscoder;
|
||||||
|
use App\Domain\Media\MediaRendition;
|
||||||
|
use App\Http\SignedLink;
|
||||||
|
use App\Jobs\DeriveFilmRenditions;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Eine neue Filmfassung ohne neue Datei (Textänderung, Marken, Änderungsnotiz) behält die HLS-Stufen der Datei.
|
||||||
|
* Vorher fiel sie in den Dateimodus, weil die Verarbeitung nur beim Speichern einer neuen Datei startet und die
|
||||||
|
* Auslieferung nur Pfade unter ableitungen/{eigene Kennung} zuließ.
|
||||||
|
*/
|
||||||
|
class FilmRenditionCarryOverTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
public function test_a_new_film_version_without_a_new_file_keeps_streaming_over_hls(): void
|
||||||
|
{
|
||||||
|
Storage::fake('media_derived');
|
||||||
|
[$first, $film] = $this->filmWithRenditions('Hofszene', 'inhalte/hof.mp4');
|
||||||
|
|
||||||
|
$second = app(ContentService::class)->publishElementVersion(
|
||||||
|
$film,
|
||||||
|
'Neuer Begleittext.',
|
||||||
|
['file' => 'inhalte/hof.mp4', 'decision_seconds' => 12],
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->assertSame('hls', app(FilmPlayback::class)->mode($second->id));
|
||||||
|
$this->assertSame(
|
||||||
|
MediaRendition::masterPath($first->id),
|
||||||
|
MediaRendition::masterPath($second->id),
|
||||||
|
);
|
||||||
|
$this->assertSame(
|
||||||
|
MediaRendition::query()->where('content_element_version_id', $first->id)->count(),
|
||||||
|
MediaRendition::query()->where('content_element_version_id', $second->id)->count(),
|
||||||
|
);
|
||||||
|
$this->assertSame('hls', app(FilmPlayback::class)->mode($first->id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_a_new_file_does_not_inherit_renditions_of_the_old_file(): void
|
||||||
|
{
|
||||||
|
Storage::fake('media_derived');
|
||||||
|
[, $film] = $this->filmWithRenditions('Hofszene', 'inhalte/hof.mp4');
|
||||||
|
|
||||||
|
$replaced = app(ContentService::class)->publishElementVersion($film, null, ['file' => 'inhalte/anderer.mp4']);
|
||||||
|
|
||||||
|
$this->assertSame('file', app(FilmPlayback::class)->mode($replaced->id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_a_member_streams_the_carried_over_renditions_but_never_another_films(): void
|
||||||
|
{
|
||||||
|
Storage::fake('media_derived');
|
||||||
|
[$first, $film] = $this->filmWithRenditions('Hofszene', 'inhalte/hof.mp4');
|
||||||
|
[$foreign] = $this->filmWithRenditions('Fremdfilm', 'inhalte/fremd.mp4');
|
||||||
|
$second = app(ContentService::class)->publishElementVersion(
|
||||||
|
$film,
|
||||||
|
'Neuer Begleittext.',
|
||||||
|
['file' => 'inhalte/hof.mp4'],
|
||||||
|
);
|
||||||
|
[$article, $offer, $release] = $this->offerFor($second, ['film' => ['streamen']]);
|
||||||
|
$member = User::factory()->create();
|
||||||
|
app(GrantIssuer::class)->claim($member, $offer);
|
||||||
|
|
||||||
|
$ask = fn (string $path) => $this->actingAs($member)->get(SignedLink::temporary('member.rendition', now()->addMinutes(5), [
|
||||||
|
'article' => $article->id,
|
||||||
|
'release' => $release->id,
|
||||||
|
'version' => $second->id,
|
||||||
|
'datei' => $path,
|
||||||
|
'reader' => $member->id,
|
||||||
|
]));
|
||||||
|
|
||||||
|
$ask('ableitungen/'.$first->id.'/master.m3u8')->assertOk()->assertHeader('content-type', 'application/vnd.apple.mpegurl');
|
||||||
|
$ask('ableitungen/'.$first->id.'/480/segment.ts')->assertOk()->assertStreamedContent('segment-bytes');
|
||||||
|
$ask('ableitungen/'.$second->id.'/480/segment.ts')->assertNotFound();
|
||||||
|
$ask('ableitungen/'.$foreign->id.'/480/segment.ts')->assertNotFound();
|
||||||
|
$ask('ableitungen/'.$first->id.'/../'.$foreign->id.'/480/segment.ts')->assertNotFound();
|
||||||
|
$ask('ableitungen/'.$first->id.'x/480/segment.ts')->assertNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_a_version_without_renditions_cannot_reach_the_files_of_another_version(): void
|
||||||
|
{
|
||||||
|
Storage::fake('media_derived');
|
||||||
|
[$first, $film] = $this->filmWithRenditions('Hofszene', 'inhalte/hof.mp4');
|
||||||
|
$replaced = app(ContentService::class)->publishElementVersion($film, null, ['file' => 'inhalte/anderer.mp4']);
|
||||||
|
[$article, $offer, $release] = $this->offerFor($replaced, ['film' => ['streamen']]);
|
||||||
|
$member = User::factory()->create();
|
||||||
|
app(GrantIssuer::class)->claim($member, $offer);
|
||||||
|
|
||||||
|
$this->actingAs($member)->get(SignedLink::temporary('member.rendition', now()->addMinutes(5), [
|
||||||
|
'article' => $article->id,
|
||||||
|
'release' => $release->id,
|
||||||
|
'version' => $replaced->id,
|
||||||
|
'datei' => 'ableitungen/'.$first->id.'/480/segment.ts',
|
||||||
|
'reader' => $member->id,
|
||||||
|
]))->assertNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_a_version_saved_while_the_file_is_still_being_processed_gets_the_renditions_afterwards(): void
|
||||||
|
{
|
||||||
|
Storage::fake('media_derived');
|
||||||
|
Storage::disk('media_derived')->put('inhalte/hof.mp4', 'filmbytes');
|
||||||
|
$content = app(ContentService::class);
|
||||||
|
$film = $content->createElement('film', 'film', 'Hofszene');
|
||||||
|
$first = $content->publishElementVersion($film, null, ['file' => 'inhalte/hof.mp4']);
|
||||||
|
// Die Verarbeitung der ersten Fassung läuft noch; die Redaktion speichert schon einen Text oder eine Marke.
|
||||||
|
$second = $content->publishElementVersion($film, 'Text.', ['file' => 'inhalte/hof.mp4']);
|
||||||
|
$replaced = $content->publishElementVersion($film, null, ['file' => 'inhalte/anderer.mp4']);
|
||||||
|
$this->assertSame('file', app(FilmPlayback::class)->mode($second->id));
|
||||||
|
|
||||||
|
$this->app->instance(FilmTranscoder::class, new class implements FilmTranscoder
|
||||||
|
{
|
||||||
|
public function available(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function derive(int $versionId, string $sourceRelative): array
|
||||||
|
{
|
||||||
|
$base = 'ableitungen/'.$versionId;
|
||||||
|
Storage::disk('media_derived')->put($base.'/master.m3u8', "#EXTM3U\n");
|
||||||
|
|
||||||
|
return [['role' => 'master', 'label' => 'master', 'path' => $base.'/master.m3u8']];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
app(DeriveFilmRenditions::class, ['versionId' => $first->id])->handle(app(FilmTranscoder::class));
|
||||||
|
|
||||||
|
$this->assertSame('hls', app(FilmPlayback::class)->mode($first->id));
|
||||||
|
$this->assertSame('hls', app(FilmPlayback::class)->mode($second->id), 'Die spätere Fassung mit derselben Datei bekommt die Stufen nachträglich.');
|
||||||
|
$this->assertSame(MediaRendition::masterPath($first->id), MediaRendition::masterPath($second->id));
|
||||||
|
$this->assertSame('file', app(FilmPlayback::class)->mode($replaced->id), 'Eine andere Datei erbt nichts.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_the_editorial_preview_streams_the_carried_over_renditions(): void
|
||||||
|
{
|
||||||
|
Storage::fake('media_derived');
|
||||||
|
[$first, $film] = $this->filmWithRenditions('Hofszene', 'inhalte/hof.mp4');
|
||||||
|
$second = app(ContentService::class)->publishElementVersion($film, 'Text.', ['file' => 'inhalte/hof.mp4']);
|
||||||
|
$editor = User::factory()->redakteur()->create();
|
||||||
|
|
||||||
|
$this->actingAs($editor, 'staff')
|
||||||
|
->get(route('content.rendition', ['version' => $second, 'datei' => 'ableitungen/'.$first->id.'/480/segment.ts']))
|
||||||
|
->assertOk();
|
||||||
|
$this->actingAs($editor, 'staff')
|
||||||
|
->get(route('content.rendition', ['version' => $second, 'datei' => 'ableitungen/999999/480/segment.ts']))
|
||||||
|
->assertNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{0: ContentElementVersion, 1: ContentElement}
|
||||||
|
*/
|
||||||
|
private function filmWithRenditions(string $title, string $file): array
|
||||||
|
{
|
||||||
|
Storage::disk('media_derived')->put($file, 'filmbytes');
|
||||||
|
$content = app(ContentService::class);
|
||||||
|
$element = $content->createElement('film', 'film', $title);
|
||||||
|
$version = $content->publishElementVersion($element, null, ['file' => $file]);
|
||||||
|
$base = 'ableitungen/'.$version->id;
|
||||||
|
Storage::disk('media_derived')->put($base.'/480/index.m3u8', "#EXTM3U\n#EXTINF:6.0,\nsegment.ts\n");
|
||||||
|
Storage::disk('media_derived')->put($base.'/480/segment.ts', 'segment-bytes');
|
||||||
|
Storage::disk('media_derived')->put($base.'/master.m3u8', "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=800000\n480/index.m3u8\n");
|
||||||
|
MediaRendition::query()->create(['content_element_version_id' => $version->id, 'role' => 'variant', 'label' => '480', 'path' => $base.'/480/index.m3u8']);
|
||||||
|
MediaRendition::query()->create(['content_element_version_id' => $version->id, 'role' => 'master', 'label' => 'master', 'path' => $base.'/master.m3u8']);
|
||||||
|
|
||||||
|
return [$version, $element];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function offerFor($version, array $capabilities): array
|
||||||
|
{
|
||||||
|
$articles = app(ArticleService::class);
|
||||||
|
$offers = app(OfferService::class);
|
||||||
|
$article = $articles->create('ausgabe-'.str()->random(6));
|
||||||
|
$release = $articles->publishRelease($article, $version, 'Stand.');
|
||||||
|
$rights = $offers->createRightsProfile('Rechte '.str()->random(4), 'ehrenamtlich', $capabilities);
|
||||||
|
$offer = $offers->create('Angebot '.str()->random(4));
|
||||||
|
$offers->activate($offer, [
|
||||||
|
['target' => $article->fresh(), 'rights' => $rights],
|
||||||
|
], ['grant_kind' => 'auf_abruf']);
|
||||||
|
|
||||||
|
return [$article->fresh(), $offer, $release];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user