From 9eafc31e6f32814f3492b8ef418f43a46b1efe6c Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 25 Jan 2021 21:37:44 -0700 Subject: [PATCH] Update AvatarObserver, add logic to delete avatars stored in S3 --- app/Observers/AvatarObserver.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/Observers/AvatarObserver.php b/app/Observers/AvatarObserver.php index 187a6ff9..3d62845b 100644 --- a/app/Observers/AvatarObserver.php +++ b/app/Observers/AvatarObserver.php @@ -3,6 +3,8 @@ namespace App\Observers; use App\Avatar; +use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; class AvatarObserver { @@ -54,6 +56,14 @@ class AvatarObserver ) { @unlink($path); } + + if($avatar->cdn_url) { + $disk = Storage::disk(config('filesystems.cloud')); + $base = Str::startsWith($avatar->media_path, 'cache/avatars/'); + if($base && $disk->exists($avatar->media_path)) { + $disk->delete($avatar->media_path); + } + } } /**