|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * laravel |
| 4 | + * |
| 5 | + * @author Jérémy GAULIN <[email protected]> |
| 6 | + * @copyright 2017 - B&B Web Expertise |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Bnb\Laravel\Attachments\Console\Commands; |
| 10 | + |
| 11 | +use Bnb\Laravel\Attachments\Attachment; |
| 12 | +use Carbon\Carbon; |
| 13 | +use Exception; |
| 14 | +use Illuminate\Console\Command; |
| 15 | +use Illuminate\Support\Collection; |
| 16 | +use Illuminate\Support\Facades\Storage; |
| 17 | +use Lang; |
| 18 | +use Log; |
| 19 | +use Symfony\Component\Console\Input\InputArgument; |
| 20 | +use Symfony\Component\Console\Input\InputOption; |
| 21 | +use Throwable; |
| 22 | + |
| 23 | +class MigrateAttachments extends Command |
| 24 | +{ |
| 25 | + |
| 26 | + protected $signature = 'attachments:migrate'; |
| 27 | + |
| 28 | + |
| 29 | + public function __construct() |
| 30 | + { |
| 31 | + parent::__construct(); |
| 32 | + |
| 33 | + $this->setDescription(Lang::get('attachments::messages.console.migrate_description')); |
| 34 | + |
| 35 | + $this->getDefinition()->addArgument(new InputArgument('from', InputArgument::REQUIRED, |
| 36 | + Lang::get('attachments::messages.console.migrate_option_from'))) |
| 37 | + ; |
| 38 | + |
| 39 | + $this->getDefinition()->addArgument(new InputArgument('to', InputArgument::REQUIRED, |
| 40 | + Lang::get('attachments::messages.console.migrate_option_to'))) |
| 41 | + ; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public function handle() |
| 46 | + { |
| 47 | + if ($this->argument('from') === $this->argument('to')) { |
| 48 | + $this->error(Lang::get('attachments::messages.console.migrate_error_missing')); |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (empty(config(sprintf('filesystems.disks.%s', $this->argument('from'))))) { |
| 54 | + $this->error(Lang::get('attachments::messages.console.migrate_error_from')); |
| 55 | + |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (empty(config(sprintf('filesystems.disks.%s', $this->argument('to'))))) { |
| 60 | + $this->error(Lang::get('attachments::messages.console.migrate_error_to')); |
| 61 | + |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + try { |
| 66 | + Storage::disk($this->argument('from')) |
| 67 | + ->has('.') |
| 68 | + ; |
| 69 | + } catch (Exception $e) { |
| 70 | + $this->error(Lang::get('attachments::messages.console.migrate_invalid_from')); |
| 71 | + } |
| 72 | + try { |
| 73 | + |
| 74 | + Storage::disk($this->argument('to')) |
| 75 | + ->has('.') |
| 76 | + ; |
| 77 | + } catch (Exception $e) { |
| 78 | + $this->error(Lang::get('attachments::messages.console.migrate_invalid_to')); |
| 79 | + } |
| 80 | + |
| 81 | + $query = Attachment::query() |
| 82 | + ->where('disk', '=', $this->argument('from')); |
| 83 | + |
| 84 | + $this |
| 85 | + ->getOutput() |
| 86 | + ->progressStart($query->count()) |
| 87 | + ; |
| 88 | + |
| 89 | + do { |
| 90 | + $deferred = []; |
| 91 | + $continue = true; |
| 92 | + |
| 93 | + try { |
| 94 | + $items = $query |
| 95 | + ->take(10) |
| 96 | + ->get() |
| 97 | + ->each(function (Attachment $attachment) use (&$deferred) { |
| 98 | + if ($this->move($attachment, $deferred)) { |
| 99 | + $attachment->disk = $this->argument('to'); |
| 100 | + |
| 101 | + $attachment->save(); |
| 102 | + } |
| 103 | + |
| 104 | + $this |
| 105 | + ->getOutput() |
| 106 | + ->progressAdvance() |
| 107 | + ; |
| 108 | + }); |
| 109 | + } catch (Exception $e) { |
| 110 | + $continue = false; |
| 111 | + |
| 112 | + $this->error($e->getMessage()); |
| 113 | + Log::error($e); |
| 114 | + } |
| 115 | + |
| 116 | + foreach ($deferred as $callable) { |
| 117 | + try { |
| 118 | + $callable(); |
| 119 | + } catch (Exception | Throwable $e) { |
| 120 | + $this->warn(sprintf('Failed to clean source file : %s', $e->getMessage())); |
| 121 | + Log::error($e); |
| 122 | + } |
| 123 | + } |
| 124 | + } while ($continue && $items->isNotEmpty()); |
| 125 | + |
| 126 | + $this |
| 127 | + ->getOutput() |
| 128 | + ->progressFinish() |
| 129 | + ; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + private function move(Attachment $attachment, &$deferred) |
| 134 | + { |
| 135 | + $from = $attachment->disk; |
| 136 | + $to = $this->argument('to'); |
| 137 | + $filepath = $attachment->filepath; |
| 138 | + |
| 139 | + if ( ! Storage::disk($from)->exists($filepath)) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + Storage::disk($to) |
| 144 | + ->put($filepath, Storage::disk($from)->get($filepath)) |
| 145 | + ; |
| 146 | + |
| 147 | + $deferred[] = function () use ($from, $filepath) { |
| 148 | + Storage::disk($from) |
| 149 | + ->delete($filepath) |
| 150 | + ; |
| 151 | + }; |
| 152 | + |
| 153 | + return true; |
| 154 | + } |
| 155 | +} |
0 commit comments