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,53 +2,58 @@
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
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'media:cloud-url-rewrite {oldDomain} {newDomain}'; protected $signature = 'media:cloud-url-rewrite {oldDomain} {newDomain}';
/** /**
* Prompt for missing input arguments using the returned questions. * Prompt for missing input arguments using the returned questions.
* *
* @return array * @return array
*/ */
protected function promptForMissingArgumentsUsing() protected function promptForMissingArgumentsUsing()
{ {
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.
* *
* @var string * @var string
*/ */
protected $description = 'Rewrite S3 media urls from local users'; protected $description = 'Rewrite S3 media urls from local users';
/** /**
* Execute the console command. * Execute the console command.
*/ */
public function handle() public function handle()
{ {
$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;
} }
@ -64,8 +69,8 @@ class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
$this->info(' '); $this->info(' ');
$this->info(' Media Cloud Url Rewrite Tool'); $this->info(' Media Cloud Url Rewrite Tool');
$this->info(' ==='); $this->info(' ===');
$this->info(' Old S3: ' . trim($this->argument('oldDomain'))); $this->info(' Old S3: '.trim($this->argument('oldDomain')));
$this->info(' New S3: ' . trim($this->argument('newDomain'))); $this->info(' New S3: '.trim($this->argument('newDomain')));
$this->info(' '); $this->info(' ');
} }
@ -73,22 +78,46 @@ class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
{ {
$disk = Storage::disk(config('filesystems.cloud'))->url('test'); $disk = Storage::disk(config('filesystems.cloud'))->url('test');
$domain = parse_url($disk, PHP_URL_HOST); $domain = parse_url($disk, PHP_URL_HOST);
if(trim($this->argument('newDomain')) !== $domain) { if (trim($this->argument('newDomain')) !== $domain) {
$this->error('Error: The new S3 domain you entered is not currently configured'); $this->error('Error: The new S3 domain you entered is not currently configured');
exit; exit;
} }
if(!$this->confirm('Confirm this is correct')) { if (! $this->confirm('Confirm this is correct')) {
$this->error('Aborting...'); $this->error('Aborting...');
exit; exit;
} }
}
$this->updateUrls(); protected function handleTask()
{
$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 updateUrls() protected function updateMediaUrls()
{ {
$this->info('Updating urls...'); $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'));
@ -96,30 +125,32 @@ class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
$bar = $this->output->createProgressBar($count); $bar = $this->output->createProgressBar($count);
$counter = 0; $counter = 0;
$bar->start(); $bar->start();
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;
} }
$media->cdn_url = str_replace($oldDomain, $newDomain, $media->cdn_url); $media->cdn_url = str_replace($oldDomain, $newDomain, $media->cdn_url);
if($media->thumbnail_url != null) { if ($media->thumbnail_url != null) {
$thumbHost = parse_url($media->thumbnail_url, PHP_URL_HOST); $thumbHost = parse_url($media->thumbnail_url, PHP_URL_HOST);
if($thumbHost == $oldDomain) { if ($thumbHost == $oldDomain) {
$thumbUrl = $disk->url($media->thumbnail_path); $thumbUrl = $disk->url($media->thumbnail_path);
$media->thumbnail_url = $thumbUrl; $media->thumbnail_url = $thumbUrl;
} }
} }
if($media->optimized_url != null) { if ($media->optimized_url != null) {
$optiHost = parse_url($media->optimized_url, PHP_URL_HOST); $optiHost = parse_url($media->optimized_url, PHP_URL_HOST);
if($optiHost == $oldDomain) { if ($optiHost == $oldDomain) {
$optiUrl = str_replace($oldDomain, $newDomain, $media->optimized_url); $optiUrl = str_replace($oldDomain, $newDomain, $media->optimized_url);
$media->optimized_url = $optiUrl; $media->optimized_url = $optiUrl;
} }
@ -133,7 +164,46 @@ class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
$bar->finish(); $bar->finish();
$this->line(' '); $this->line(' ');
$this->info('Finished! Updated ' . $counter . ' total records!'); $this->info('Finished! Updated '.$counter.' total records!');
$this->line(' ');
$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->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');
} }

Loading…
Cancel
Save