このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
kikikan/backend/app/Http/Controllers/Api/MeetingImagesController.php

102 行
3.3 KiB
PHP
Raw 通常表示 履歴

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
2021-10-15 10:37:47 +09:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use App\Models\MeetingImage;
class MeetingImagesController extends Controller {
2021-10-01 14:22:07 +09:00
public function register (Request $r) {
if (!isset($r->meeting_id) || !isset($r->image)) {
2021-10-06 14:50:13 +09:00
return ['status' => 400];
}
2021-10-31 00:50:35 +09:00
if (MeetingImage::select('id')->where('meeting_id', (int)$r->meeting_id)->count() >= 10) {
2021-10-31 22:23:04 +09:00
return ['status' => 400, 'error_messages' => 'エラー'];
2021-10-01 14:22:07 +09:00
}
2021-10-31 00:15:02 +09:00
// ファイルサイズは10MiB以内
Validator::extend('image_size', function ($attribute, $value, $params, $validator) {
try {
return strlen(base64_decode($value)) < 1048576;
} catch (\Throwable $e) {
Log::critical($e->getMessage());
return false;
2021-10-01 14:22:07 +09:00
}
});
2021-10-31 00:15:02 +09:00
// ミームタイプ
Validator::extend('image_meme', function ($attribute, $value, $params, $validator) {
try {
return (
mime_content_type($value) == 'image/jpeg' || // jpg
mime_content_type($value) == 'image/png' || // png
mime_content_type($value) == 'image/gif' // gif
);
} catch (\Throwable $e) {
Log::critical($e->getMessage());
return false;
}
});
// バリデーションエラー
$validate = Validator::make($r->all(), ['image' => 'image_size|image_meme']);
2021-10-01 14:22:07 +09:00
if ($validate->fails()) {
return ['status_code' => 422, 'error_messages' => $validate->errors()];
}
2021-10-31 00:15:02 +09:00
try {
$ext = explode('/', mime_content_type($r->image))[1];
$filename = uniqid() . '.'.$ext;
$image = base64_decode(substr($r->image, strpos($r->image, ',') + 1));
Storage::disk('public')->put($filename, $image);
2021-10-31 00:15:02 +09:00
$insert = [
2021-10-31 17:45:56 +09:00
'meeting_id' => (int)$r->meeting_id,
2021-10-31 00:15:02 +09:00
'image' => '/storage/'.$filename,
];
2021-10-31 00:15:02 +09:00
MeetingImage::create($insert);
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
return ['status_code' => 400];
2021-10-01 14:22:07 +09:00
}
2021-10-31 17:45:56 +09:00
$meeting_images_select = ['image'];
if (null === ($params = MeetingImage::select($meeting_images_select)->where('meeting_id', (int)$r->meeting_id)->get())) {
return ['status_code' => 400];
}
return ['status_code' => 200, 'params' => $params];
}
2021-10-31 16:34:01 +09:00
public function delete ($image_id) {
if (!isset($image_id)) {
return ['status_code' => 400];
}
2021-10-31 22:23:04 +09:00
if (null === ($get = MeetingImage::select('image')->where('id', (int)$image_id)->first())) {
return ['status_code' => 400];
}
2021-10-07 13:51:53 +09:00
try {
2021-10-31 16:34:01 +09:00
MeetingImage::where('id', $image_id)->delete();
Storage::disk('public')->delete($get->image);
2021-10-07 13:51:53 +09:00
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
return ['status_code' => 400];
2021-10-01 14:22:07 +09:00
}
2021-10-07 13:51:53 +09:00
return ['status_code' => 200];
}
}