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)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Plugins\Sirsoft\Ckeditor5\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use App\Models\User;
|
||||
|
||||
/**
|
||||
* CKEditor5 이미지 업로드 기록 모델
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $hash URL용 고유 해시 (12자)
|
||||
* @property string $original_name 원본 파일명
|
||||
* @property string $file_path 저장 파일 경로
|
||||
* @property string $storage_disk 스토리지 디스크
|
||||
* @property int $file_size 파일 크기 (bytes)
|
||||
* @property string $mime_type MIME 타입
|
||||
* @property int|null $uploaded_by 업로드 사용자 ID
|
||||
* @property \Illuminate\Support\Carbon $created_at
|
||||
* @property \Illuminate\Support\Carbon $updated_at
|
||||
* @property-read string $download_url 이미지 서빙 URL
|
||||
*/
|
||||
class Ckeditor5ImageUpload extends Model
|
||||
{
|
||||
/**
|
||||
* 테이블명
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'ckeditor5_image_uploads';
|
||||
|
||||
/**
|
||||
* 대량 할당 허용 필드
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'hash',
|
||||
'original_name',
|
||||
'file_path',
|
||||
'storage_disk',
|
||||
'file_size',
|
||||
'mime_type',
|
||||
'uploaded_by',
|
||||
];
|
||||
|
||||
/**
|
||||
* 모델 부트 메서드
|
||||
*/
|
||||
protected static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::creating(function (self $model) {
|
||||
if (empty($model->hash)) {
|
||||
$model->hash = self::generateHash();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 고유 해시를 생성합니다.
|
||||
*
|
||||
* @return string 12자리 고유 해시
|
||||
*/
|
||||
public static function generateHash(): string
|
||||
{
|
||||
do {
|
||||
$hash = substr(bin2hex(random_bytes(6)), 0, 12);
|
||||
} while (self::where('hash', $hash)->exists());
|
||||
|
||||
return $hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* 이미지 서빙 URL 반환 (API 라우트 경유)
|
||||
*
|
||||
* @return string 이미지 서빙 URL
|
||||
*/
|
||||
public function getDownloadUrlAttribute(): string
|
||||
{
|
||||
return '/api/plugins/sirsoft-ckeditor5/images/'.$this->hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* 업로드한 사용자
|
||||
*
|
||||
* @return BelongsTo<User, Ckeditor5ImageUpload>
|
||||
*/
|
||||
public function uploader(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'uploaded_by');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Plugins\Sirsoft\Ckeditor5\Providers;
|
||||
|
||||
use App\Contracts\Extension\StorageInterface;
|
||||
use App\Extension\Storage\PluginStorageDriver;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Plugins\Sirsoft\Ckeditor5\Repositories\Contracts\ImageUploadRepositoryInterface;
|
||||
use Plugins\Sirsoft\Ckeditor5\Repositories\ImageUploadRepository;
|
||||
use Plugins\Sirsoft\Ckeditor5\Services\ImageServeService;
|
||||
use Plugins\Sirsoft\Ckeditor5\Services\ImageUploadService;
|
||||
|
||||
/**
|
||||
* CKEditor5 플러그인 서비스 프로바이더
|
||||
*
|
||||
* Repository 인터페이스와 구현체 바인딩,
|
||||
* StorageInterface 주입을 담당합니다.
|
||||
*/
|
||||
class Ckeditor5ServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* 플러그인 식별자
|
||||
*/
|
||||
private const PLUGIN_ID = 'sirsoft-ckeditor5';
|
||||
|
||||
/**
|
||||
* 서비스 컨테이너 바인딩 등록
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
// Repository 바인딩
|
||||
$this->app->bind(
|
||||
ImageUploadRepositoryInterface::class,
|
||||
ImageUploadRepository::class
|
||||
);
|
||||
|
||||
// StorageInterface 바인딩 — plugins 디스크 사용
|
||||
$this->app->when([ImageUploadService::class, ImageServeService::class])
|
||||
->needs(StorageInterface::class)
|
||||
->give(function () {
|
||||
return new PluginStorageDriver(self::PLUGIN_ID, 'plugins');
|
||||
});
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Plugins\Sirsoft\Ckeditor5\Repositories\Contracts;
|
||||
|
||||
use Plugins\Sirsoft\Ckeditor5\Models\Ckeditor5ImageUpload;
|
||||
|
||||
/**
|
||||
* CKEditor5 이미지 업로드 Repository 인터페이스
|
||||
*/
|
||||
interface ImageUploadRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* 해시로 이미지 조회
|
||||
*
|
||||
* @param string $hash 이미지 해시 (12자)
|
||||
* @return Ckeditor5ImageUpload|null
|
||||
*/
|
||||
public function findByHash(string $hash): ?Ckeditor5ImageUpload;
|
||||
|
||||
/**
|
||||
* 이미지 기록 생성
|
||||
*
|
||||
* @param array $data 이미지 데이터
|
||||
* @return Ckeditor5ImageUpload
|
||||
*/
|
||||
public function create(array $data): Ckeditor5ImageUpload;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Plugins\Sirsoft\Ckeditor5\Repositories;
|
||||
|
||||
use Plugins\Sirsoft\Ckeditor5\Models\Ckeditor5ImageUpload;
|
||||
use Plugins\Sirsoft\Ckeditor5\Repositories\Contracts\ImageUploadRepositoryInterface;
|
||||
|
||||
/**
|
||||
* CKEditor5 이미지 업로드 Repository 구현체
|
||||
*/
|
||||
class ImageUploadRepository implements ImageUploadRepositoryInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected Ckeditor5ImageUpload $model
|
||||
) {}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function findByHash(string $hash): ?Ckeditor5ImageUpload
|
||||
{
|
||||
return $this->model->where('hash', $hash)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function create(array $data): Ckeditor5ImageUpload
|
||||
{
|
||||
return $this->model->create($data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Plugins\Sirsoft\Ckeditor5\Services;
|
||||
|
||||
use App\Contracts\Extension\StorageInterface;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Plugins\Sirsoft\Ckeditor5\Models\Ckeditor5ImageUpload;
|
||||
use Plugins\Sirsoft\Ckeditor5\Repositories\Contracts\ImageUploadRepositoryInterface;
|
||||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||
|
||||
/**
|
||||
* CKEditor5 이미지 서빙 서비스
|
||||
*
|
||||
* 업로드된 이미지를 hash로 조회하여 StreamedResponse를 반환합니다.
|
||||
*/
|
||||
class ImageServeService
|
||||
{
|
||||
/**
|
||||
* @param ImageUploadRepositoryInterface $repository 이미지 업로드 리포지토리
|
||||
* @param StorageInterface $storage 플러그인 스토리지 드라이버
|
||||
*/
|
||||
public function __construct(
|
||||
protected ImageUploadRepositoryInterface $repository,
|
||||
protected StorageInterface $storage
|
||||
) {}
|
||||
|
||||
/**
|
||||
* hash로 이미지 조회
|
||||
*
|
||||
* @param string $hash 이미지 해시 (12자)
|
||||
* @return Ckeditor5ImageUpload|null
|
||||
*/
|
||||
public function findByHash(string $hash): ?Ckeditor5ImageUpload
|
||||
{
|
||||
return $this->repository->findByHash($hash);
|
||||
}
|
||||
|
||||
/**
|
||||
* 이미지 스트림 응답 생성
|
||||
*
|
||||
* @param Ckeditor5ImageUpload $image 이미지 업로드 모델
|
||||
* @return StreamedResponse|null 이미지 스트림 또는 스토리지 파일 없을 경우 null
|
||||
*/
|
||||
public function serve(Ckeditor5ImageUpload $image): ?StreamedResponse
|
||||
{
|
||||
// file_path 형태: "images/2026/04/06/{uuid}.jpg"
|
||||
// PluginStorageDriver::response(category, path) 에서 category = 'images'
|
||||
$category = 'images';
|
||||
$relativePath = substr($image->file_path, strlen($category) + 1);
|
||||
|
||||
$response = $this->storage->response(
|
||||
$category,
|
||||
$relativePath,
|
||||
$image->original_name,
|
||||
[
|
||||
'Content-Type' => $image->mime_type,
|
||||
'Cache-Control' => 'public, max-age=31536000',
|
||||
]
|
||||
);
|
||||
|
||||
if (! $response) {
|
||||
Log::error('CKEditor5 이미지 스토리지에 없음', [
|
||||
'image_id' => $image->id,
|
||||
'hash' => $image->hash,
|
||||
'file_path' => $image->file_path,
|
||||
'disk' => $image->storage_disk,
|
||||
]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Plugins\Sirsoft\Ckeditor5\Services;
|
||||
|
||||
use App\Contracts\Extension\StorageInterface;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Str;
|
||||
use Plugins\Sirsoft\Ckeditor5\Models\Ckeditor5ImageUpload;
|
||||
use Plugins\Sirsoft\Ckeditor5\Repositories\Contracts\ImageUploadRepositoryInterface;
|
||||
|
||||
/**
|
||||
* CKEditor5 이미지 업로드 서비스
|
||||
*
|
||||
* 이미지를 plugins 디스크에 저장하고 업로드 기록을 생성합니다.
|
||||
*/
|
||||
class ImageUploadService
|
||||
{
|
||||
/**
|
||||
* @param ImageUploadRepositoryInterface $repository 이미지 업로드 리포지토리
|
||||
* @param StorageInterface $storage 플러그인 스토리지 드라이버
|
||||
*/
|
||||
public function __construct(
|
||||
protected ImageUploadRepositoryInterface $repository,
|
||||
protected StorageInterface $storage
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 이미지를 업로드하고 기록을 저장합니다.
|
||||
*
|
||||
* @param UploadedFile $file 업로드된 파일
|
||||
* @param int|null $uploadedBy 업로드 사용자 ID
|
||||
* @return Ckeditor5ImageUpload 생성된 업로드 기록
|
||||
*/
|
||||
public function upload(UploadedFile $file, ?int $uploadedBy): Ckeditor5ImageUpload
|
||||
{
|
||||
$extension = $file->guessExtension() ?? $file->getClientOriginalExtension();
|
||||
$storedFilename = Str::uuid().'.'.$extension;
|
||||
$path = date('Y/m/d').'/'.$storedFilename;
|
||||
|
||||
$this->storage->put('images', $path, file_get_contents($file->getRealPath()));
|
||||
|
||||
return $this->repository->create([
|
||||
'original_name' => $file->getClientOriginalName(),
|
||||
'file_path' => "images/{$path}",
|
||||
'storage_disk' => $this->storage->getDisk(),
|
||||
'file_size' => $file->getSize(),
|
||||
'mime_type' => $file->getMimeType(),
|
||||
'uploaded_by' => $uploadedBy,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Plugins\Sirsoft\Ckeditor5\Http\Controllers\ImageServeController;
|
||||
use Plugins\Sirsoft\Ckeditor5\Http\Controllers\ImageUploadController;
|
||||
|
||||
/*
|
||||
* sirsoft-ckeditor5 플러그인 API 라우트
|
||||
*
|
||||
* URL prefix: /api/plugins/sirsoft-ckeditor5 (PluginRouteServiceProvider 자동 적용)
|
||||
*
|
||||
* 인증 라우트: AdminBaseController에서 auth:sanctum + admin 미들웨어 적용
|
||||
* 공개 라우트: PublicBaseController 사용, 인증 불필요
|
||||
*/
|
||||
|
||||
// 이미지 업로드 (관리자 인증 필요)
|
||||
Route::post('upload', [ImageUploadController::class, 'upload'])
|
||||
->name('api.sirsoft-ckeditor5.upload');
|
||||
|
||||
// 이미지 서빙 (공개 접근 — CKEditor 에디터 내 <img src> 직접 접근)
|
||||
Route::get('images/{hash}', [ImageServeController::class, 'serve'])
|
||||
->where('hash', '[a-f0-9]{12}')
|
||||
->name('api.sirsoft-ckeditor5.images.serve');
|
||||
Reference in new issue
Block a user