Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images Part 1 and Part 2 #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions src/main/java/com/google/codeu/data/Datastore.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public void storeMessage(Message message) {
messageEntity.setProperty("text", message.getText());
messageEntity.setProperty("timestamp", message.getTimestamp());
messageEntity.setProperty("recipient", message.getRecipient());

if(message.getImageUrl() != null) {
messageEntity.setProperty("imageUrl", message.getImageUrl());
}
datastore.put(messageEntity);
}

Expand All @@ -58,8 +60,8 @@ public List<Message> messageConverter(PreparedQuery results) {
String recipient = (String) entity.getProperty("recipient");
String text = (String) entity.getProperty("text");
long timestamp = (long) entity.getProperty("timestamp");

Message message = new Message(id, user, text, timestamp, recipient);
String imageUrl = (String) entity.getProperty("imageUrl");
Message message = new Message(id, user, text, timestamp, recipient, imageUrl);
messages.add(message);
} catch (Exception e) {
System.err.println("Error reading message.");
Expand Down
45 changes: 43 additions & 2 deletions src/main/java/com/google/codeu/data/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ public class Message {
private String text;
private long timestamp;
private String recipient;
private String imageUrl;
private int positiveVote = 0;
private int negativeVote = 0;

/**
* Constructs a new {@link Message} posted by {@code user} with {@code text} content to a {@code
* recipient}. Generates a random ID and uses the current system time for the creation time.
*/
public Message(String user, String text, String recipient) {
this(UUID.randomUUID(), user, text, System.currentTimeMillis(), recipient);
this(UUID.randomUUID(), user, text, System.currentTimeMillis(), recipient, "none");
}

public Message(UUID id, String user, String text, long timestamp, String recipient) {
public Message(UUID id, String user, String text, long timestamp, String recipient, String imageUrl) {
this.id = id;
this.user = user;
this.text = text;
this.timestamp = timestamp;
this.recipient = recipient;
this.imageUrl = imageUrl;
}

public UUID getId() {
Expand All @@ -62,4 +66,41 @@ public long getTimestamp() {
public String getRecipient() {
return recipient;
}

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

public Object getImageUrl() {
// TODO Auto-generated method stub
return imageUrl;
}

public int getNegativeVote() {
return negativeVote;
}

public void setNegativeVote(int negativeVote) {
this.negativeVote = negativeVote;
}

public void incrementNegativeVote() {
this.negativeVote++;
}

public int getPositiveVote() {
return positiveVote;
}

public void setPositiveVote(int positiveVote) {
this.positiveVote = positiveVote;
}

public void incrementPositiveVote() {
this.positiveVote++;
}

public int getMessageScore() {
return this.positiveVote-this.negativeVote;
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/google/codeu/servlets/ImageUploadUrlServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.google.codeu.servlets;

import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Provides access to a URL that allows a user to upload an image to Blobstore.
*/
@WebServlet("/image-upload-url")
public class ImageUploadUrlServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String uploadUrl = blobstoreService.createUploadUrl("/messages") ;

response.setContentType("text/html");
response.getOutputStream().println(uploadUrl);
}
}
28 changes: 26 additions & 2 deletions src/main/java/com/google/codeu/servlets/MessageServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@

package com.google.codeu.servlets;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.ImagesServicePb.ImagesService;
import com.google.appengine.api.images.ServingUrlOptions;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.codeu.data.Datastore;
import com.google.codeu.data.Message;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -80,8 +88,24 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
String recipient = request.getParameter("recipient");
System.out.println("print recipient:");
System.out.println(recipient);

Message message = new Message(user, text, recipient);

String regex = "(https?://\\S+\\.(png|jpg))";
String replacement = "<img src=\"$1\" />";
String textWithImagesReplaced = text.replaceAll(regex, replacement);

Message message = new Message(user, textWithImagesReplaced, recipient);
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
List<BlobKey> blobKeys = blobs.get("image");

if(blobKeys != null && !blobKeys.isEmpty()) {
BlobKey blobKey = blobKeys.get(0);
com.google.appengine.api.images.ImagesService imagesService = ImagesServiceFactory.getImagesService();
ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey);
String imageUrl = imagesService.getServingUrl(options);
message.setImageUrl(imageUrl);
}

datastore.storeMessage(message);

response.sendRedirect("/user-page.html?user=" + recipient);
Expand Down
9 changes: 9 additions & 0 deletions src/main/webapp/css/user-page.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,13 @@
user is not logged in or viewing their own page. */
.hidden {
display: none;
}

/* Use this to ensure that the message is completely
displayed on one page */

.message-body img {
display: block;
margin-top: 25px;
max-width: 75%;
}
34 changes: 33 additions & 1 deletion src/main/webapp/js/user-page-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,33 @@ function showMessageFormIfLoggedIn() {
});
}

/**
* Shows the message form if the user is logged in and viewing their own page.
*/
function showMessageFormIfViewingSelf() {
fetch('/login-status')
.then((response) => {
return response.json();
})
.then((loginStatus) => {
if (loginStatus.isLoggedIn &&
loginStatus.username == parameterUsername) {
fetchImageUploadUrlAndShowForm();
}
});
}

function fetchImageUploadUrlAndShowForm() {
fetch('/image-upload-url')
.then((response) => {
return response.text();
})
.then((imageUploadUrl) => {
const messageForm = document.getElementById('message-form');
messageForm.action = imageUploadUrl;
messageForm.classList.remove('hidden');
});
}

/** Fetches messages and add them to the page. */
function fetchMessages() {
Expand Down Expand Up @@ -89,13 +116,18 @@ function buildMessageDiv(message) {
messageDiv.appendChild(headerDiv);
messageDiv.appendChild(bodyDiv);

if(message.imageUrl){
bodyDiv.innerHTML += '<br/>';
bodyDiv.innerHTML += '<img src="' + message.imageUrl + '" />';
}
return messageDiv;
}

/** Fetches data and populates the UI of the page. */
function buildUI() {
setPageTitle();
showMessageFormIfLoggedIn();
//showMessageFormIfLoggedIn();
showMessageFormIfViewingSelf()
fetchMessages();
fetchAboutMe();
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/webapp/user-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@
</nav>
<h1 id="page-title">User Page</h1>

<form id="message-form" action="/messages" method="POST" class="hidden">
<form id="message-form" action="/messages" method="POST" class="hidden" enctype="multipart/form-data">
Enter a new message:
<br/>
<textarea name="text" id="message-input"></textarea>
<br/>
Add an image to your message:
<input type="file" name="image">
<br/>
<input type="submit" value="Submit">
</form>
<hr/>
Expand Down