in src/main/java/com/amazon/photosharing/facade/ContentFacade.java [90:178]
public Media uploadPictureToS3(User p_user, String p_file_name, InputStream p_file_stream, String p_content_type, Comment ... _comments) throws IOException {
Media media = null;
try {
ContentHelper.getInstance().createS3BucketIfNotExists(ContentHelper.getInstance().getConfiguredBucketName());
beginTx();
String s3Key = S3Helper.createS3Key(p_file_name, p_user.getUserName(), new Date());
String s3ThumbKey = S3Helper.createS3Key("thumb_"+p_file_name, p_user.getUserName(), new Date());
byte[] original_bytes = null;
byte[] thumb_bytes = null;
//clone a byte[] of the input original for image resize and thumb clone
ByteArrayOutputStream byte_worker = new ByteArrayOutputStream();
ImageIO.write(ImageIO.read(p_file_stream), p_file_name.substring(p_file_name.lastIndexOf(".")+1), byte_worker);
original_bytes = byte_worker.toByteArray();
try {
thumb_bytes = new MediaResizeTask(new ByteArrayInputStream(original_bytes), p_file_name).call();
} catch (Exception e) {
_logger.error(e.getMessage(), e);
}
User u = em().find(User.class, p_user.getId());
media = new Media();
media.setS3Bucket(ContentHelper.getInstance().getConfiguredBucketName());
media.setS3FileName(s3Key);
media.setS3ThumbFileName(s3ThumbKey);
media.setName(p_file_name);
media.setUser(u);
if (_comments != null) {
for (Comment comment : _comments) {
comment.setMedia(media);
media.getComments().add(comment);
}
}
u.getMedia().add(media);
em().persist(u);
commitTx();
ContentHelper.getInstance().uploadContent(p_content_type, thumb_bytes.length, ContentHelper.getInstance().getConfiguredBucketName(), s3ThumbKey, new ByteArrayInputStream(thumb_bytes));
ContentHelper.getInstance().uploadContent(p_content_type, original_bytes.length, ContentHelper.getInstance().getConfiguredBucketName(), s3Key, new ByteArrayInputStream(original_bytes));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//do some sleeping here..
}
} catch (AmazonServiceException ase) {
_logger.info("Caught an AmazonServiceException, which " +
"means your request made it " +
"to Amazon S3, but was rejected with an error response" +
" for some reason.");
_logger.info("Error Message: " + ase.getMessage());
_logger.info("HTTP Status Code: " + ase.getStatusCode());
_logger.info("AWS Error Code: " + ase.getErrorCode());
_logger.info("Error Type: " + ase.getErrorType());
_logger.info("Request ID: " + ase.getRequestId());
try {
rollbackTx();
} catch (Exception ex) {}
ase.printStackTrace();
} catch (AmazonClientException ace) {
_logger.info("Caught an AmazonClientException, which " +
"means the client encountered " +
"an internal error while trying to " +
"communicate with S3, " +
"such as not being able to access the network.");
_logger.info("Error Message: " + ace.getMessage());
ace.printStackTrace();
rollbackTx();
}
return media;
}