CloudMediaMigrate.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 = config('pixelfed.cloud_storage');
  33. if(!$enabled) {
  34. $this->error('Cloud storage not enabled. Exiting...');
  35. return;
  36. }
  37. $limit = $this->option('limit');
  38. $hugeMode = $this->option('huge');
  39. if($limit > 500 && !$hugeMode) {
  40. $this->error('Max limit exceeded, use a limit lower than 500 or run again with the --huge flag');
  41. return;
  42. }
  43. $bar = $this->output->createProgressBar($limit);
  44. $bar->start();
  45. Media::whereNot('version', '4')
  46. ->where('created_at', '<', now()->subDays(2))
  47. ->whereRemoteMedia(false)
  48. ->whereNotNull(['status_id', 'profile_id'])
  49. ->whereNull(['cdn_url', 'replicated_at'])
  50. ->orderByDesc('size')
  51. ->take($limit)
  52. ->get()
  53. ->each(function($media) use($bar) {
  54. if(Storage::disk('local')->exists($media->media_path)) {
  55. $this->totalSize = $this->totalSize + $media->size;
  56. try {
  57. MediaStorageService::store($media);
  58. } catch (FileNotFoundException $e) {
  59. $this->error('Error migrating media ' . $media->id . ' to cloud storage: ' . $e->getMessage());
  60. return;
  61. } catch (NotFoundHttpException $e) {
  62. $this->error('Error migrating media ' . $media->id . ' to cloud storage: ' . $e->getMessage());
  63. return;
  64. } catch (\Exception $e) {
  65. $this->error('Error migrating media ' . $media->id . ' to cloud storage: ' . $e->getMessage());
  66. return;
  67. }
  68. }
  69. $bar->advance();
  70. });
  71. $bar->finish();
  72. $this->line(' ');
  73. $this->info('Finished!');
  74. if($this->totalSize) {
  75. $this->info('Uploaded ' . PrettyNumber::size($this->totalSize) . ' of media to cloud storage!');
  76. $this->line(' ');
  77. $this->info('These files are still stored locally, and will be automatically removed.');
  78. }
  79. return Command::SUCCESS;
  80. }
  81. }