Skip to content

Commit

Permalink
+ Created the first version of gift images
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaroloes committed Nov 24, 2014
1 parent 6c20cb6 commit 63ade5d
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.gradle/
out/
build/
gifts/
*.iml
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ dependencies {
compile("org.apache.commons:commons-lang3:3.3.2")
compile("com.squareup.retrofit:retrofit:1.6.0")
compile("commons-io:commons-io:2.4")
compile('org.imgscalr:imgscalr-lib:4.2')


}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/capstone/potlatch/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.MultiPartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
Expand All @@ -17,6 +18,7 @@
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.servlet.MultipartConfigElement;
import javax.sql.DataSource;

@Import(OAuth2SecurityConfiguration.class)
Expand All @@ -39,6 +41,8 @@
@ComponentScan
public class Application extends RepositoryRestMvcConfiguration {

private static final String MAX_REQUEST_SIZE = "50MB";

public static DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
Expand Down Expand Up @@ -71,6 +75,17 @@ public JpaVendorAdapter jpaVendorAdapter() {
return hibernateJpaVendorAdapter;
}


// This configuration element adds the ability to accept multipart
// requests to the web container.
@Bean
public MultipartConfigElement multipartConfigElement() {
final MultiPartConfigFactory factory = new MultiPartConfigFactory();
factory.setMaxFileSize(MAX_REQUEST_SIZE);
factory.setMaxRequestSize(MAX_REQUEST_SIZE);
return factory.createMultipartConfig();
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@
import com.capstone.potlatch.Constants;
import com.capstone.potlatch.Routes;
import com.capstone.potlatch.models.*;
import com.capstone.potlatch.util.GiftImageFileManager;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.yaml.snakeyaml.util.UriEncoder;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.Principal;
import java.util.Collection;
Expand Down Expand Up @@ -80,9 +86,14 @@ public class GiftsController {
@PreAuthorize("hasRole(mobile)")
@RequestMapping(value = Routes.GIFTS_PATH, method = RequestMethod.POST)
public @ResponseBody Gift create(
@RequestBody Gift gift,
Principal p)
{
@RequestParam("gift") String giftString,
@RequestParam MultipartFile image,
Principal p) throws IOException {
// In order to send in one request the gift data and the image, the gift data must be
// sent as a json string encoded along with the image.
// Here we parse it into a Gift object.
Gift gift = new ObjectMapper().readValue(UriEncoder.decode(giftString), Gift.class);

User u = users.findByUsername(p.getName());
gift.setUser(u);

Expand All @@ -94,7 +105,21 @@ public class GiftsController {
gift.setGiftChain(giftChains.findOne(giftChain.getId()));
}

gifts.save(gift);
gifts.save(gift); // This generates the id, needed for the images.

byte[] imageBytes = IOUtils.toByteArray(image.getInputStream());
GiftImageFileManager imgMan = GiftImageFileManager.get();

// versiones
imgMan.saveImage(gift, Gift.SIZE_FULL, new ByteArrayInputStream(imageBytes));
imgMan.saveImage(gift, Gift.SIZE_MEDIUM, new ByteArrayInputStream(imageBytes));
imgMan.saveImage(gift, Gift.SIZE_SMALL, new ByteArrayInputStream(imageBytes));

gift.setImageUrlFull(imgMan.getImagePath(gift, Gift.SIZE_FULL).toString());
gift.setImageUrlMedium(imgMan.getImagePath(gift, Gift.SIZE_MEDIUM).toString());
gift.setImageUrlSmall(imgMan.getImagePath(gift, Gift.SIZE_SMALL).toString());

gifts.save(gift); // Update the gift
return gift;
}

Expand Down
32 changes: 27 additions & 5 deletions src/main/java/com/capstone/potlatch/models/Gift.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@
@JsonInclude(JsonInclude.Include.NON_NULL)
//@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Gift {
public static final String SIZE_FULL = "full";
public static final String SIZE_MEDIUM = "medium";
public static final String SIZE_SMALL = "small";

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

private String title;
private String description;
private String imageUrl;
private String imageUrlFull;
private String imageUrlMedium;
private String imageUrlSmall;
@ElementCollection
private Set<Long> touchedByUserIds = new HashSet<Long>();
@ElementCollection
Expand Down Expand Up @@ -70,12 +76,28 @@ public void setDescription(String description) {
this.description = description;
}

public String getImageUrl() {
return imageUrl;
public String getImageUrlFull() {
return imageUrlFull;
}

public void setImageUrlFull(String imageUrlFull) {
this.imageUrlFull = imageUrlFull;
}

public String getImageUrlMedium() {
return imageUrlMedium;
}

public void setImageUrlMedium(String imageUrlMedium) {
this.imageUrlMedium = imageUrlMedium;
}

public String getImageUrlSmall() {
return imageUrlSmall;
}

public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
public void setImageUrlSmall(String imageUrlSmall) {
this.imageUrlSmall = imageUrlSmall;
}

public Set<Long> getTouchedByUserIds() {
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/capstone/potlatch/util/GiftImageFileManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.capstone.potlatch.util;

import com.capstone.potlatch.models.Gift;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class GiftImageFileManager {

public static GiftImageFileManager get() throws IOException {
return new GiftImageFileManager();
}

private Path targetDir = Paths.get("gifts");

private GiftImageFileManager() throws IOException{
if(!Files.exists(targetDir)){
Files.createDirectories(targetDir);
}
}

// Private helper method for resolving video file paths
public Path getImagePath(Gift gift, String size) throws IOException {
assert(gift != null);

String dirPathString = "id_" + gift.getId();
Path dirPath = targetDir.resolve(dirPathString);
if(!Files.exists(dirPath)){
Files.createDirectories(dirPath);
}

return dirPath.resolve("image_" + size + ".jpg");
}

// public void copyData(Gift gift, String size, OutputStream out) throws IOException {
// Path source = getImagePath(gift, size);
// if(!Files.exists(source)){
// throw new FileNotFoundException("Unable to find the referenced image file for gift id:"+gift.getId());
// }
// Files.copy(source, out);
// }

public void saveImage(Gift gift, String size, InputStream giftImage) throws IOException{
assert(giftImage != null);

Path target = getImagePath(gift, size);
Files.copy(giftImage, target, StandardCopyOption.REPLACE_EXISTING);
}

}

0 comments on commit 63ade5d

Please sign in to comment.