From 7a72d5aaf20fdba19c8391f90127a1e72ddb2729 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 6 Apr 2019 19:42:19 -0600 Subject: [PATCH] Add avatar migrate command --- .../Commands/AvatarDefaultMigration.php | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 app/Console/Commands/AvatarDefaultMigration.php diff --git a/app/Console/Commands/AvatarDefaultMigration.php b/app/Console/Commands/AvatarDefaultMigration.php new file mode 100644 index 000000000..ba6252b9e --- /dev/null +++ b/app/Console/Commands/AvatarDefaultMigration.php @@ -0,0 +1,88 @@ +info('Running avatar migration...'); + $count = Avatar::whereChangeCount(0)->count(); + + if($count == 0) { + $this->info('Found no avatars needing to be migrated!'); + exit; + } + + $bar = $this->output->createProgressBar($count); + $this->info("Found {$count} avatars that may need to be migrated"); + + Avatar::whereChangeCount(0)->chunk(50, function($avatars) use ($bar) { + foreach($avatars as $avatar) { + if($avatar->media_path == 'public/avatars/default.png' || $avatar->thumb_path == 'public/avatars/default.png') { + continue; + } + + if(Str::endsWith($avatar->media_path, '/avatar.svg') == false) { + // do not modify non-default avatars + continue; + } + + DB::transaction(function() use ($avatar, $bar) { + + if(is_file(storage_path('app/' . $avatar->media_path))) { + @unlink(storage_path('app/' . $avatar->media_path)); + } + + if(is_file(storage_path('app/' . $avatar->thumb_path))) { + @unlink(storage_path('app/' . $avatar->thumb_path)); + } + + $avatar->media_path = 'public/avatars/default.png'; + $avatar->thumb_path = 'public/avatars/default.png'; + $avatar->change_count = $avatar->change_count + 1; + $avatar->save(); + + Cache::forget('avatar:' . $avatar->profile_id); + $bar->advance(); + }); + } + }); + + $bar->finish(); + } +}