From 3ab197075a883b3c8bb014991cd883af9dd96d20 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 22 May 2024 17:20:03 +0100 Subject: [PATCH 1/4] Nicer handling of erroring when filename+log do not match Signed-off-by: snipe --- .../Controllers/Users/UserFilesController.php | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/Users/UserFilesController.php b/app/Http/Controllers/Users/UserFilesController.php index 87213f2498..ea729d6d5e 100644 --- a/app/Http/Controllers/Users/UserFilesController.php +++ b/app/Http/Controllers/Users/UserFilesController.php @@ -78,24 +78,29 @@ class UserFilesController extends Controller */ public function destroy($userId = null, $fileId = null) { - $user = User::find($userId); - $destinationPath = config('app.private_uploads').'/users'; + if ($user = User::find($userId)) { - if (isset($user->id)) { - $this->authorize('update', $user); - $log = Actionlog::find($fileId); - $full_filename = $destinationPath.'/'.$log->filename; - if (file_exists($full_filename)) { - unlink($destinationPath.'/'.$log->filename); + $this->authorize('delete', $user); + $rel_path = 'private_uploads/users'; + + if ($log = Actionlog::find($fileId)) { + + $full_filename = $rel_path.'/'.$log->filename; + + if (file_exists($full_filename)) { + Storage::delete($rel_path.'/'.$log->filename); + return redirect()->back()->with('success', trans('admin/users/message.deletefile.success')); + } + + $log->delete(); + return redirect()->back()->with('success', trans('admin/users/message.deletefile.success')); } - $log->delete(); - return redirect()->back()->with('success', trans('admin/users/message.deletefile.success')); + return redirect()->back()->with('success', trans('admin/users/message.user_not_found', ['id' => $userId])); + } - // Prepare the error message - $error = trans('admin/users/message.user_not_found', ['id' => $userId]); - // Redirect to the licence management page - return redirect()->route('users.index')->with('error', $error); + + return redirect()->route('users.index')->with('error', trans('admin/users/message.user_not_found', ['id' => $userId])); } From 37d7e89e93e2048479eb15b03032960fafee7932 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 22 May 2024 17:29:43 +0100 Subject: [PATCH 2/4] Fixed error message Signed-off-by: snipe --- app/Http/Controllers/Users/UserFilesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Users/UserFilesController.php b/app/Http/Controllers/Users/UserFilesController.php index ea729d6d5e..db6873a8a3 100644 --- a/app/Http/Controllers/Users/UserFilesController.php +++ b/app/Http/Controllers/Users/UserFilesController.php @@ -96,7 +96,7 @@ class UserFilesController extends Controller return redirect()->back()->with('success', trans('admin/users/message.deletefile.success')); } - return redirect()->back()->with('success', trans('admin/users/message.user_not_found', ['id' => $userId])); + return redirect()->back()->with('error', trans('admin/users/general.log_does_not_exist')); } From 5fd0f562583e43f5741873dabbe3bf0bd97369d3 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 22 May 2024 17:38:49 +0100 Subject: [PATCH 3/4] Use proper storage facade for checking if the file exists Signed-off-by: snipe --- app/Http/Controllers/Users/UserFilesController.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Users/UserFilesController.php b/app/Http/Controllers/Users/UserFilesController.php index db6873a8a3..adc387fe32 100644 --- a/app/Http/Controllers/Users/UserFilesController.php +++ b/app/Http/Controllers/Users/UserFilesController.php @@ -87,13 +87,11 @@ class UserFilesController extends Controller $full_filename = $rel_path.'/'.$log->filename; - if (file_exists($full_filename)) { - Storage::delete($rel_path.'/'.$log->filename); + if (Storage::exists($rel_path.'/'.$full_filename)) { + Storage::delete($rel_path.'/'.$full_filename); + $log->delete(); return redirect()->back()->with('success', trans('admin/users/message.deletefile.success')); } - - $log->delete(); - return redirect()->back()->with('success', trans('admin/users/message.deletefile.success')); } return redirect()->back()->with('error', trans('admin/users/general.log_does_not_exist')); From 4bb7c1701f25ba9025d961e91be6cd6acb621759 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 22 May 2024 17:44:49 +0100 Subject: [PATCH 4/4] Fiddled with storage facade a little more Signed-off-by: snipe --- .../Controllers/Users/UserFilesController.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/Users/UserFilesController.php b/app/Http/Controllers/Users/UserFilesController.php index adc387fe32..ded44f35f6 100644 --- a/app/Http/Controllers/Users/UserFilesController.php +++ b/app/Http/Controllers/Users/UserFilesController.php @@ -83,19 +83,20 @@ class UserFilesController extends Controller $this->authorize('delete', $user); $rel_path = 'private_uploads/users'; + if ($log = Actionlog::find($fileId)) { - - $full_filename = $rel_path.'/'.$log->filename; - - if (Storage::exists($rel_path.'/'.$full_filename)) { - Storage::delete($rel_path.'/'.$full_filename); - $log->delete(); + $filename = $log->filename; + $log->delete(); + + if (Storage::exists($rel_path.'/'.$filename)) { + Storage::delete($rel_path.'/'.$filename); return redirect()->back()->with('success', trans('admin/users/message.deletefile.success')); } + } - return redirect()->back()->with('error', trans('admin/users/general.log_does_not_exist')); - + // The log record doesn't exist somehow + return redirect()->back()->with('success', trans('admin/users/message.deletefile.success')); } return redirect()->route('users.index')->with('error', trans('admin/users/message.user_not_found', ['id' => $userId]));