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

111 行
3.9 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 Image;
use App\Models\MeetingImage;
class MeetingImagesController extends Controller {
2021-10-01 14:22:07 +09:00
public function register (Request $r) {
2021-11-25 12:24:00 +09:00
if (!isset($r->meeting_id) || !isset($r->image) || empty(json_decode($r->image))) {
2021-10-31 22:26:12 +09:00
return ['status_code' => 400];
2021-10-06 14:50:13 +09:00
}
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:32:17 +09:00
return ['status_code' => 400, 'error_messages' => '画像は最大10個までです。'];
2021-10-01 14:22:07 +09:00
}
if (count(Storage::disk('private')->files('/')) >= 9999) {
Log::critical('ストレージの限界を超えています。9999個ファイルまで保存可能ですので、不要なファイルを削除して下さい。');
2021-11-26 16:15:22 +09:00
return ['status_code' => 400, 'error_messages' => ['親の更新に失敗しました。']];
}
2021-10-31 00:15:02 +09:00
// ファイルサイズは10MiB以内
Validator::extend('image_size', function ($attribute, $value, $params, $validator) {
2021-11-25 12:15:15 +09:00
return $this->imagesizemulti($value);
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) {
2021-11-25 12:15:15 +09:00
return $this->imagemememulti($value);
2021-10-31 00:15:02 +09:00
});
// バリデーションエラー
$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-11-25 14:51:36 +09:00
$fname = [];
2021-10-31 00:15:02 +09:00
try {
2021-11-25 12:24:00 +09:00
foreach (json_decode($r->image) as $img) {
$filename = $this->uuidv4() . '.jpg';
$fname[] = $filename;
2021-11-25 12:11:50 +09:00
$image = base64_decode(substr($img, strpos($img, ',') + 1));
Storage::disk('private')->put($filename, $image);
$quality = 1;
$imag = Image::make('/work/storage/app/private/'.$filename)->encode('jpg', $quality);
$imag->save('/work/storage/app/private/'.$filename);
2021-11-25 12:11:50 +09:00
$insert = [
'meeting_id' => (int)$r->meeting_id,
'image' => '/files/'.$filename,
2021-11-25 12:11:50 +09:00
];
MeetingImage::create($insert);
}
2021-10-31 00:15:02 +09:00
} catch (\Throwable $e) {
// 失敗
Log::critical($e->getMessage());
2021-11-25 14:51:36 +09:00
foreach ($fname as $filename) {
Storage::disk('private')->delete($filename);
2021-11-25 14:51:36 +09:00
}
2021-10-31 00:15:02 +09:00
return ['status_code' => 400];
2021-10-01 14:22:07 +09:00
}
2021-10-31 22:46:49 +09:00
$meeting_images_select = ['id', 'image'];
2021-10-31 17:45:56 +09:00
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];
}
public function delete ($meeting_id, Request $r) {
2021-11-25 13:14:50 +09:00
if (!isset($meeting_id) || !isset($r->image_id)) {
return ['status_code' => 400];
}
2021-11-25 13:14:50 +09:00
if (null === ($get = MeetingImage::select('image')->where('id', (int)$r->image_id)->first())) {
return ['status_code' => 400];
}
2021-11-25 12:11:50 +09:00
2021-11-25 13:14:50 +09:00
try {
MeetingImage::where('id', (int)$r->image_id)->delete();
Storage::disk('private')->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-31 22:46:49 +09:00
$meeting_images_select = ['id', 'image'];
2021-10-31 22:27:54 +09:00
2021-10-31 23:22:14 +09:00
if (null === ($params = MeetingImage::select($meeting_images_select)->where('meeting_id', (int)$meeting_id)->get())) {
$params = [];
2021-10-31 22:27:54 +09:00
}
return ['status_code' => 200, 'params' => $params];
}
}