package foodtruck.server;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.tools.cloudstorage.GcsFileMetadata;
import com.google.appengine.tools.cloudstorage.GcsFilename;
import com.google.appengine.tools.cloudstorage.GcsInputChannel;
import com.google.appengine.tools.cloudstorage.GcsService;
import com.google.common.io.ByteStreams;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;

import foodtruck.util.HttpHeaderFormat;

/**
 * @author aviolette
 * @since 12/29/14
 */
@Singleton
public class ImageServlet extends HttpServlet {
  private static final Logger log = Logger.getLogger(ImageServlet.class.getName());
  private final GcsService cloudStorage;
  private final DateTimeFormatter dateFormatter;

  @Inject
  public ImageServlet(GcsService cloudStorage, @HttpHeaderFormat DateTimeFormatter formatter) {
    this.cloudStorage = cloudStorage;
    this.dateFormatter = formatter;
  }

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    GcsFilename fileName = getFileName(req);
    try {
      GcsInputChannel readChannel = cloudStorage.openPrefetchingReadChannel(fileName, 0, 2097152);
      InputStream in = Channels.newInputStream(readChannel);
      try {
        ByteStreams.copy(Channels.newInputStream(readChannel), resp.getOutputStream());
      } finally {
        in.close();
      }
      GcsFileMetadata metaData = cloudStorage.getMetadata(fileName);
      resp.setHeader("ETag", metaData.getEtag());
      resp.setHeader("Cache-Control", "no-transform,public,max-age=300,s-max-age=900");
      resp.setHeader("Last-Modified", dateFormatter.print(new DateTime(metaData.getLastModified().getTime())));
      resp.setContentType(metaData.getOptions().getMimeType());
    } catch (FileNotFoundException fnfe) {
      log.log(Level.FINE, fnfe.getMessage());
      resp.sendError(404, "Not found: " + req.getRequestURI());
    } catch (Exception e) {
      log.log(Level.WARNING, e.getMessage(), e);
      resp.sendError(500, e.getMessage());
    }
  }

  private GcsFilename getFileName(HttpServletRequest req) {
    String[] splits = req.getRequestURI().split("/");
    return new GcsFilename("truckicons", splits[3]);
  }
}

@@ -820,24 +820,63 @@
 Formatter;%0A%0A
+import foodtruck.dao.ConfigurationDAO;%0A
 import foodt
@@ -1184,16 +1184,60 @@
 rmatter;
+%0A  private final ConfigurationDAO configDAO;
 %0A%0A  @Inj
@@ -1332,16 +1332,44 @@
 ormatter
+, ConfigurationDAO configDAO
 ) %7B%0A    
@@ -1438,16 +1438,48 @@
 matter;%0A
+    this.configDAO = configDAO;%0A
   %7D%0A%0A  @
@@ -2772,20 +2772,46 @@
 ame(
-%22t
+configDAO.find().getT
 ruck
-i
+I
 cons
-%22
+Bucket()
 , sp

Use configured gcs bucket for image serving