// Copyright 2019 Google LLC
//
// 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 com.google.sps.data;

import javax.servlet.http.HttpServletRequest;

/** Servlets Utility Class. */
public final class HttpServletUtils {
  private HttpServletUtils() {
    throw new java.lang.UnsupportedOperationException("Utility class cannot be instantiated");
  }

  /**
   * Return the request parameter or the default value if the parameter
   * was not specified by the client
   */
  public static String getParameter(HttpServletRequest request, String name, String defaultValue) {
    if (request == null) {
      return defaultValue;
    }
    
    String value = request.getParameter(name);
    if (value == null || value.isEmpty()) {
      return defaultValue;
    }
    return value;
  }
}
@@ -612,16 +612,585 @@
 .data;%0A%0A
+import com.google.appengine.api.blobstore.BlobInfo;%0Aimport com.google.appengine.api.blobstore.BlobInfoFactory;%0Aimport com.google.appengine.api.blobstore.BlobKey;%0Aimport com.google.appengine.api.blobstore.BlobstoreService;%0Aimport com.google.appengine.api.blobstore.BlobstoreServiceFactory;%0Aimport com.google.appengine.api.datastore.DatastoreService;%0Aimport com.google.appengine.api.datastore.DatastoreServiceFactory;%0Aimport com.google.appengine.api.datastore.EmbeddedEntity;%0Aimport com.google.appengine.api.datastore.Entity;%0Aimport java.util.List;%0Aimport java.util.Map;%0A
 import j
@@ -1859,10 +1859,1247 @@
 lue;%0A  %7D
+%0A%0A  /** Returns a BlobKey for the uploaded file, or null if the user didn't upload a file. */%0A  public static BlobKey getUploadedFileBlobKey(HttpServletRequest request, String formInput) %7B%0A    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();%0A    Map%3CString, List%3CBlobKey%3E%3E blobs = blobstoreService.getUploads(request);%0A    List%3CBlobKey%3E blobKeys = blobs.get(formInput);%0A%0A    // User submitted form without selecting a file (dev server)%0A    if (blobKeys == null %7C%7C blobKeys.isEmpty()) %7B%0A      return null;%0A    %7D%0A%0A    // Our form only contains a single file input, so get the first index.%0A    BlobKey blobKey = blobKeys.get(0);%0A%0A    // User submitted form without selecting a file (live server)%0A    BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey);%0A    if (blobInfo == null %7C%7C blobInfo.getSize() == 0) %7B%0A      blobstoreService.delete(blobKey);%0A      return null;%0A    %7D%0A%0A    // Validate the file is an image file (.png, .jpg, .jpeg, .jfif, .pjpeg, .pjp)%0A    String blobMimeType = blobInfo.getContentType();%0A    if (blobMimeType.equals(%22image/png%22) %7C%7C blobMimeType.equals(%22image/jpeg%22)) %7B%0A      return blobKey;%0A    %7D else %7B%0A      blobstoreService.delete(blobKey);%0A      return null;%0A    %7D%0A  %7D
 %0A%7D

Add GetUploadedFile to Utils