From 0229aecc69f51dde83a98076b65a85ac75f2eeb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=86=E3=82=AF=E3=83=8B=E3=82=AB=E3=83=AB=E8=AB=8F?= =?UTF-8?q?=E8=A8=AA=E5=AD=90?= Date: Fri, 26 Nov 2021 15:19:42 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=97=E3=83=AD=E3=83=95=E3=82=A3=E3=83=BC?= =?UTF-8?q?=E3=83=AB=E7=94=BB=E5=83=8F=E3=82=92=E3=82=A2=E3=83=83=E3=83=97?= =?UTF-8?q?=E3=83=AD=E3=83=BC=E3=83=89=E3=81=97=E3=81=9F=E3=82=89=E3=80=81?= =?UTF-8?q?=E5=8F=A4=E3=81=84=E7=94=BB=E5=83=8F=E3=82=92=E5=89=8A=E9=99=A4?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=BE?= =?UTF-8?q?=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/ChildrenController.php | 22 ++++++++++++++----- .../Controllers/Api/FathersController.php | 10 ++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/backend/app/Http/Controllers/Api/ChildrenController.php b/backend/app/Http/Controllers/Api/ChildrenController.php index 97447d2b..30ef6c4f 100644 --- a/backend/app/Http/Controllers/Api/ChildrenController.php +++ b/backend/app/Http/Controllers/Api/ChildrenController.php @@ -376,16 +376,24 @@ class ChildrenController extends Controller { $ext = explode('/', mime_content_type($r->image))[1]; $filename = $this->uuidv4() . '.'.$ext; + $oldimg = null; try { + DB::beginTransaction(); + $image = base64_decode(substr($r->image, strpos($r->image, ',') + 1)); Storage::disk('private')->put($filename, $image); - $update = [ - 'image' => '/files/'.$filename, - ]; - - Child::where('id', (int)$child_id)->update($update); + $child = Child::find((int)$child_id); + if (!is_null($child->image) && $child->image != '/assets/default/avatar.jpg') { + $oldimg = str_replace('/files/', '', $child->image); + if (!Storage::disk('private')->exists($oldimg)) { + Log::warning($oldimg.'というパスは不正です。'); + $oldimg = null; + } + } + $child->image = '/files/'.$filename; + $child->save(); $get = Child::where('id', (int)$child_id)->first(); $login_user_datum = $get->toArray(); @@ -399,6 +407,10 @@ class ChildrenController extends Controller { return ['status_code' => 400, 'error_messages' => ['画像の更新に失敗しました。']]; } + if (!is_null($oldimg)) { + Storage::disk('private')->delete($oldimg); + } + // 成功 return ['status_code' => 200, 'success_messages' => ['画像の更新に成功しました。']]; } diff --git a/backend/app/Http/Controllers/Api/FathersController.php b/backend/app/Http/Controllers/Api/FathersController.php index e6b0957d..f22c3f02 100644 --- a/backend/app/Http/Controllers/Api/FathersController.php +++ b/backend/app/Http/Controllers/Api/FathersController.php @@ -325,8 +325,12 @@ class FathersController extends Controller { Storage::disk('private')->put($filename, $image); $father = Father::find((int)$father_id); - if (!is_null($father->image)) { - $oldimg = $father->image; + if (!is_null($father->image) && $father->image != '/assets/default/avatar.jpg') { + $oldimg = str_replace('/files/', '', $father->image); + if (!Storage::disk('private')->exists($oldimg)) { + Log::warning($oldimg.'というパスは不正です。'); + $oldimg = null; + } } $father->image = '/files/'.$filename; $father->save(); @@ -347,7 +351,7 @@ class FathersController extends Controller { } if (!is_null($oldimg)) { - $stor = Storage::disk('private')->delete($oldimg); + Storage::disk('private')->delete($oldimg); } // 親プロフィール画像のアップロードに成功