Laravel 12.69, Livewire 4.4, Flux 2.20, Cashier 16.8 (Stripe-SDK 21), Larastan 3.12; Vite 6.4.3, Tailwind 4.3, Alpine 3.17 - npm audit 0. Drei neue Larastan-Befunde behoben, drei tote Baseline-Einträge entfernt. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
140 lines
4.5 KiB
PHP
140 lines
4.5 KiB
PHP
<?php
|
|
|
|
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
|
|
{
|
|
/**
|
|
* 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(bool $pruneMissing = false): array
|
|
{
|
|
$counts = ['images' => 0, 'attachments' => 0, 'legacy_skipped' => 0, 'legacy_pruned' => 0];
|
|
|
|
PressReleaseImage::query()
|
|
->withTrashed()
|
|
->where('disk', 'public')
|
|
->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) {
|
|
$this->moveToPrivateDisk($path);
|
|
}
|
|
|
|
$image->updateQuietly(['disk' => 'local']);
|
|
$counts['images']++;
|
|
}
|
|
});
|
|
|
|
PressReleaseAttachment::query()
|
|
->withTrashed()
|
|
->where('disk', 'public')
|
|
->chunkById(100, function ($attachments) use (&$counts): void {
|
|
foreach ($attachments as $attachment) {
|
|
$this->assertPathsAvailable([$attachment->path]);
|
|
$this->moveToPrivateDisk($attachment->path);
|
|
$attachment->updateQuietly(['disk' => 'local']);
|
|
$counts['attachments']++;
|
|
}
|
|
});
|
|
|
|
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
|
|
*/
|
|
private function assertPathsAvailable(array $paths): void
|
|
{
|
|
$public = Storage::disk('public');
|
|
$private = Storage::disk('local');
|
|
|
|
foreach ($paths as $path) {
|
|
if (! $public->exists($path) && ! $private->exists($path)) {
|
|
throw new RuntimeException("Press release media file is missing on both disks: {$path}");
|
|
}
|
|
}
|
|
}
|
|
|
|
private function moveToPrivateDisk(string $path): void
|
|
{
|
|
$public = Storage::disk('public');
|
|
$private = Storage::disk('local');
|
|
|
|
if ($private->exists($path)) {
|
|
$public->delete($path);
|
|
|
|
return;
|
|
}
|
|
|
|
if (! $public->exists($path)) {
|
|
throw new RuntimeException("Public press release media file disappeared during migration: {$path}");
|
|
}
|
|
|
|
$stream = $public->readStream($path);
|
|
|
|
if (! is_resource($stream)) {
|
|
throw new RuntimeException("Could not read public press release media: {$path}");
|
|
}
|
|
|
|
try {
|
|
if (! $private->put($path, $stream, 'private')) {
|
|
throw new RuntimeException("Could not write private press release media: {$path}");
|
|
}
|
|
} finally {
|
|
fclose($stream);
|
|
}
|
|
|
|
$public->delete($path);
|
|
}
|
|
}
|