40
40
use Symfony \Component \HttpFoundation \BinaryFileResponse ;
41
41
use Symfony \Component \HttpFoundation \ResponseHeaderBag ;
42
42
43
+ use Symfony \Component \Validator \Constraints as Assert ;
44
+
43
45
/**
44
46
* @Version({"1.0"})
45
47
*
@@ -188,11 +190,16 @@ public function postRegisterAction(Request $request)
188
190
// Validate Client credentials
189
191
$ this ->validateClient ($ request );
190
192
191
- // Set User data to ve validated next
192
- $ this ->setUserData ($ request , $ user );
193
+ // Set User data which will also return Image Validation errors, if any
194
+ $ validationErrorsImage = $ this ->setUserData ($ request , $ user );
193
195
194
- // Validate
195
- $ validationGroups = array ('Registration ' , 'profile_edit ' );
196
+ // If Image Validtion returns error, then return errors
197
+ if ( $ validationErrorsImage ) {
198
+ return $ validationErrorsImage ;
199
+ }
200
+
201
+ // Validate rest of the input data
202
+ $ validationGroups = array ('Registration ' );
196
203
$ validationErrors = $ this ->reportValidationErrors ($ user , $ validationGroups , $ request ->getLocale ());
197
204
198
205
// If Validtion returns error, then return errors
@@ -324,7 +331,8 @@ public function getProfileAction()
324
331
'firstname ' => $ user ->getFirstname (),
325
332
'lastname ' => $ user ->getLastname (),
326
333
'dob ' => $ dobString ,
327
- 'email ' => $ user ->getEmail ()
334
+ 'email ' => $ user ->getEmail (),
335
+ 'image_url ' => $ this ->getParameter ('images_profile_dir ' ).$ user ->getImage ()
328
336
));
329
337
}
330
338
@@ -350,7 +358,7 @@ public function getProfilePicAction()
350
358
$ this ->logAndThrowError (400 , 'Invalid User ' , $ this ->get ('translator ' )->trans ('api.show_error_perm_show ' , array (), 'messages ' , $ request ->getLocale ()), $ request ->getLocale ());
351
359
}
352
360
353
- $ file = $ user ->getImage () ? new File ($ this ->getParameter ('images_profile_directory ' ). ' / ' .$ user ->getImage ()) : null ;
361
+ $ file = $ user ->getImage () ? new File ($ this ->getParameter ('images_profile_path ' ) .$ user ->getImage ()) : null ;
354
362
355
363
$ response = new BinaryFileResponse ($ file );
356
364
$ response ->setContentDisposition (ResponseHeaderBag::DISPOSITION_ATTACHMENT );
@@ -435,14 +443,10 @@ public function editProfilePicAction()
435
443
$ this ->logAndThrowError (400 , 'Invalid User ' , $ this ->get ('translator ' )->trans ('api.show_error_perm_show ' , array (), 'messages ' , $ request ->getLocale ()), $ request ->getLocale ());
436
444
}
437
445
438
- // Set User data to ve validated next
439
- $ this ->setUserPicData ($ request , $ user );
446
+ // Set User data which will also return Image Validation errors, if any
447
+ $ validationErrors = $ this ->setUserPicData ($ request , $ user );
440
448
441
- // Validate
442
- $ validationGroups = array ('profile_pic ' );
443
- $ validationErrors = $ this ->reportValidationErrors ($ user , $ validationGroups , $ request ->getLocale ());
444
-
445
- // If Validtion returns error, then return errors
449
+ // If Image Validtion returns error, then return errors
446
450
if ( $ validationErrors ) {
447
451
return $ validationErrors ;
448
452
}
@@ -457,6 +461,7 @@ public function editProfilePicAction()
457
461
return new JsonResponse (array (
458
462
'code ' => 201 ,
459
463
'show_message ' => $ msg ,
464
+ 'image_url ' => $ this ->getParameter ('images_profile_dir ' ).$ user ->getImage ()
460
465
));
461
466
}
462
467
@@ -733,21 +738,12 @@ private function validateOldPassword(User $user, $oldPassword, $locale)
733
738
734
739
private function setUserData (Request $ request , User $ user )
735
740
{
736
- // $file stores the uploaded Image file
737
- /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
738
- $ file = $ request ->files ->get ('image ' );
739
-
740
- // If a file has been uploaded
741
- if ( null != $ file ) {
742
- // Generate a unique name for the file before saving it
743
- $ fileName = md5 (uniqid ()).'. ' .$ file ->guessExtension ();
744
-
745
- // Move the file to the directory where images are stored
746
- $ file ->move ($ this ->getParameter ('images_profile_directory ' ), $ fileName );
741
+ // Set User data which will also return Validation errors, if any
742
+ $ validationErrors = $ this ->setUserPicData ($ request , $ user );
747
743
748
- // Update the 'image' property to store the Image file name
749
- // instead of its contents
750
- $ user -> setImage ( $ this -> getParameter ( ' images_profile_directory ' ). $ fileName ) ;
744
+ // If Validtion returns error, then return errors
745
+ if ( $ validationErrors ) {
746
+ return $ validationErrors ;
751
747
}
752
748
753
749
$ user ->setUsername ($ request ->request ->get ('username ' ));
@@ -765,25 +761,86 @@ private function setUserData(Request $request, User $user)
765
761
if (!$ timestamp ) {
766
762
$ this ->logAndThrowError (400 , 'Date of Birth should be in MM/DD/YYYY format. ' , $ this ->get ('translator ' )->trans ('api.show_error_dob ' , array (), 'messages ' , $ request ->getLocale ()), $ request ->getLocale ());
767
763
}
764
+
765
+ // return null to indicate success
766
+ return null ;
768
767
}
769
768
770
769
private function setUserPicData (Request $ request , User $ user )
771
770
{
771
+ $ locale = $ request ->getLocale ();
772
+
772
773
// $file stores the uploaded Image file
773
774
/** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
774
775
$ file = $ request ->files ->get ('image ' );
775
776
776
- // If a file has been uploaded
777
+ // File is Valid. Now save it.
777
778
if ( null != $ file ) {
779
+ // First validate uploaded image. If errors found, return errors
780
+ $ imageErrors = $ this ->validateImage ($ request );
781
+ if (!$ imageErrors ) {
782
+ return $ imageErrors ;
783
+ }
784
+
778
785
// Generate a unique name for the file before saving it
779
786
$ fileName = md5 (uniqid ()).'. ' .$ file ->guessExtension ();
780
787
781
788
// Move the file to the directory where images are stored
782
- $ file ->move ($ this ->getParameter ('images_profile_directory ' ), $ fileName );
789
+ $ file ->move ($ this ->getParameter ('images_profile_path ' ), $ fileName );
783
790
784
791
// Update the 'image' property to store the Image file name
785
792
// instead of its contents
786
- $ user ->setImage ($ this ->getParameter ('images_profile_directory ' ).$ fileName );
793
+ $ user ->setImage ($ fileName );
794
+ }
795
+
796
+ // Null is returned to indicate no errors
797
+ return null ;
798
+ }
799
+
800
+ private function validateImage (Request $ request )
801
+ {
802
+ $ locale = $ request ->getLocale ();
803
+
804
+ // $file stores the uploaded Image file
805
+ /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
806
+ $ file = $ request ->files ->get ('image ' );
807
+
808
+ $ imageConstraint = new Assert \Image ();
809
+
810
+ // all constraint "options" can be set this way
811
+ $ imageConstraint ->mimeTypes = ["image/jpeg " , "image/jpg " , "image/gif " , "image/png " ];
812
+ $ imageConstraint ->mimeTypesMessage = 'Please upload a valid Image (jpeg/jpg/gif/png only within 1024k size ' ;
813
+ $ imageConstraint ->maxSize = 1024 *1024 ;
814
+ $ imageConstraint ->minWidth = 100 ;
815
+ $ imageConstraint ->minHeight = 100 ;
816
+ // $imageConstraint->payload->api_error = 'api.show_error_image';
817
+
818
+ // use the validator to validate the value
819
+ $ errors = $ this ->get ('validator ' )->validate ($ file , $ imageConstraint );
820
+
821
+ if (0 != count ($ errors )) {
822
+ // this is *not* a valid image
823
+ $ errorArray = [];
824
+ foreach ($ errors as $ error ) {
825
+ $ constraint = $ error ->getConstraint ();
826
+ $ errorItem = array (
827
+ "error_description " => $ error ->getPropertyPath ().': ' .$ error ->getMessage ().' ' .$ error ->getInvalidValue (),
828
+ "show_message " => $ this ->get ('translator ' )->trans ($ constraint ->payload ['api_error ' ], array (), 'messages ' , $ locale )
829
+ );
830
+ array_push ($ errorArray , $ errorItem );
831
+ $ this ->logMessage (400 , $ errorItem ['error_description ' ] );
832
+ }
833
+
834
+ return new JsonResponse (array (
835
+ "code " => 400 ,
836
+ "error " => "Bad Request " ,
837
+ "error_description " => $ errorArray [0 ]['error_description ' ],
838
+ "show_message " => $ errorArray [0 ]['show_message ' ],
839
+ 'errors ' => $ errorArray
840
+ ));
841
+ } else {
842
+ // Null is returned to indicate no errors
843
+ return null ;
787
844
}
788
845
}
789
846
0 commit comments