diff --git a/backend/app/Http/Controllers/Api/FathersController.php b/backend/app/Http/Controllers/Api/FathersController.php index 18f53137..cdb6a5d2 100644 --- a/backend/app/Http/Controllers/Api/FathersController.php +++ b/backend/app/Http/Controllers/Api/FathersController.php @@ -123,6 +123,8 @@ class FathersController extends Controller { } public function registerMain (Request $r) { + if ($r->image == 'null') $r->image = null; + // 電話番号の文字数。 Validator::extend('tel_size', function ($attribute, $value, $params, $validator) { return $this->telsize($value); @@ -161,11 +163,13 @@ class FathersController extends Controller { $password = Hash::make($r->password); - $ext = explode('/', mime_content_type($r->image))[1]; - $lastid = Father::select('id')->orderBy('id', 'desc')->first(); - $filename = $this->uuidv4() . '.'.$ext; - $image = base64_decode(substr($r->image, strpos($r->image, ',') + 1)); - Storage::disk('public')->put($filename, $image); + if (!is_null($r->image)) { + $ext = explode('/', mime_content_type($r->image))[1]; + $lastid = Father::select('id')->orderBy('id', 'desc')->first(); + $filename = $this->uuidv4() . '.'.$ext; + $image = base64_decode(substr($r->image, strpos($r->image, ',') + 1)); + Storage::disk('public')->put($filename, $image); + } try { // DBの値の準備。 @@ -175,7 +179,7 @@ class FathersController extends Controller { 'email' => $get->email, 'password' => $password, 'company' => $r->company, - 'image' => '/storage/'.$filename, + 'image' => !is_null($r->image) ? '/storage/'.$filename : '/assets/default/avatar.jpg', 'profile' => $r->profile, 'tel' => $r->tel, ]; diff --git a/backend/app/Http/Controllers/Api/MeetingImagesController.php b/backend/app/Http/Controllers/Api/MeetingImagesController.php index cc052dd6..35dc4357 100644 --- a/backend/app/Http/Controllers/Api/MeetingImagesController.php +++ b/backend/app/Http/Controllers/Api/MeetingImagesController.php @@ -12,7 +12,7 @@ use App\Models\MeetingImage; class MeetingImagesController extends Controller { public function register (Request $r) { - if (!isset($r->meeting_id) || !isset($r->image)) { + if (!isset($r->meeting_id) || !isset($r->image) || empty(json_decode($r->image))) { return ['status_code' => 400]; } @@ -22,12 +22,12 @@ class MeetingImagesController extends Controller { // ファイルサイズは10MiB以内 Validator::extend('image_size', function ($attribute, $value, $params, $validator) { - return $this->imagesize($value); + return $this->imagesizemulti($value); }); // ミームタイプ Validator::extend('image_meme', function ($attribute, $value, $params, $validator) { - return $this->imagememe($value); + return $this->imagemememulti($value); }); // バリデーションエラー @@ -37,19 +37,20 @@ class MeetingImagesController extends Controller { return ['status_code' => 422, 'error_messages' => $validate->errors()]; } - $ext = explode('/', mime_content_type($r->image))[1]; - $filename = $this->uuidv4() . '.'.$ext; - try { - $image = base64_decode(substr($r->image, strpos($r->image, ',') + 1)); - Storage::disk('public')->put($filename, $image); + foreach (json_decode($r->image) as $img) { + $ext = explode('/', mime_content_type($img))[1]; + $filename = $this->uuidv4() . '.'.$ext; + $image = base64_decode(substr($img, strpos($img, ',') + 1)); + Storage::disk('public')->put($filename, $image); - $insert = [ - 'meeting_id' => (int)$r->meeting_id, - 'image' => '/storage/'.$filename, - ]; + $insert = [ + 'meeting_id' => (int)$r->meeting_id, + 'image' => '/storage/'.$filename, + ]; - MeetingImage::create($insert); + MeetingImage::create($insert); + } } catch (\Throwable $e) { // 失敗 Log::critical($e->getMessage()); @@ -67,17 +68,19 @@ class MeetingImagesController extends Controller { } public function delete ($meeting_id, Request $r) { - if (!isset($meeting_id) || !isset($r->image_id)) { - return ['status_code' => 400]; - } - - if (null === ($get = MeetingImage::select('image')->where('id', (int)$r->image_id)->first())) { + if (!isset($meeting_id) || !isset($r->image_id) || empty(json_decode($r->image_id))) { return ['status_code' => 400]; } try { - MeetingImage::where('id', (int)$r->image_id)->delete(); - Storage::disk('public')->delete($get->image); + foreach (json_decode($r->image_id) as $img) { + if (null === ($get = MeetingImage::select('image')->where('id', (int)$img)->first())) { + return ['status_code' => 400]; + } + + MeetingImage::where('id', (int)$img)->delete(); + Storage::disk('public')->delete($get->image); + } } catch (\Throwable $e) { // 失敗 Log::critical($e->getMessage()); diff --git a/backend/app/Http/Controllers/Controller.php b/backend/app/Http/Controllers/Controller.php index cfc024f8..9e2f97d4 100644 --- a/backend/app/Http/Controllers/Controller.php +++ b/backend/app/Http/Controllers/Controller.php @@ -50,6 +50,7 @@ class Controller extends BaseController } public function imagesizecannull ($value) { + if ($value == 'null') $value = null; if (is_null($value)) return true; return $this->imagesize($value); } @@ -68,6 +69,7 @@ class Controller extends BaseController } public function imagememecannull ($value) { + if ($value == 'null') $value = null; if (is_null($value)) return true; return $this->imagememe($value); } @@ -98,6 +100,25 @@ class Controller extends BaseController } } + public function imagemememulti ($value) { + try { + $ok = true; + foreach (json_decode($value) as $v) { + if ( + mime_content_type($value) != 'image/jpeg' && // jpg + mime_content_type($value) != 'image/png' && // png + mime_content_type($value) != 'image/gif' // gif + ) { + $ok = false; + } + } + return $ok; + } catch (\Throwable $e) { + Log::critical($e->getMessage()); + return false; + } + } + public function imagesizemulti ($value) { try { $ok = true;