diff --git a/README.md b/README.md index 2cb0898..362a15d 100644 --- a/README.md +++ b/README.md @@ -611,6 +611,7 @@ * **Error response:** will be elaborated at a later time #### Image removal +* **Note:** Image name parameter is not needed when removing user's profile picture. * **URL:** `/image/:resource/:id/:imageName` * **Method:** `DELETE` * **URL paramteres:** `resource = ['user','wish','story']`, `id = String`, `imageName = String` diff --git a/src/main/java/hr/asc/appic/controller/model/ImagePathModel.java b/src/main/java/hr/asc/appic/controller/model/ImagePathModel.java index 6c9d2a6..94d5e77 100644 --- a/src/main/java/hr/asc/appic/controller/model/ImagePathModel.java +++ b/src/main/java/hr/asc/appic/controller/model/ImagePathModel.java @@ -1,14 +1,14 @@ package hr.asc.appic.controller.model; -import java.util.Collections; -import java.util.List; - import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.experimental.Accessors; +import java.util.Collections; +import java.util.List; + @Getter @Setter @AllArgsConstructor @@ -21,6 +21,11 @@ public class ImagePathModel { public ImagePathModel(String id, String path) { this.id = id; - this.paths = Collections.singletonList(path); + + if (path == null) { + paths = Collections.emptyList(); + } else { + paths = Collections.singletonList(path); + } } } diff --git a/src/main/java/hr/asc/appic/service/image/AmazonS3ImageService.java b/src/main/java/hr/asc/appic/service/image/AmazonS3ImageService.java index 5260671..135c1cf 100644 --- a/src/main/java/hr/asc/appic/service/image/AmazonS3ImageService.java +++ b/src/main/java/hr/asc/appic/service/image/AmazonS3ImageService.java @@ -37,11 +37,6 @@ @Service public class AmazonS3ImageService implements ImageService { - @Value("${aws-bucket-image}") - private String bucket; - @Autowired - private AmazonS3 client; - @Autowired private ListeningExecutorService listeningExecutorService; @@ -52,6 +47,10 @@ public class AmazonS3ImageService implements ImageService { @Autowired private StoryRepository storyRepository; + @Value("${aws-bucket-image}") + private String bucket; + @Autowired + private AmazonS3 client; @Autowired private ImagePaths imagePaths; @@ -176,7 +175,7 @@ public DeferredResult> addWishPhoto(String id, Mu } @Override - public DeferredResult> deleteWishPhoto(String id, String imagePath) { + public DeferredResult> deleteWishPhoto(String id, String imageName) { DeferredResult> result = new DeferredResult<>(); ListenableFuture deleteWishPhotoJob = listeningExecutorService.submit( @@ -184,15 +183,14 @@ public DeferredResult> deleteWishPhoto(String id, Wish wish = wishRepository.findById(id).get(); Assert.notNull(wish, "Could not find wish with id: " + id); - String deleteUrl = imagePaths.deleteUrl(wish, imagePath); - if (wish.getPictures().contains(imagePath)) { - wish.getPictures().remove(imagePath); - deleteImage(deleteUrl); - wishRepository.save(wish); - return new ImagePathModel(id, wish.getPictures()); - } else { - throw new IllegalArgumentException("Wish does not contain specified image"); - } + String accessUrl = imagePaths.accessUrl(wish, imageName); + String deleteUrl = imagePaths.deleteUrl(wish, imageName); + + wish.getPictures().remove(accessUrl); + deleteImage(deleteUrl); + + wishRepository.save(wish); + return new ImagePathModel(id, wish.getPictures()); } ); @@ -243,7 +241,7 @@ public DeferredResult> addStoryPhoto(String id, M } @Override - public DeferredResult> deleteStoryPhoto(String id, String imagePath) { + public DeferredResult> deleteStoryPhoto(String id, String imageName) { DeferredResult> result = new DeferredResult<>(); ListenableFuture deleteStoryPhotoJob = listeningExecutorService.submit( @@ -251,15 +249,14 @@ public DeferredResult> deleteStoryPhoto(String id Story story = storyRepository.findById(id).get(); Assert.notNull(story, "Could not find story with id: " + id); - String deleteUrl = imagePaths.deleteUrl(story, imagePath); - if (story.getPictures().contains(imagePath)) { - story.getPictures().remove(imagePath); - deleteImage(deleteUrl); - storyRepository.save(story); - return new ImagePathModel(id, story.getPictures()); - } else { - throw new IllegalArgumentException("Story does not contain specified image"); - } + String accessUrl = imagePaths.accessUrl(story, imageName); + String deleteUrl = imagePaths.deleteUrl(story, imageName); + + story.getPictures().remove(accessUrl); + deleteImage(deleteUrl); + + storyRepository.save(story); + return new ImagePathModel(id, story.getPictures()); } ); diff --git a/src/main/java/hr/asc/appic/service/image/ImagePaths.java b/src/main/java/hr/asc/appic/service/image/ImagePaths.java index 81d8a0f..9ff0c96 100644 --- a/src/main/java/hr/asc/appic/service/image/ImagePaths.java +++ b/src/main/java/hr/asc/appic/service/image/ImagePaths.java @@ -29,11 +29,12 @@ public ImagePaths(@Value("${aws-region}") String region, accessRoot = region + "." + url + "/" + bucket + "/"; } - public String accessUrl(String resourceUrl) { return accessRoot + resourceUrl; } + // ======================================== User ======================================== // + public String uploadUrl(User u) { return userDir + "/" + u.getId() + "_" + new Date().getTime(); } @@ -42,28 +43,42 @@ public String deleteUrl(User u) { if (u.getProfilePicture() != null) { return u.getProfilePicture().substring(accessRoot.length()); } - throw new NullPointerException("Image for user is not present"); + throw new IllegalArgumentException("Image for user is not present"); } + // ======================================== Wish ======================================== // + public String uploadUrl(Wish wish) { return wishDir + "/" + wish.getId() + "_" + new Date().getTime(); } - public String deleteUrl(Wish wish, String imagePath) { - if (wish.getPictures().contains(imagePath)) { - return imagePath.substring(accessRoot.length()); + public String accessUrl(Wish wish, String imageName) { + String fullImagePath = accessRoot + wishDir + "/" + imageName; + if (wish.getPictures().contains(fullImagePath)) { + return fullImagePath; } - throw new NullPointerException("Image for wish is not present"); + throw new IllegalArgumentException("Image for wish is not present: " + imageName); + } + + public String deleteUrl(Wish wish, String imageName) { + return accessUrl(wish, imageName).substring(accessRoot.length()); } + // ======================================== Story ======================================== // + public String uploadUrl(Story story) { return storyDir + "/" + story.getId() + "_" + new Date().getTime(); } - public String deleteUrl(Story story, String imagePath) { - if (story.getPictures().contains(imagePath)) { - return imagePath.substring(accessRoot.length()); + public String accessUrl(Story story, String imageName) { + String fullImagePath = accessRoot + storyDir + "/" + imageName; + if (story.getPictures().contains(fullImagePath)) { + return fullImagePath; } - throw new NullPointerException("Image for story is not present"); + throw new IllegalArgumentException("Image for story is not present: " + imageName); + } + + public String deleteUrl(Story story, String imageName) { + return accessUrl(story, imageName).substring(accessRoot.length()); } }