legacy: privatize-media überspringt unmigrierte Legacy-Bildzeilen, --prune-missing löscht sie

Der Legacy-Import legt pro Alt-Bild eine Zeile mit rohem Legacy-Dateinamen
als path an; legacy:migrate-media biegt sie auf die echte Datei um. Zeilen,
deren Datei im Alt-System verloren ging (Live-Befund: ~22k bei Presseecho),
blieben als disk=public-Leichen liegen und ließen den Privatizer mit
RuntimeException abbrechen. Jetzt: Skip + Warnhinweis statt Abbruch;
--prune-missing löscht solche Zeilen endgültig, wenn ihre Datei auf keiner
Disk existiert. Der Integritäts-Assert für migrierte Pfade bleibt.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Kevin Adametz 2026-07-20 10:50:31 +00:00
parent 50d9208ca9
commit 4c73ba0a89
3 changed files with 115 additions and 6 deletions

View file

@ -5,26 +5,43 @@ namespace App\Services\PressRelease;
use App\Models\PressReleaseAttachment;
use App\Models\PressReleaseImage;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RuntimeException;
class PressReleaseMediaPrivatizer
{
/**
* @return array{images:int,attachments:int}
* Der Legacy-Import legt Bildzeilen mit dem rohen Alt-Dateinamen als `path`
* an; erst `legacy:migrate-media` biegt sie auf `press-releases/…` um.
* Zeilen, die danach noch den Alt-Pfad tragen, haben keine Quelldatei mehr —
* sie werden hier übersprungen bzw. mit `$pruneMissing` endgültig gelöscht.
*
* @return array{images:int,attachments:int,legacy_skipped:int,legacy_pruned:int}
*/
public function privatizeAll(): array
public function privatizeAll(bool $pruneMissing = false): array
{
$counts = ['images' => 0, 'attachments' => 0];
$counts = ['images' => 0, 'attachments' => 0, 'legacy_skipped' => 0, 'legacy_pruned' => 0];
PressReleaseImage::query()
->withTrashed()
->where('disk', 'public')
->chunkById(100, function ($images) use (&$counts): void {
->chunkById(100, function ($images) use (&$counts, $pruneMissing): void {
foreach ($images as $image) {
$paths = collect([$image->path, ...array_values($image->variants ?? [])])
->filter(fn ($path): bool => is_string($path) && filled($path))
->unique();
if ($this->isUnmigratedLegacyImage($image)) {
if ($pruneMissing && ! $this->anyPathExists($paths->all())) {
$image->forceDelete();
$counts['legacy_pruned']++;
} else {
$counts['legacy_skipped']++;
}
continue;
}
$this->assertPathsAvailable($paths->all());
foreach ($paths as $path) {
@ -51,6 +68,29 @@ class PressReleaseMediaPrivatizer
return $counts;
}
private function isUnmigratedLegacyImage(PressReleaseImage $image): bool
{
return $image->legacy_id !== null
&& ! Str::startsWith((string) $image->path, 'press-releases/');
}
/**
* @param list<string> $paths
*/
private function anyPathExists(array $paths): bool
{
$public = Storage::disk('public');
$private = Storage::disk('local');
foreach ($paths as $path) {
if ($public->exists($path) || $private->exists($path)) {
return true;
}
}
return false;
}
/**
* @param list<string> $paths
*/