MediaCloudUrlRewrite.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Media;
  5. use Cache, Storage;
  6. use Illuminate\Contracts\Console\PromptsForMissingInput;
  7. class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'media:cloud-url-rewrite {oldDomain} {newDomain}';
  15. /**
  16. * Prompt for missing input arguments using the returned questions.
  17. *
  18. * @return array
  19. */
  20. protected function promptForMissingArgumentsUsing()
  21. {
  22. return [
  23. 'oldDomain' => 'The old S3 domain',
  24. 'newDomain' => 'The new S3 domain'
  25. ];
  26. }
  27. /**
  28. * The console command description.
  29. *
  30. * @var string
  31. */
  32. protected $description = 'Rewrite S3 media urls from local users';
  33. /**
  34. * Execute the console command.
  35. */
  36. public function handle()
  37. {
  38. $this->preflightCheck();
  39. $this->bootMessage();
  40. $this->confirmCloudUrl();
  41. }
  42. protected function preflightCheck()
  43. {
  44. if(config_cache('pixelfed.cloud_storage') != true) {
  45. $this->info('Error: Cloud storage is not enabled!');
  46. $this->error('Aborting...');
  47. exit;
  48. }
  49. }
  50. protected function bootMessage()
  51. {
  52. $this->info(' ____ _ ______ __ ');
  53. $this->info(' / __ \(_) _____ / / __/__ ____/ / ');
  54. $this->info(' / /_/ / / |/_/ _ \/ / /_/ _ \/ __ / ');
  55. $this->info(' / ____/ /> </ __/ / __/ __/ /_/ / ');
  56. $this->info(' /_/ /_/_/|_|\___/_/_/ \___/\__,_/ ');
  57. $this->info(' ');
  58. $this->info(' Media Cloud Url Rewrite Tool');
  59. $this->info(' ===');
  60. $this->info(' Old S3: ' . trim($this->argument('oldDomain')));
  61. $this->info(' New S3: ' . trim($this->argument('newDomain')));
  62. $this->info(' ');
  63. }
  64. protected function confirmCloudUrl()
  65. {
  66. $disk = Storage::disk(config('filesystems.cloud'))->url('test');
  67. $domain = parse_url($disk, PHP_URL_HOST);
  68. if(trim($this->argument('newDomain')) !== $domain) {
  69. $this->error('Error: The new S3 domain you entered is not currently configured');
  70. exit;
  71. }
  72. if(!$this->confirm('Confirm this is correct')) {
  73. $this->error('Aborting...');
  74. exit;
  75. }
  76. $this->updateUrls();
  77. }
  78. protected function updateUrls()
  79. {
  80. $this->info('Updating urls...');
  81. $oldDomain = trim($this->argument('oldDomain'));
  82. $newDomain = trim($this->argument('newDomain'));
  83. $disk = Storage::disk(config('filesystems.cloud'));
  84. $count = Media::whereNotNull('cdn_url')->count();
  85. $bar = $this->output->createProgressBar($count);
  86. $counter = 0;
  87. $bar->start();
  88. foreach(Media::whereNotNull('cdn_url')->lazyById(1000, 'id') as $media) {
  89. if(strncmp($media->media_path, 'http', 4) === 0) {
  90. $bar->advance();
  91. continue;
  92. }
  93. $cdnHost = parse_url($media->cdn_url, PHP_URL_HOST);
  94. if($oldDomain != $cdnHost || $newDomain == $cdnHost) {
  95. $bar->advance();
  96. continue;
  97. }
  98. $media->cdn_url = str_replace($oldDomain, $newDomain, $media->cdn_url);
  99. if($media->thumbnail_url != null) {
  100. $thumbHost = parse_url($media->thumbnail_url, PHP_URL_HOST);
  101. if($thumbHost == $oldDomain) {
  102. $thumbUrl = $disk->url($media->thumbnail_path);
  103. $media->thumbnail_url = $thumbUrl;
  104. }
  105. }
  106. if($media->optimized_url != null) {
  107. $optiHost = parse_url($media->optimized_url, PHP_URL_HOST);
  108. if($optiHost == $oldDomain) {
  109. $optiUrl = str_replace($oldDomain, $newDomain, $media->optimized_url);
  110. $media->optimized_url = $optiUrl;
  111. }
  112. }
  113. $media->save();
  114. $counter++;
  115. $bar->advance();
  116. }
  117. $bar->finish();
  118. $this->line(' ');
  119. $this->info('Finished! Updated ' . $counter . ' total records!');
  120. $this->line(' ');
  121. $this->info('Tip: Run `php artisan cache:clear` to purge cached urls');
  122. }
  123. }