diff --git a/app/Console/Commands/UserAvatarDelete.php b/app/Console/Commands/UserAvatarDelete.php new file mode 100644 index 000000000..55a320ee9 --- /dev/null +++ b/app/Console/Commands/UserAvatarDelete.php @@ -0,0 +1,103 @@ + 'Which username should we delete the avatar for?', + ]; + } + + /** + * Execute the console command. + */ + public function handle() + { + $user = User::whereUsername($this->argument('username'))->first(); + + if (! $user) { + $this->error('Could not find any user with that username'); + exit; + } + + if (! $user->profile_id) { + $this->error('Could not find the profile with that username'); + exit; + } + + $pid = $user->profile_id; + + $avatarModel = Avatar::where('profile_id', $pid)->first(); + + if (! $avatarModel) { + $this->error('No avatar model found'); + Cache::forget('avatar:'.$pid); + exit; + } + + $defaultPaths = ['public/avatars/default.jpg', 'public/avatars/default.png']; + $mediaPath = $avatarModel->media_path; + + if (in_array($mediaPath, $defaultPaths)) { + $this->info('Default avatar already used, aborting...'); + Cache::forget('avatar:'.$pid); + exit; + } + + if (Storage::disk(config('filesystems.cloud'))->exists($mediaPath)) { + if ($this->confirm('Found a S3 avatar at '.$mediaPath.'! Are you sure you want to delete this?')) { + Storage::disk(config('filesystems.cloud'))->delete($mediaPath); + $this->info('Deleting S3 copy'); + } else { + exit; + } + } + + if (Storage::disk('local')->exists($mediaPath)) { + if ($this->confirm('Found a local avatar at '.$mediaPath.'! Are you sure you want to delete this?')) { + Storage::disk('local')->delete($mediaPath); + $this->info('Deleting local copy'); + } else { + exit; + } + } + + $avatarModel->media_path = 'public/avatars/default.jpg'; + $avatarModel->cdn_url = null; + $avatarModel->save(); + Cache::forget('avatar:'.$pid); + AccountService::del($pid); + + $this->info('Successfully deleted user avatar!'); + } +}