Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
82 changes: 22 additions & 60 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions client/src/css/userPage.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@
.message-body {
margin: 5px;
}

.message-body img {
display: block;
margin-top: 25px;
max-width: 75%;
}
3 changes: 3 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@WebServlet("/blobstore-upload-url")
public class BlobstoreUploadUrlServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String uploadUrl = blobstoreService.createUploadUrl("/my-form-handler");

response.setContentType("text/html");
response.getOutputStream().println(uploadUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
@WebServlet("/my-form-handler")
public class FormHandlerServlet extends HttpServlet {

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

// Get the message entered by the user.
String message = request.getParameter("message");

// Get the URL of the image that the user uploaded to Blobstore.
String imageUrl = getUploadedFileUrl(request, "image");

// Output some HTML that shows the data the user entered.
// A real codebase would probably store these in Datastore.
ServletOutputStream out = response.getOutputStream();
out.println("<p>Here's the image you uploaded:</p>");
out.println("<a href=\"" + imageUrl + "\">");
out.println("<img src=\"" + imageUrl + "\" />");
out.println("</a>");
out.println("<p>Here's the text you entered:</p>");
out.println(message);
}

/**
* Returns a URL that points to the uploaded file, or null if the user didn't
* upload a file.
*/
private String getUploadedFileUrl(HttpServletRequest request, String formInputElementName) {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
List<BlobKey> blobKeys = blobs.get("image");

// User submitted form without selecting a file, so we can't get a URL.
// (devserver)
if (blobKeys == null || blobKeys.isEmpty()) {
return null;
}

// Our form only contains a single file input, so get the first index.
BlobKey blobKey = blobKeys.get(0);

// User submitted form without selecting a file, so we can't get a URL. (live
// server)
BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);
if (blobInfo.getSize() == 0) {
blobstoreService.delete(blobKey);
return null;
}

// We could check the validity of the file here, e.g. to make sure it's an image
// file
// https://stackoverflow.com/q/10779564/873165

// Use ImagesService to get a URL that points to the uploaded file.
ImagesService imagesService = ImagesServiceFactory.getImagesService();
ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey);
return imagesService.getServingUrl(options);
}
}
Loading