From 59fe66710acf47057773f6875229a7bcce80d34d Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Mon, 27 Jan 2020 13:56:19 +0100 Subject: [PATCH] Delete possible orphan icon when updating a twofaccount --- .../Controllers/TwoFAccountController.php | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/TwoFAccountController.php b/app/Http/Controllers/TwoFAccountController.php index 81a36093..43c19948 100644 --- a/app/Http/Controllers/TwoFAccountController.php +++ b/app/Http/Controllers/TwoFAccountController.php @@ -92,7 +92,22 @@ public function update(Request $request, $id) 'service' => 'required', ]); - $twofaccount = TwoFAccount::FindOrFail($id); + + // Here we catch a possible missing model exception in order to + // delete orphan submited icon + try { + + $twofaccount = TwoFAccount::FindOrFail($id); + + } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { + + if( $request->icon ) { + Storage::delete('public/icons/' . $request->icon); + } + + throw $e; + } + if( $twofaccount->type === 'hotp' ) { @@ -138,11 +153,7 @@ public function destroy($id) $twofaccount = TwoFAccount::FindOrFail($id); // delete icon - $storedIcon = 'public/icons/' . $twofaccount->icon; - - if( Storage::exists($storedIcon) ) { - Storage::delete($storedIcon); - } + Storage::delete('public/icons/' . $twofaccount->icon); // delete account $twofaccount->delete();