1212use Ibexa \AdminUi \Form \Data \Asset \ImageAssetUploadData ;
1313use Ibexa \Contracts \AdminUi \Controller \Controller ;
1414use Ibexa \Core \FieldType \Image \Value as ImageValue ;
15- use Ibexa \Core \FieldType \ImageAsset \AssetMapper ;
1615use Ibexa \Core \FieldType \ImageAsset \AssetMapper as ImageAssetMapper ;
1716use JMS \TranslationBundle \Annotation \Desc ;
1817use Symfony \Component \HttpFoundation \JsonResponse ;
2423use Symfony \Component \Validator \Validator \ValidatorInterface ;
2524use Symfony \Contracts \Translation \TranslatorInterface ;
2625
27- class AssetController extends Controller
26+ final class AssetController extends Controller
2827{
29- public const CSRF_TOKEN_HEADER = 'X-CSRF-Token ' ;
28+ public const string CSRF_TOKEN_HEADER = 'X-CSRF-Token ' ;
3029
31- public const LANGUAGE_CODE_KEY = 'languageCode ' ;
32- public const FILE_KEY = 'file ' ;
30+ public const string LANGUAGE_CODE_KEY = 'languageCode ' ;
31+ public const string FILE_KEY = 'file ' ;
3332
34- private ValidatorInterface $ validator ;
35-
36- private CsrfTokenManagerInterface $ csrfTokenManager ;
37-
38- private AssetMapper $ imageAssetMapper ;
39-
40- private TranslatorInterface $ translator ;
41-
42- /**
43- * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
44- * @param \Symfony\Component\Security\Csrf\CsrfTokenManagerInterface $csrfTokenManager
45- * @param \Ibexa\Core\FieldType\ImageAsset\AssetMapper $imageAssetMapper
46- * @param \Symfony\Contracts\Translation\TranslatorInterface $translator
47- */
4833 public function __construct (
49- ValidatorInterface $ validator ,
50- CsrfTokenManagerInterface $ csrfTokenManager ,
51- ImageAssetMapper $ imageAssetMapper ,
52- TranslatorInterface $ translator
34+ private readonly ValidatorInterface $ validator ,
35+ private readonly CsrfTokenManagerInterface $ csrfTokenManager ,
36+ private readonly ImageAssetMapper $ imageAssetMapper ,
37+ private readonly TranslatorInterface $ translator
5338 ) {
54- $ this ->validator = $ validator ;
55- $ this ->csrfTokenManager = $ csrfTokenManager ;
56- $ this ->imageAssetMapper = $ imageAssetMapper ;
57- $ this ->translator = $ translator ;
5839 }
5940
60- /**
61- * @param \Symfony\Component\HttpFoundation\Request $request
62- *
63- * @return \Symfony\Component\HttpFoundation\Response
64- *
65- * @throws \Ibexa\Core\Base\Exceptions\InvalidArgumentType
66- */
6741 public function uploadImageAction (Request $ request ): Response
6842 {
69- if ($ this ->isValidCsrfToken ($ request )) {
70- $ data = new ImageAssetUploadData (
71- $ request ->files ->get (self ::FILE_KEY ),
72- $ request ->request ->get (self ::LANGUAGE_CODE_KEY )
73- );
74-
75- $ errors = $ this ->validator ->validate ($ data );
76- if ($ errors ->count () === 0 ) {
77- try {
78- $ file = $ data ->getFile ();
79-
80- $ content = $ this ->imageAssetMapper ->createAsset (
81- $ file ->getClientOriginalName (),
82- new ImageValue ([
83- 'inputUri ' => $ file ->getRealPath (),
84- 'fileSize ' => $ file ->getSize (),
85- 'fileName ' => $ file ->getClientOriginalName (),
86- 'alternativeText ' => $ file ->getClientOriginalName (),
87- ]),
88- $ data ->getLanguageCode ()
89- );
90-
91- return new JsonResponse ([
92- 'destinationContent ' => [
93- 'id ' => $ content ->contentInfo ->id ,
94- 'name ' => $ content ->getName (),
95- 'locationId ' => $ content ->contentInfo ->mainLocationId ,
96- ],
97- 'value ' => $ this ->imageAssetMapper ->getAssetValue ($ content ),
98- ]);
99- } catch (Exception $ e ) {
100- return $ this ->createGenericErrorResponse ($ e ->getMessage ());
43+ if (!$ this ->isValidCsrfToken ($ request )) {
44+ return $ this ->createInvalidCsrfResponse ();
45+ }
46+
47+ $ data = new ImageAssetUploadData (
48+ $ request ->files ->get (self ::FILE_KEY ),
49+ $ request ->request ->get (self ::LANGUAGE_CODE_KEY )
50+ );
51+
52+ $ errors = $ this ->validator ->validate ($ data );
53+ if ($ errors ->count () === 0 ) {
54+ try {
55+ $ file = $ data ->getFile ();
56+ if ($ file === null ) {
57+ throw new Exception ('File is missing in the request. ' );
10158 }
102- } else {
103- return $ this ->createInvalidInputResponse ($ errors );
59+
60+ $ content = $ this ->imageAssetMapper ->createAsset (
61+ $ file ->getClientOriginalName (),
62+ new ImageValue ([
63+ 'inputUri ' => $ file ->getRealPath (),
64+ 'fileSize ' => $ file ->getSize (),
65+ 'fileName ' => $ file ->getClientOriginalName (),
66+ 'alternativeText ' => $ file ->getClientOriginalName (),
67+ ]),
68+ $ data ->getLanguageCode () ?? ''
69+ );
70+
71+ $ contentInfo = $ content ->getContentInfo ();
72+
73+ return new JsonResponse ([
74+ 'destinationContent ' => [
75+ 'id ' => $ contentInfo ->getId (),
76+ 'name ' => $ content ->getName (),
77+ 'locationId ' => $ contentInfo ->getMainLocationId (),
78+ ],
79+ 'value ' => $ this ->imageAssetMapper ->getAssetValue ($ content ),
80+ ]);
81+ } catch (Exception $ e ) {
82+ return $ this ->createGenericErrorResponse ($ e ->getMessage ());
10483 }
84+ } else {
85+ return $ this ->createInvalidInputResponse ($ errors );
10586 }
106-
107- return $ this ->createInvalidCsrfResponse ();
10887 }
10988
110- /**
111- * @return \Symfony\Component\HttpFoundation\JsonResponse
112- */
11389 private function createInvalidCsrfResponse (): JsonResponse
11490 {
11591 $ errorMessage = $ this ->translator ->trans (
@@ -122,11 +98,6 @@ private function createInvalidCsrfResponse(): JsonResponse
12298 return $ this ->createGenericErrorResponse ($ errorMessage );
12399 }
124100
125- /**
126- * @param \Symfony\Component\Validator\ConstraintViolationListInterface $errors
127- *
128- * @return \Symfony\Component\HttpFoundation\JsonResponse
129- */
130101 private function createInvalidInputResponse (ConstraintViolationListInterface $ errors ): JsonResponse
131102 {
132103 $ errorMessages = [];
@@ -137,11 +108,6 @@ private function createInvalidInputResponse(ConstraintViolationListInterface $er
137108 return $ this ->createGenericErrorResponse (implode (', ' , $ errorMessages ));
138109 }
139110
140- /**
141- * @param string $errorMessage
142- *
143- * @return \Symfony\Component\HttpFoundation\JsonResponse
144- */
145111 private function createGenericErrorResponse (string $ errorMessage ): JsonResponse
146112 {
147113 return new JsonResponse (
@@ -154,11 +120,6 @@ private function createGenericErrorResponse(string $errorMessage): JsonResponse
154120 );
155121 }
156122
157- /**
158- * @param \Symfony\Component\HttpFoundation\Request $request
159- *
160- * @return bool
161- */
162123 private function isValidCsrfToken (Request $ request ): bool
163124 {
164125 $ csrfTokenValue = $ request ->headers ->get (self ::CSRF_TOKEN_HEADER );
0 commit comments