package com.casamind.adware.server.servlet;
import java.io.IOException;
import java.util.Map;
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.casamind.adware.server.utils.ServletHelper;
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.ImagesServiceFactory;
@SuppressWarnings("serial")
public class UploadServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(UploadServlet.class.getName());
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobKey = blobs.get("imageUploader");
log.info("BlobKey: " + blobKey);
if (blobKey != null) {
String imageUrl = ImagesServiceFactory.getImagesService().getServingUrl(blobKey);
String keyString = blobKey.getKeyString();
if (ServletHelper.isDevMode()) {
imageUrl = imageUrl.replace("http://0.0.0.0:", "http://127.0.0.1:");
}
res.sendRedirect("/adware/uploadService?url=" + imageUrl + "&key=" + keyString);
// res.sendRedirect("/upload?uploadedImageKey=" + keyString);
// res.getWriter().println("/upload?uploadedImageKey=" + keyString);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String url = req.getParameter("url");
String key = req.getParameter("key");
String output = key + "\t" + url;
resp.setHeader("Content-Type", "text/html");
// This is a bit hacky, but it'll work. We'll use this URL to fetch the
// image by URL
resp.getWriter().println(output);
}
}