Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.
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
2 changes: 1 addition & 1 deletion src/main/java/shef/data/Ingredient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Ingredient {
public Ingredient() {

}

public Ingredient(EmbeddedEntity entity) {

}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/shef/data/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public class Recipe {
private String key;
private String name;
private String description;
private double time;
private double servings;
private String imageKey;
private Set<String> tags;
private Set<String> ingredients;
private Set<String> equipment;
private List<Step> steps;
private Set<SpinOff> spinOffs;
private long timestamp;
Expand Down Expand Up @@ -66,8 +70,12 @@ public Recipe(String name, String description, Set<String> tags, Set<String> ing
public Recipe(Entity recipeEntity) {
this.name = (String) recipeEntity.getProperty("name");
this.description = (String) recipeEntity.getProperty("description");
this.time = (double) recipeEntity.getProperty("time");
this.servings = (double) recipeEntity.getProperty("servings");
this.imageKey = (String) recipeEntity.getProperty("imageKey");
this.tags = getTagsFromEntity((Collection<EmbeddedEntity>) recipeEntity.getProperty("tags"));
this.ingredients = getIngredientsFromEntity((Collection<EmbeddedEntity>) recipeEntity.getProperty("ingredients"));
this.equipment = getEquipmentFromEntity((Collection<EmbeddedEntity>) recipeEntity.getProperty("equipment"));
this.steps = getStepsFromEntity((Collection<EmbeddedEntity>) recipeEntity.getProperty("steps"));
this.timestamp = (long) recipeEntity.getProperty("timestamp");
}
Expand Down Expand Up @@ -252,6 +260,15 @@ private Set<String> getIngredientsFromEntity(Collection<EmbeddedEntity> entityIn
return ingredientsSet;
}

/** Returns the equipment of an EmbeddedEntity as a Set. */
private Set<String> getEquipmentFromEntity(Collection<EmbeddedEntity> entityEquipment) {
Set<String> equipmentSet = new HashSet<>();
for (EmbeddedEntity equipment : entityEquipment) {
equipmentSet.add((String) equipment.getProperty("equipment"));
}
return equipmentSet;
}

/** Returns the steps of an EmbeddedEntity as a List. */
private List<Step> getStepsFromEntity(Collection<EmbeddedEntity> entitySteps) {
List<Step> stepsList = new LinkedList<>();
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/shef/servlets/BlobServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
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.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/** Servlet that serves blobstore files. */
@WebServlet("/blob")
Expand All @@ -33,4 +37,30 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
BlobKey blobKey = new BlobKey(request.getParameter("blob-key"));
blobstoreService.serve(blobKey, response);
}

/*
* Returns the String representation of the BlobKey of the uploaded file, or null if the user didn't upload a file.
* Used in alternative to getting the blob's direct URL, which causes crashes on the live server.
*/
public static String getUploadedFileBlobKey(HttpServletRequest request, String formInputElementName) {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
List<BlobKey> blobKeys = blobs.get(formInputElementName);

// User submitted form without selecting a file, so we can't get a URL. (dev server)
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;
}
return blobKey.getKeyString();
}
}
9 changes: 9 additions & 0 deletions src/main/java/shef/servlets/NewRecipeServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class NewRecipeServlet extends HttpServlet {
private DatastoreService datastore;
private final String TAG = "tag";
private final String INGREDIENT = "ingredient";
private final String EQUIPMENT = "equipment";
private final String STEP = "step";

@Override
Expand Down Expand Up @@ -71,9 +72,13 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
Collection<String> searchStrings = new HashSet<>();
String name = request.getParameter("name");
searchStrings.add(name.toUpperCase());
double time = Double.parseDouble(request.getParameter("time"));
double servings = Double.parseDouble(request.getParameter("servings"));
String imageKey = BlobServlet.getUploadedFileBlobKey(request, "image");
String description = request.getParameter("description");
Collection<EmbeddedEntity> tags = getParameters(request, TAG, searchStrings);
Collection<EmbeddedEntity> ingredients = getParameters(request, INGREDIENT, searchStrings);
Collection<EmbeddedEntity> equipment = getParameters(request, EQUIPMENT, null);
Collection<EmbeddedEntity> steps = getParameters(request, STEP, null);
int likes;
try {
Expand All @@ -85,9 +90,13 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr

Entity recipe = new Entity("Recipe");
recipe.setProperty("name", name);
recipe.setProperty("time", time);
recipe.setProperty("servings", servings);
recipe.setProperty("imageKey", imageKey);
recipe.setProperty("description", description);
recipe.setProperty("tags", tags);
recipe.setProperty("ingredients", ingredients);
recipe.setProperty("equipment", equipment);
recipe.setProperty("steps", steps);
recipe.setProperty("search-strings", new ArrayList<String>(searchStrings));
recipe.setProperty("timestamp", timestamp);
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/shef/servlets/RecipeImageUploadUrlServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package shef.servlets;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
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.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.ServingUrlOptions;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.List;

/** Gets the Blobstore URL to send the recipe creation form to. */
@WebServlet("/recipe-image-upload-url")
public class RecipeImageUploadUrlServlet extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String uploadUrl = blobstoreService.createUploadUrl("/new-recipe");
response.setContentType("text/html");
response.getWriter().println(uploadUrl);
}
}
60 changes: 0 additions & 60 deletions src/main/java/shef/servlets/TestDisplayRecipesServlet.java

This file was deleted.

26 changes: 1 addition & 25 deletions src/main/java/shef/servlets/UserServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
// Get properties that don't come from the request.
String id = userService.getCurrentUser().getUserId();
String email = userService.getCurrentUser().getEmail();
String profilePicKey = getUploadedFileBlobKey(request, "profile-pic");
String profilePicKey = BlobServlet.getUploadedFileBlobKey(request, "profile-pic");

// Get properties from the request.
Map<String, String[]> parameterMap = request.getParameterMap();
Expand Down Expand Up @@ -130,28 +130,4 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) thr
datastore.put(finalUser);
response.sendRedirect(redirectUrl);
}

