プロフィール画像をアップロードしたら、古い画像を削除するようにしました

このコミットが含まれているのは:
守矢諏訪子 2021-11-26 15:19:42 +09:00
コミット 0229aecc69
2個のファイルの変更24行の追加8行の削除

ファイルの表示

@ -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' => ['画像の更新に成功しました。']];
}

ファイルの表示

@ -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);
}
// 親プロフィール画像のアップロードに成功