@@ -72,7 +72,8 @@ public class PostServiceImp implements PostService {
7272 public ResponseEntity <?> addPostWithFile (InscriptionPostDto inscriptionPostDto , MultipartFile [] files ,
7373 String usernameFromToken ) {
7474
75- List <ImageMetaAndInfo > ls = validateAndExtractImages (files , Collections .emptySet (), true );
75+ User user = userRepository .findByEmail (usernameFromToken );
76+ List <ImageMetaAndInfo > ls = validateAndExtractImages (files , user .getId (), Collections .emptySet (), true );
7677
7778 // Below Line To use for Threshold similarty
7879
@@ -94,8 +95,6 @@ public ResponseEntity<?> addPostWithFile(InscriptionPostDto inscriptionPostDto,
9495 // .filter(Objects::nonNull)
9596 // .findFirst();
9697
97- User user = userRepository .findByEmail (usernameFromToken );
98-
9998 InscriptionPost inscriptionPost = new InscriptionPost ();
10099
101100 if (inscriptionPostDto != null ) {
@@ -398,7 +397,7 @@ public ResponseEntity<?> updatePost(String usernameFromToken, InscriptionPostDto
398397 List <String > existingImageIds = getExistingImageIds (post );
399398 List <String > imagesToDelete = validateDeletedImageIds (existingImageIds , deletedImageIds , false );
400399 Set <String > deletableImageIds = new HashSet <>(imagesToDelete );
401- List <ImageMetaAndInfo > newImages = validateAndExtractImages (files , deletableImageIds , false );
400+ List <ImageMetaAndInfo > newImages = validateAndExtractImages (files , user . getId (), deletableImageIds , false );
402401
403402 ensureMinimumImageCount (existingImageIds .size (), deletableImageIds .size (), newImages .size ());
404403
@@ -439,7 +438,7 @@ public ResponseEntity<?> updatePost(String usernameFromToken, InscriptionPostDto
439438 public ResponseEntity <?> addImagesToPost (String usernameFromToken , String postId , MultipartFile [] files ) {
440439 InscriptionPost post = getOwnedPost (usernameFromToken , postId );
441440 User user = userRepository .findByEmail (usernameFromToken );
442- List <ImageMetaAndInfo > newImages = validateAndExtractImages (files , Collections .emptySet (), true );
441+ List <ImageMetaAndInfo > newImages = validateAndExtractImages (files , user . getId (), Collections .emptySet (), true );
443442
444443 List <String > updatedImageIds = getExistingImageIds (post );
445444 updatedImageIds .addAll (saveImages (post .getId (), newImages ));
@@ -606,8 +605,8 @@ private InscriptionPost.Description mergeDescription(InscriptionPost.Description
606605 .build ();
607606 }
608607
609- private List <ImageMetaAndInfo > validateAndExtractImages (MultipartFile [] files , Set < String > replaceableImageIds ,
610- boolean filesRequired ) {
608+ private List <ImageMetaAndInfo > validateAndExtractImages (MultipartFile [] files , ObjectId userId ,
609+ Set < String > replaceableImageIds , boolean filesRequired ) {
611610 if (files == null || files .length == 0 ) {
612611 if (filesRequired ) {
613612 throw new StoneInscriptionException ("No File Uploaded" , HttpStatus .BAD_REQUEST );
@@ -626,14 +625,18 @@ private List<ImageMetaAndInfo> validateAndExtractImages(MultipartFile[] files, S
626625 throw new StoneInscriptionException ("Duplicate Image Uploaded" , HttpStatus .BAD_REQUEST );
627626 }
628627
629- boolean imageAlreadyExists = ls .stream ().anyMatch (image -> {
628+ List <ObjectId > userPostIds = getUserPostIds (userId );
629+
630+ boolean imageAlreadyExists = !userPostIds .isEmpty () && ls .stream ().anyMatch (image -> {
630631 Optional <ImagesData > existingImage = imagesDataRepo
631- .findFirstByMetadata_ImageHashValue (image .getPHash ().getHashValue ().toString ());
632+ .findFirstByMetadata_ImageHashValueAndPostIdIn (
633+ image .getPHash ().getHashValue ().toString (),
634+ userPostIds );
632635 return existingImage .isPresent () && !replaceableImageIds .contains (existingImage .get ().getId ());
633636 });
634637
635638 if (imageAlreadyExists ) {
636- throw new StoneInscriptionException ("Image Already Uploaded By some User" , HttpStatus .CONFLICT );
639+ throw new StoneInscriptionException ("Image Already Uploaded By same User" , HttpStatus .CONFLICT );
637640 }
638641
639642 return ls ;
@@ -652,6 +655,13 @@ private List<String> saveImages(ObjectId postId, List<ImageMetaAndInfo> images)
652655 .build ()).getId ()).toList ();
653656 }
654657
658+ private List <ObjectId > getUserPostIds (ObjectId userId ) {
659+ return inscriptionPostRepo .findByUserId (userId ).stream ()
660+ .map (InscriptionPost ::getId )
661+ .filter (Objects ::nonNull )
662+ .toList ();
663+ }
664+
655665 private InscriptionPost getOwnedPost (String usernameFromToken , String postId ) {
656666 User user = userRepository .findByEmail (usernameFromToken );
657667 Optional <InscriptionPost > inscriptionPost = inscriptionPostRepo .findById (new ObjectId (postId ));
0 commit comments