/**
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * 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
 *
 *       http://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.example.managedvms.gettingstartedjava.util;

import com.google.gcloud.storage.Acl;
import com.google.gcloud.storage.Acl.Role;
import com.google.gcloud.storage.Acl.User;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

// [START example]
public class CloudStorageHelper {

  private final Logger logger =
      Logger.getLogger(
         com.example.managedvms.gettingstartedjava.util.CloudStorageHelper.class.getName());
  private static Storage storage = null;

  static {
    storage = StorageOptions.defaultInstance().service();
  }

  /**
   * Uploads a file to Google Cloud Storage to the bucket specified in the BUCKET_NAME
   * environment variable, appending a timestamp to end of the uploaded filename.
   */
  public String uploadFile(Part filePart, final String bucketName) throws IOException {
    DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
    DateTime dt = DateTime.now(DateTimeZone.UTC);
    String dtString = dt.toString(dtf);
    final String fileName = filePart.getSubmittedFileName() + dtString;

    // the inputstream is closed by default, so we don't need to close it here
    BlobInfo blobInfo =
        storage.create(
            BlobInfo
                .builder(bucketName, fileName)
                // Modify access list to allow all users with link to read file
                .acl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
                .build(),
            filePart.getInputStream());
    logger.log(
        Level.INFO, "Uploaded file {0} as {1}", new Object[]{
            filePart.getSubmittedFileName(), fileName});
    // return the public download link
    return blobInfo.mediaLink();
  }

  /**
   * Extracts the file payload from an HttpServletRequest, checks that the file extension
   * is supported and uploads the file to Google Cloud Storage.
   */
  public String getImageUrl(HttpServletRequest req, HttpServletResponse resp,
                            final String bucket) throws IOException, ServletException {
    Part filePart = req.getPart("file");
    final String fileName = filePart.getSubmittedFileName();
    String imageUrl = req.getParameter("imageUrl");
    // Check extension of file
    if (fileName != null && !fileName.isEmpty() && fileName.contains(".")) {
      final String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
      String[] allowedExt = { "jpg", "jpeg", "png", "gif" };
      for (String s : allowedExt) {
        if (extension.equals(s)) {
          return this.uploadFile(filePart, bucket);
        }
      }
      throw new ServletException("file must be an image");
    }
    return imageUrl;
  }
}
// [END example]

@@ -1482,22 +1482,16 @@
 logger =
-%0A     
  Logger.
@@ -1504,65 +1504,8 @@
 ger(
-%0A         com.example.managedvms.gettingstartedjava.util.
 Clou
@@ -1579,16 +1579,34 @@
  null;%0A%0A
+  // %5BSTART init%5D%0A
   static
@@ -1662,32 +1662,72 @@
 .service();%0A  %7D%0A
+  // %5BEND init%5D%0A%0A  // %5BSTART uploadFile%5D
 %0A  /**%0A   * Uplo
@@ -2868,16 +2868,63 @@
 ();%0A  %7D%0A
+  // %5BEND uploadFile%5D%0A%0A  // %5BSTART getImageUrl%5D
 %0A  /**%0A 
@@ -3884,16 +3884,39 @@
 rl;%0A  %7D%0A
+  // %5BEND getImageUrl%5D%0A
 %7D%0A// %5BEN

add decorators