1
0

CloudMediaMigrate.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Media;
  5. use App\Services\MediaStorageService;
  6. use App\Util\Lexer\PrettyNumber;
  7. use Illuminate\Support\Facades\Storage;
  8. use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. class CloudMediaMigrate extends Command
  11. {
  12. public $totalSize = 0;
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'media:migrate2cloud {--limit=200} {--huge}';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = 'Move older media to cloud storage';
  25. /**
  26. * Execute the console command.
  27. *
  28. * @return int
  29. */
  30. public function handle()
  31. {
  32. $enabled = (bool) config_cache('pixelfed.cloud_storage');
  33. if(!$enabled) {
  34. $this->error('Cloud storage not enabled. Exiting...');
  35. return;
  36. }
  37. if(!$this->confirm('Are you sure you want to proceed?')) {
  38. return;
  39. }
  40. $limit = $this->option('limit');
  41. $hugeMode = $this->option('huge');
  42. if($limit > 500 && !$hugeMode) {
  43. $this->error('Max limit exceeded, use a limit lower than 500 or run again with the --huge flag');
  44. return;
  45. }
  46. $bar = $this->output->createProgressBar($limit);
  47. $bar->start();
  48. Media::whereNot('version', '4')
  49. ->where('created_at', '<', now()->subDays(2))
  50. ->whereRemoteMedia(false)
  51. ->whereNotNull(['status_id', 'profile_id'])
  52. ->whereNull(['cdn_url', 'replicated_at'])
  53. ->orderByDesc('size')
  54. ->take($limit)
  55. ->get()
  56. ->each(function($media) use($bar) {
  57. if(Storage::disk('local')->exists($media->media_path)) {
  58. $this->totalSize = $this->totalSize + $media->size;
  59. try {
  60. MediaStorageService::store($media);
  61. } catch (FileNotFoundException $e) {
  62. $this->error('Error migrating media ' . $media->id . ' to cloud storage: ' . $e->getMessage());
  63. return;
  64. } catch (NotFoundHttpException $e) {
  65. $this->error('Error migrating media ' . $media->id . ' to cloud storage: ' . $e->getMessage());
  66. return;
  67. } catch (\Exception $e) {
  68. $this->error('Error migrating media ' . $media->id . ' to cloud storage: ' . $e->getMessage());
  69. return;
  70. }
  71. }
  72. $bar->advance();
  73. });
  74. $bar->finish();
  75. $this->line(' ');
  76. $this->info('Finished!');
  77. if($this->totalSize) {
  78. $this->info('Uploaded ' . PrettyNumber::size($this->totalSize) . ' of media to cloud storage!');
  79. $this->line(' ');
  80. $this->info('These files are still stored locally, and will be automatically removed.');
  81. }
  82. return Command::SUCCESS;
  83. }
  84. }