// Returns the String representation of the BlobKey of the uploaded file, or null if the user didn't upload a file.
private String getUploadedFileBlobKey(HttpServletRequest request, String formInputElementName) {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
List<BlobKey> blobKeys = blobs.get(formInputElementName);

// User submitted form without selecting a file, so we can't get a URL. (dev server)
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;
}

return blobKey.getKeyString();
}
}
20 changes: 12 additions & 8 deletions src/main/webapp/create-recipe-blank.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<title>shef</title>
</head>

<body class="page-container" onload="protectPage(); navBarSetup();">
<body class="page-container" onload="protectPage(); navBarSetup(); getRecipeImageUploadUrl();">
<template>
<span>
<svg class="bi bi-plus-circle bootstrap-icon" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
Expand Down Expand Up @@ -72,7 +72,7 @@ <h1 class="display-4">So you want to create a recipe?</h1>
<div class="med-sep">
<div class="row">
<div class="col-sm-6 col-md-6 col-lg-6 text-left">
<form action="/new-recipe" method="POST">
<form id="form" method="POST" enctype="multipart/form-data">
<h2>Basic Info</h2>
<div class="form-group small-sep">
<label for="dishNameInput">1. What’s your dish called? *</label>
Expand All @@ -85,10 +85,14 @@ <h2>Basic Info</h2>
<div class="form-group small-sep">
<label for="servingsInput">3. How many servings does it make? *</label>
<input type="number" name="servings" class="form-control" id="servingsInput" placeholder="...">
</div>
</div>
<div class="form-group small-sep">
<label for="image">4. Upload an image of your dish!</label><br>
<input type="file" name="image" id="image">
</div>
<div class="form-group small-sep">
<label>4. What tags apply to this recipe?</label>
<div id="Tags">
<label>5. What tags apply to this recipe?</label>
<div id="Tags">
<parameter-input name="Tag" index="0" id="Tag0"></parameter-input>
</div>
</div>
Expand All @@ -102,7 +106,7 @@ <h2>Description</h2>
<div class="med-sep">
<h2>Ingredients</h2>
<div class="form-group small-sep" id="Ingredients">
<parameter-input name="Ingredient" index="0" id="Ingredient0"></parameter-input>
<ingredient-input name="Ingredient" index="0"></ingredient-input>
</div>
</div>
<div class="med-sep">
Expand Down Expand Up @@ -138,7 +142,7 @@ <h2>Permissions</h2>
<h2>Sharing</h2>
<div class="form-group">
<label for="permissionSelect">Spread the joy!</label>
<select class="form-control" id="permissionSelect">
<select class="form-control" id="groupSelect">
<option>Reciplease</option>
<option>Cal Badminton</option>
<option>Some Group</option>
Expand All @@ -150,7 +154,7 @@ <h2>Sharing</h2>
</div>
<div class="col-sm-6 col-md-6 col-lg-6 text-center">
<figure class="figure">
<img src="assets/images/bdd_jjg.jpeg" class="figure-img img-fluid rounded" alt="...">
<img id="recipe-image" src="assets/images/bdd_jjg.jpeg" class="figure-img img-fluid rounded" alt="...">
<figcaption class="figure-caption">The cover photo for your recipe - choose wisely.</figcaption>
</figure>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -11484,7 +11484,7 @@ p {
font-size: 1.25rem;
}

parameter-input {
parameter-input, ingredient-input {
display: inline-block;
vertical-align: middle;
margin: 0 0 15px 0;
Expand Down
Loading