v7.0.0-beta.2 release
This commit is contained in:
1 parent
6595fd0eb5
commit
fa8eba7a0d
1165 files changed
+107118
-762127
No files matched your search
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Plugins\Sirsoft\Ckeditor5\Http\Controllers;
|
||||
|
||||
use App\Helpers\ResponseHelper;
|
||||
use App\Http\Controllers\Api\Base\PublicBaseController;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Plugins\Sirsoft\Ckeditor5\Services\ImageServeService;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
/**
|
||||
* CKEditor5 이미지 서빙 컨트롤러
|
||||
*
|
||||
* plugins 디스크에 저장된 이미지를 공개 API를 통해 서빙합니다.
|
||||
* 인증 없이 접근 가능한 공개 엔드포인트입니다.
|
||||
*/
|
||||
class ImageServeController extends PublicBaseController
|
||||
{
|
||||
public function __construct(
|
||||
private ImageServeService $imageServeService
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 이미지 서빙
|
||||
*
|
||||
* @param string $hash 이미지 해시 (12자)
|
||||
* @return StreamedResponse|JsonResponse 이미지 스트림 또는 에러 응답
|
||||
*/
|
||||
public function serve(string $hash): StreamedResponse|JsonResponse
|
||||
{
|
||||
$image = $this->imageServeService->findByHash($hash);
|
||||
|
||||
if (! $image) {
|
||||
return ResponseHelper::notFound('messages.image.not_found', [], 'sirsoft-ckeditor5');
|
||||
}
|
||||
|
||||
$response = $this->imageServeService->serve($image);
|
||||
|
||||
if (! $response) {
|
||||
return ResponseHelper::notFound('messages.image.not_found', [], 'sirsoft-ckeditor5');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Plugins\Sirsoft\Ckeditor5\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Api\Base\AdminBaseController;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Plugins\Sirsoft\Ckeditor5\Http\Requests\ImageUploadRequest;
|
||||
use Plugins\Sirsoft\Ckeditor5\Services\ImageUploadService;
|
||||
|
||||
/**
|
||||
* CKEditor5 이미지 업로드 컨트롤러
|
||||
*
|
||||
* CKEditor5 SimpleUploadAdapter가 요구하는 형식으로 이미지를 저장하고
|
||||
* 공개 URL을 반환합니다.
|
||||
*
|
||||
* 응답 형식: { "url": "https://..." }
|
||||
* ResponseHelper 사용 불가 — CKEditor SimpleUploadAdapter는 최상위 { url } 키를 요구합니다.
|
||||
*/
|
||||
class ImageUploadController extends AdminBaseController
|
||||
{
|
||||
public function __construct(
|
||||
private ImageUploadService $imageUploadService
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* 이미지를 업로드하고 공개 URL을 반환합니다.
|
||||
*
|
||||
* CKEditor5 SimpleUploadAdapter 규격:
|
||||
* - 성공: HTTP 201, { "url": "..." }
|
||||
* - 실패: HTTP 4xx/5xx, { "error": { "message": "..." } }
|
||||
*
|
||||
* @param ImageUploadRequest $request 업로드 요청
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function upload(ImageUploadRequest $request): JsonResponse
|
||||
{
|
||||
$uploadPermission = $request->query('permission', '');
|
||||
|
||||
$user = $this->getCurrentUser();
|
||||
if ($uploadPermission && $user && ! $user->hasPermission($uploadPermission)) {
|
||||
return response()->json([
|
||||
'error' => ['message' => __('sirsoft-ckeditor5::messages.upload.forbidden')],
|
||||
], 403);
|
||||
}
|
||||
|
||||
try {
|
||||
$image = $this->imageUploadService->upload(
|
||||
$request->file('upload'),
|
||||
$user?->id
|
||||
);
|
||||
|
||||
// CKEditor SimpleUploadAdapter가 요구하는 최상위 { url } 형식
|
||||
return response()->json(['url' => $image->download_url], 201);
|
||||
} catch (\Exception $e) {
|
||||
Log::error('[sirsoft-ckeditor5] 이미지 업로드 실패', [
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'error' => ['message' => __('sirsoft-ckeditor5::messages.upload.failed')],
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Plugins\Sirsoft\Ckeditor5\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Exceptions\HttpResponseException;
|
||||
|
||||
/**
|
||||
* CKEditor5 이미지 업로드 요청 검증
|
||||
*
|
||||
* CKEditor5 SimpleUploadAdapter가 전송하는 이미지 파일을 검증합니다.
|
||||
*/
|
||||
class ImageUploadRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* 권한 확인 (미들웨어 체인에서 처리)
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 검증 규칙
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
/** @var int $maxSizeMb 최대 파일 크기 (MB) */
|
||||
$maxSizeMb = (int) plugin_setting('sirsoft-ckeditor5', 'imageMaxSizeMb', 2);
|
||||
|
||||
/** @var int $maxSizeKb Laravel max 규칙은 KB 단위 */
|
||||
$maxSizeKb = $maxSizeMb * 1024;
|
||||
|
||||
return [
|
||||
'upload' => [
|
||||
'required',
|
||||
'file',
|
||||
'image',
|
||||
'mimes:jpeg,jpg,png,gif,webp',
|
||||
"max:{$maxSizeKb}",
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 검증 오류 메시지
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function messages(): array
|
||||
{
|
||||
$maxSizeMb = (int) plugin_setting('sirsoft-ckeditor5', 'imageMaxSizeMb', 2);
|
||||
|
||||
return [
|
||||
'upload.required' => __('sirsoft-ckeditor5::messages.upload.required'),
|
||||
'upload.file' => __('sirsoft-ckeditor5::messages.upload.invalid_file'),
|
||||
'upload.image' => __('sirsoft-ckeditor5::messages.upload.not_image'),
|
||||
'upload.mimes' => __('sirsoft-ckeditor5::messages.upload.invalid_mime'),
|
||||
'upload.max' => __('sirsoft-ckeditor5::messages.upload.too_large', ['max' => $maxSizeMb]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 검증 실패 시 CKEditor SimpleUploadAdapter 규격으로 응답합니다.
|
||||
*
|
||||
* Laravel 기본 응답: { message, errors }
|
||||
* CKEditor 요구 형식: { error: { message } }
|
||||
*
|
||||
* @param Validator $validator
|
||||
* @return never
|
||||
*/
|
||||
protected function failedValidation(Validator $validator): never
|
||||
{
|
||||
$firstError = collect($validator->errors()->all())->first()
|
||||
?? __('sirsoft-ckeditor5::messages.upload.failed');
|
||||
|
||||
throw new HttpResponseException(
|
||||
response()->json(['error' => ['message' => $firstError]], 422)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user