Update MediaCloudUrlRewrite command, add avatar support. Fixes #6069

pull/6109/head
Daniel Supernault 3 months ago
parent 9012fd5fc5
commit 506fe14c11
No known key found for this signature in database
GPG Key ID: 23740873EE6F76A1

@ -2,10 +2,13 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use Illuminate\Console\Command; use App\Avatar;
use App\Media; use App\Media;
use Cache, Storage; use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput; use Illuminate\Contracts\Console\PromptsForMissingInput;
use Storage;
use function Laravel\Prompts\select;
class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
{ {
@ -25,9 +28,10 @@ class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
{ {
return [ return [
'oldDomain' => 'The old S3 domain', 'oldDomain' => 'The old S3 domain',
'newDomain' => 'The new S3 domain' 'newDomain' => 'The new S3 domain',
]; ];
} }
/** /**
* The console command description. * The console command description.
* *
@ -43,12 +47,13 @@ class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
$this->preflightCheck(); $this->preflightCheck();
$this->bootMessage(); $this->bootMessage();
$this->confirmCloudUrl(); $this->confirmCloudUrl();
$this->handleTask();
} }
protected function preflightCheck() protected function preflightCheck()
{ {
if (! (bool) config_cache('pixelfed.cloud_storage')) { if (! (bool) config_cache('pixelfed.cloud_storage')) {
$this->info('Error: Cloud storage is not enabled!'); $this->info('Error: Cloud storage is not enabled! Please enable before proceeding.');
$this->error('Aborting...'); $this->error('Aborting...');
exit; exit;
} }
@ -82,13 +87,37 @@ class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
$this->error('Aborting...'); $this->error('Aborting...');
exit; exit;
} }
$this->updateUrls();
} }
protected function updateUrls() protected function handleTask()
{ {
$this->info('Updating urls...'); $task = select(
label: 'What action would you like to perform?',
options: ['Migrate All', 'Migrate Media', 'Migrate Avatars']
);
switch ($task) {
case 'Migrate All':
$this->updateMediaUrls();
$this->updateAvatarUrls();
break;
case 'Migrate Media':
$this->updateMediaUrls();
break;
case 'Migrate Avatars':
$this->updateAvatarUrls();
break;
default:
$this->error('Invalid selection');
return;
break;
}
}
protected function updateMediaUrls()
{
$this->info('Updating media urls...');
$oldDomain = trim($this->argument('oldDomain')); $oldDomain = trim($this->argument('oldDomain'));
$newDomain = trim($this->argument('newDomain')); $newDomain = trim($this->argument('newDomain'));
$disk = Storage::disk(config('filesystems.cloud')); $disk = Storage::disk(config('filesystems.cloud'));
@ -99,11 +128,13 @@ class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
foreach (Media::whereNotNull('cdn_url')->lazyById(1000, 'id') as $media) { foreach (Media::whereNotNull('cdn_url')->lazyById(1000, 'id') as $media) {
if (strncmp($media->media_path, 'http', 4) === 0) { if (strncmp($media->media_path, 'http', 4) === 0) {
$bar->advance(); $bar->advance();
continue; continue;
} }
$cdnHost = parse_url($media->cdn_url, PHP_URL_HOST); $cdnHost = parse_url($media->cdn_url, PHP_URL_HOST);
if ($oldDomain != $cdnHost || $newDomain == $cdnHost) { if ($oldDomain != $cdnHost || $newDomain == $cdnHost) {
$bar->advance(); $bar->advance();
continue; continue;
} }
@ -137,4 +168,43 @@ class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
$this->line(' '); $this->line(' ');
$this->info('Tip: Run `php artisan cache:clear` to purge cached urls'); $this->info('Tip: Run `php artisan cache:clear` to purge cached urls');
} }
protected function updateAvatarUrls()
{
$this->info('Updating avatar urls...');
$oldDomain = trim($this->argument('oldDomain'));
$newDomain = trim($this->argument('newDomain'));
$disk = Storage::disk(config('filesystems.cloud'));
$count = Avatar::count();
$bar = $this->output->createProgressBar($count);
$counter = 0;
$bar->start();
foreach (Avatar::lazyById(1000, 'id') as $avatar) {
if (! $avatar->cdn_url) {
$bar->advance();
continue;
}
$cdnHost = parse_url($avatar->cdn_url, PHP_URL_HOST);
if (strcasecmp($oldDomain, $cdnHost) !== 0 || strcasecmp($newDomain, $cdnHost) === 0) {
$bar->advance();
continue;
}
$avatar->cdn_url = str_replace($oldDomain, $newDomain, $avatar->cdn_url);
$avatar->save();
$counter++;
$bar->advance();
}
$bar->finish();
$this->line(' ');
$this->info('Finished! Updated '.$counter.' total records!');
$this->line(' ');
$this->info('Tip: Run `php artisan cache:clear` to purge cached urls');
}
} }

Loading…
Cancel
Save