Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public class PostServiceImp implements PostService {
public ResponseEntity<?> addPostWithFile(InscriptionPostDto inscriptionPostDto, MultipartFile[] files,
String usernameFromToken) {

List<ImageMetaAndInfo> ls = validateAndExtractImages(files, Collections.emptySet(), true);
User user = userRepository.findByEmail(usernameFromToken);
List<ImageMetaAndInfo> ls = validateAndExtractImages(files, user.getId(), Collections.emptySet(), true);

// Below Line To use for Threshold similarty

Expand All @@ -94,8 +95,6 @@ public ResponseEntity<?> addPostWithFile(InscriptionPostDto inscriptionPostDto,
// .filter(Objects::nonNull)
// .findFirst();

User user = userRepository.findByEmail(usernameFromToken);

InscriptionPost inscriptionPost = new InscriptionPost();

if (inscriptionPostDto != null) {
Expand Down Expand Up @@ -398,7 +397,7 @@ public ResponseEntity<?> updatePost(String usernameFromToken, InscriptionPostDto
List<String> existingImageIds = getExistingImageIds(post);
List<String> imagesToDelete = validateDeletedImageIds(existingImageIds, deletedImageIds, false);
Set<String> deletableImageIds = new HashSet<>(imagesToDelete);
List<ImageMetaAndInfo> newImages = validateAndExtractImages(files, deletableImageIds, false);
List<ImageMetaAndInfo> newImages = validateAndExtractImages(files, user.getId(), deletableImageIds, false);

ensureMinimumImageCount(existingImageIds.size(), deletableImageIds.size(), newImages.size());

Expand Down Expand Up @@ -439,7 +438,7 @@ public ResponseEntity<?> updatePost(String usernameFromToken, InscriptionPostDto
public ResponseEntity<?> addImagesToPost(String usernameFromToken, String postId, MultipartFile[] files) {
InscriptionPost post = getOwnedPost(usernameFromToken, postId);
User user = userRepository.findByEmail(usernameFromToken);
List<ImageMetaAndInfo> newImages = validateAndExtractImages(files, Collections.emptySet(), true);
List<ImageMetaAndInfo> newImages = validateAndExtractImages(files, user.getId(), Collections.emptySet(), true);

List<String> updatedImageIds = getExistingImageIds(post);
updatedImageIds.addAll(saveImages(post.getId(), newImages));
Expand Down Expand Up @@ -606,8 +605,8 @@ private InscriptionPost.Description mergeDescription(InscriptionPost.Description
.build();
}

private List<ImageMetaAndInfo> validateAndExtractImages(MultipartFile[] files, Set<String> replaceableImageIds,
boolean filesRequired) {
private List<ImageMetaAndInfo> validateAndExtractImages(MultipartFile[] files, ObjectId userId,
Set<String> replaceableImageIds, boolean filesRequired) {
if (files == null || files.length == 0) {
if (filesRequired) {
throw new StoneInscriptionException("No File Uploaded", HttpStatus.BAD_REQUEST);
Expand All @@ -626,14 +625,18 @@ private List<ImageMetaAndInfo> validateAndExtractImages(MultipartFile[] files, S
throw new StoneInscriptionException("Duplicate Image Uploaded", HttpStatus.BAD_REQUEST);
}

boolean imageAlreadyExists = ls.stream().anyMatch(image -> {
List<ObjectId> userPostIds = getUserPostIds(userId);

boolean imageAlreadyExists = !userPostIds.isEmpty() && ls.stream().anyMatch(image -> {
Optional<ImagesData> existingImage = imagesDataRepo
.findFirstByMetadata_ImageHashValue(image.getPHash().getHashValue().toString());
.findFirstByMetadata_ImageHashValueAndPostIdIn(
image.getPHash().getHashValue().toString(),
userPostIds);
return existingImage.isPresent() && !replaceableImageIds.contains(existingImage.get().getId());
});

if (imageAlreadyExists) {
throw new StoneInscriptionException("Image Already Uploaded By some User", HttpStatus.CONFLICT);
throw new StoneInscriptionException("Image Already Uploaded By same User", HttpStatus.CONFLICT);
}

return ls;
Expand All @@ -652,6 +655,13 @@ private List<String> saveImages(ObjectId postId, List<ImageMetaAndInfo> images)
.build()).getId()).toList();
}

private List<ObjectId> getUserPostIds(ObjectId userId) {
return inscriptionPostRepo.findByUserId(userId).stream()
.map(InscriptionPost::getId)
.filter(Objects::nonNull)
.toList();
}

private InscriptionPost getOwnedPost(String usernameFromToken, String postId) {
User user = userRepository.findByEmail(usernameFromToken);
Optional<InscriptionPost> inscriptionPost = inscriptionPostRepo.findById(new ObjectId(postId));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.cadac.stone_inscription.repository;

import java.util.Optional;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.cadac.stone_inscription.entity.ImagesData;

import dev.brachtendorf.jimagehash.hash.Hash;

@Repository
public interface ImagesDataRepo extends MongoRepository<ImagesData, String> {

Optional<ImagesData> findFirstByMetadata_ImageHashValue(String imageHashValue);

}
package com.cadac.stone_inscription.repository;

import java.util.Collection;
import java.util.Optional;

import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.cadac.stone_inscription.entity.ImagesData;

@Repository
public interface ImagesDataRepo extends MongoRepository<ImagesData, String> {

Optional<ImagesData> findFirstByMetadata_ImageHashValueAndPostIdIn(String imageHashValue,
Collection<ObjectId> postIds);

}
Loading