Skip to content

Commit

Permalink
Further testing and bugfixes of ImageService. Small update to documen…
Browse files Browse the repository at this point in the history
…tation.
  • Loading branch information
hrvojebusic committed Apr 7, 2017
1 parent 4e08de5 commit a8725c4
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/hr/asc/appic/controller/model/ImagePathModel.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}
}
}
47 changes: 22 additions & 25 deletions src/main/java/hr/asc/appic/service/image/AmazonS3ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -176,23 +175,22 @@ public DeferredResult<ResponseEntity<ImagePathModel>> addWishPhoto(String id, Mu
}

@Override
public DeferredResult<ResponseEntity<ImagePathModel>> deleteWishPhoto(String id, String imagePath) {
public DeferredResult<ResponseEntity<ImagePathModel>> deleteWishPhoto(String id, String imageName) {
DeferredResult<ResponseEntity<ImagePathModel>> result = new DeferredResult<>();

ListenableFuture<ImagePathModel> deleteWishPhotoJob = listeningExecutorService.submit(
() -> {
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());
}
);

Expand Down Expand Up @@ -243,23 +241,22 @@ public DeferredResult<ResponseEntity<ImagePathModel>> addStoryPhoto(String id, M
}

@Override
public DeferredResult<ResponseEntity<ImagePathModel>> deleteStoryPhoto(String id, String imagePath) {
public DeferredResult<ResponseEntity<ImagePathModel>> deleteStoryPhoto(String id, String imageName) {
DeferredResult<ResponseEntity<ImagePathModel>> result = new DeferredResult<>();

ListenableFuture<ImagePathModel> deleteStoryPhotoJob = listeningExecutorService.submit(
() -> {
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());
}
);

Expand Down
35 changes: 25 additions & 10 deletions src/main/java/hr/asc/appic/service/image/ImagePaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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());
}
}

0 comments on commit a8725c4

Please sign in to comment.