// Path: src/main/java/com/sketchy/image/SourceImageAttributes.java
// public class SourceImageAttributes extends ImageAttributes{
// 	// same as the Image Attributes (for now)
// }
// 
// Path: src/main/java/com/sketchy/server/JSONServletResult.java
// public enum Status{
// 	SUCCESS, ERROR;
// }
// 
// Path: src/main/java/com/sketchy/utils/ImageUtils.java
// public class ImageUtils{
// 	
// 	public static BufferedImage loadImage(InputStream inputStream) throws Exception {
// 		BufferedImage image = ImageIO.read(inputStream);
// 		return image;
// 	}
// 	
// 	public static BufferedImage loadImage(File imageFile) throws Exception {
// 		BufferedImage image = ImageIO.read(imageFile);
// 		return image;
// 	}
// 	
// 	public static void saveImage(File imageFile, BufferedImage image) throws Exception {
// 		ImageIO.write(image, "png", imageFile);
// 	}
// 	
// }


import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import com.sketchy.image.SourceImageAttributes;
import com.sketchy.server.JSONServletResult.Status;
import com.sketchy.utils.ImageUtils;
		    		if (StringUtils.isNotBlank(uploadFileName)){
		    			// Don't allow \\ in the filename, assume it's a directory separator and convert to "/"
		    			// and take the filename after the last "/"
		    			// This will fix the issue of Jetty not reading and serving files
		    			// with "\" (%5C) characters 
		    			// This also fixes the issue of IE sometimes sending the whole path 
		    			// (depending on the security settings)
		    			uploadFileName = StringUtils.replaceChars(uploadFileName, "\\", "/");
		    			if (StringUtils.contains(uploadFileName, "/")){
		    				uploadFileName = StringUtils.substringAfterLast(uploadFileName, "/");
		    			}
		    			
			    		File uploadFile = HttpServer.getUploadFile(uploadFileName);
			    		// make sure filename is actually in the upload directory
			    		// we don't want any funny games
			    		if (!uploadFile.getParentFile().equals(HttpServer.IMAGE_UPLOAD_DIRECTORY)){
			    			throw new RuntimeException("Can not upload File. Invalid directory!");
			    		}

			    		// if saved ok, then need to add the data file
			    		SourceImageAttributes sourceImageAttributes = new SourceImageAttributes();
			    		sourceImageAttributes.setImageName(uploadFileName);

			    		File pngFile = HttpServer.getUploadFile(sourceImageAttributes.getImageFilename());
			    		if (pngFile.exists()) {
			    			throw new Exception("Can not Upload file. File '" + uploadFileName +"' already exists!");
			    		}
			    		File dataFile = HttpServer.getUploadFile(sourceImageAttributes.getDataFilename());

			    		// Convert the image to a .PNG file
// Path: src/main/java/com/sketchy/image/SourceImageAttributes.java
// public class SourceImageAttributes extends ImageAttributes{
// 	// same as the Image Attributes (for now)
// }
// 
// Path: src/main/java/com/sketchy/server/JSONServletResult.java
// public enum Status{
// 	SUCCESS, ERROR;
// }
// 
// Path: src/main/java/com/sketchy/utils/ImageUtils.java
// public class ImageUtils{
// 	
// 	public static BufferedImage loadImage(InputStream inputStream) throws Exception {
// 		BufferedImage image = ImageIO.read(inputStream);
// 		return image;
// 	}
// 	
// 	public static BufferedImage loadImage(File imageFile) throws Exception {
// 		BufferedImage image = ImageIO.read(imageFile);
// 		return image;
// 	}
// 	
// 	public static void saveImage(File imageFile, BufferedImage image) throws Exception {
// 		ImageIO.write(image, "png", imageFile);
// 	}
// 	
// }


// Path: src/main/java/com/sketchy/server/ImageUploadServlet.java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import com.sketchy.image.SourceImageAttributes;
import com.sketchy.server.JSONServletResult.Status;
import com.sketchy.utils.ImageUtils;

		    		if (StringUtils.isNotBlank(uploadFileName)){
		    			// Don't allow \\ in the filename, assume it's a directory separator and convert to "/"
		    			// and take the filename after the last "/"
		    			// This will fix the issue of Jetty not reading and serving files
		    			// with "\" (%5C) characters 
		    			// This also fixes the issue of IE sometimes sending the whole path 
		    			// (depending on the security settings)
		    			uploadFileName = StringUtils.replaceChars(uploadFileName, "\\", "/");
		    			if (StringUtils.contains(uploadFileName, "/")){
		    				uploadFileName = StringUtils.substringAfterLast(uploadFileName, "/");
		    			}
		    			
			    		File uploadFile = HttpServer.getUploadFile(uploadFileName);
			    		// make sure filename is actually in the upload directory
			    		// we don't want any funny games
			    		if (!uploadFile.getParentFile().equals(HttpServer.IMAGE_UPLOAD_DIRECTORY)){
			    			throw new RuntimeException("Can not upload File. Invalid directory!");
			    		}

			    		// if saved ok, then need to add the data file
			    		SourceImageAttributes sourceImageAttributes = new SourceImageAttributes();
			    		sourceImageAttributes.setImageName(uploadFileName);

			    		File pngFile = HttpServer.getUploadFile(sourceImageAttributes.getImageFilename());
			    		if (pngFile.exists()) {
			    			throw new Exception("Can not Upload file. File '" + uploadFileName +"' already exists!");
			    		}
			    		File dataFile = HttpServer.getUploadFile(sourceImageAttributes.getDataFilename());

			    		// Convert the image to a .PNG file
			    		BufferedImage image=ImageUtils.loadImage(fileItem.getInputStream());