|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\RestBundle\Messaging\Controller; |
| 6 | + |
| 7 | +use OpenApi\Attributes as OA; |
| 8 | +use PhpList\Core\Security\Authentication; |
| 9 | +use PhpList\RestBundle\Common\Controller\BaseController; |
| 10 | +use PhpList\Core\Domain\Common\Upload\UploadService; |
| 11 | +use PhpList\RestBundle\Common\Validator\RequestValidator; |
| 12 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 13 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use Symfony\Component\Routing\Attribute\Route; |
| 17 | + |
| 18 | +#[Route('/editor-uploads', name: 'editor_uploads_')] |
| 19 | +class EditorUploadController extends BaseController |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + Authentication $authentication, |
| 23 | + RequestValidator $validator, |
| 24 | + private readonly UploadService $uploadService, |
| 25 | + ) { |
| 26 | + parent::__construct($authentication, $validator); |
| 27 | + } |
| 28 | + |
| 29 | + #[Route('', name: 'upload', methods: ['POST'])] |
| 30 | + #[OA\Post( |
| 31 | + path: '/api/v2/editor-uploads', |
| 32 | + description: 'Uploads an editor asset for use in CKEditor.', |
| 33 | + summary: 'Upload editor asset', |
| 34 | + requestBody: new OA\RequestBody( |
| 35 | + required: true, |
| 36 | + content: new OA\MediaType( |
| 37 | + mediaType: 'multipart/form-data', |
| 38 | + schema: new OA\Schema( |
| 39 | + properties: [ |
| 40 | + new OA\Property( |
| 41 | + property: 'upload', |
| 42 | + description: 'Asset file to upload', |
| 43 | + type: 'string', |
| 44 | + format: 'binary' |
| 45 | + ), |
| 46 | + new OA\Property( |
| 47 | + property: 'file', |
| 48 | + description: 'Alternative asset file field name', |
| 49 | + type: 'string', |
| 50 | + format: 'binary' |
| 51 | + ), |
| 52 | + ], |
| 53 | + type: 'object' |
| 54 | + ) |
| 55 | + ) |
| 56 | + ), |
| 57 | + tags: ['editor-uploads'], |
| 58 | + parameters: [ |
| 59 | + new OA\Parameter( |
| 60 | + name: 'php-auth-pw', |
| 61 | + description: 'Session key obtained from login', |
| 62 | + in: 'header', |
| 63 | + required: true, |
| 64 | + schema: new OA\Schema(type: 'string') |
| 65 | + ), |
| 66 | + ], |
| 67 | + responses: [ |
| 68 | + new OA\Response( |
| 69 | + response: 201, |
| 70 | + description: 'Asset uploaded', |
| 71 | + content: new OA\JsonContent( |
| 72 | + properties: [ |
| 73 | + new OA\Property(property: 'uploaded', type: 'boolean', example: true), |
| 74 | + new OA\Property(property: 'fileName', type: 'string', example: 'f47ac10b58cc4372a5670e02b2c3d479.png'), |
| 75 | + new OA\Property(property: 'url', type: 'string', example: 'https://example.com/uploadfiles/f47ac10b58cc4372a5670e02b2c3d479.png'), |
| 76 | + new OA\Property(property: 'default', type: 'string', example: 'https://example.com/uploadfiles/f47ac10b58cc4372a5670e02b2c3d479.png'), |
| 77 | + new OA\Property(property: 'mimeType', type: 'string', example: 'image/png'), |
| 78 | + new OA\Property(property: 'size', type: 'integer', example: 123456, nullable: true), |
| 79 | + new OA\Property(property: 'extension', type: 'string', example: 'png'), |
| 80 | + ], |
| 81 | + type: 'object' |
| 82 | + ) |
| 83 | + ), |
| 84 | + new OA\Response( |
| 85 | + response: 400, |
| 86 | + description: 'Bad request', |
| 87 | + content: new OA\JsonContent(ref: '#/components/schemas/BadRequestResponse') |
| 88 | + ), |
| 89 | + new OA\Response( |
| 90 | + response: 401, |
| 91 | + description: 'Unauthorized', |
| 92 | + content: new OA\JsonContent(ref: '#/components/schemas/UnauthorizedResponse') |
| 93 | + ), |
| 94 | + new OA\Response( |
| 95 | + response: 413, |
| 96 | + description: 'Payload too large', |
| 97 | + content: new OA\JsonContent(ref: '#/components/schemas/GenericErrorResponse') |
| 98 | + ), |
| 99 | + new OA\Response( |
| 100 | + response: 500, |
| 101 | + description: 'Storage failure', |
| 102 | + content: new OA\JsonContent(ref: '#/components/schemas/GenericErrorResponse') |
| 103 | + ), |
| 104 | + ] |
| 105 | + )] |
| 106 | + public function uploadAsset(Request $request): JsonResponse |
| 107 | + { |
| 108 | + $this->requireAuthentication($request); |
| 109 | + |
| 110 | + /** @var UploadedFile|null $uploadedFile */ |
| 111 | + $uploadedFile = $request->files->get('upload') ?? $request->files->get('file'); |
| 112 | + $uploadResult = $this->uploadService->upload($uploadedFile); |
| 113 | + |
| 114 | + return new JsonResponse( |
| 115 | + [ |
| 116 | + 'uploaded' => true, |
| 117 | + 'fileName' => $uploadResult->getFilename(), |
| 118 | + 'url' => $uploadResult->getUrl(), |
| 119 | + 'default' => $uploadResult->getUrl(), |
| 120 | + 'mimeType' => $uploadResult->getMimeType(), |
| 121 | + 'size' => $uploadResult->getSize(), |
| 122 | + 'extension' => $uploadResult->getExtension(), |
| 123 | + ], |
| 124 | + Response::HTTP_CREATED |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments