diff src/nabble/view/web/user/ChangeAvatar2.jtp @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children 18cf4872fd7f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/nabble/view/web/user/ChangeAvatar2.jtp	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,340 @@
+<%
+package nabble.view.web.user;
+
+import fschmidt.util.java.HtmlUtils;
+import fschmidt.util.java.ImageUtils;
+import fschmidt.util.servlet.JtpContext;
+import nabble.model.FileUpload;
+import nabble.model.Init;
+import nabble.model.Message;
+import nabble.model.ModelException;
+import nabble.model.User;
+import nabble.view.lib.Jtp;
+import nabble.view.lib.Shared;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileUploadException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.imageio.ImageIO;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.awt.image.BufferedImage;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.util.Map;
+import java.util.Random;
+
+
+public final class ChangeAvatar2 extends HttpServlet {
+
+	private static final Logger logger = LoggerFactory.getLogger(ChangeAvatar2.class);
+
+	private static final long SIZE_LIMIT = 4194304; // 4 Mb
+
+	private void handleError(HttpServletRequest request, String errorMessage, PrintWriter out) {
+		%>
+		<html>
+			<head>
+				<% Shared.loadJavascript(request, out); %>
+				<script type="text/javascript">
+					function say() {
+						alert("<%=HtmlUtils.javascriptStringEncode(errorMessage)%>");
+						location.replace("<%=request.getContextPath()%>/user/ChangeAvatar.jtp");
+					};
+				</script>
+			</head>
+			<body onload="say()"></body>
+		</html>
+		<%
+	}
+
+	protected void service(HttpServletRequest request, HttpServletResponse response)
+			throws ServletException, IOException {
+		JtpContext jtpContext = (JtpContext)getServletContext().getAttribute(JtpContext.attrName);
+		jtpContext.setTimeLimit(request,0L);
+		PrintWriter out = response.getWriter();
+		String context = request.getContextPath();
+		User user = Jtp.getUser(request, response);
+		if (user == null) {
+			Jtp.login("You must login to change your avatar.", request, response);
+			return;
+		}
+
+		String errorMessage = null;
+
+		String action = request.getParameter("action");
+		if ("crop".equals(action)) {
+			String imageName = request.getParameter("image");
+			int x = Jtp.getInt(request, "crop_x");
+			int y = Jtp.getInt(request, "crop_y");
+			int width = Jtp.getInt(request, "crop_width");
+			int height = Jtp.getInt(request, "crop_height");
+			try {
+				Message.Source srcTemp = Message.SourceType.getType('t').getSource(user.getSite(),user.getId());
+				InputStream in = FileUpload.getFileContent(srcTemp, imageName);
+				if (in == null) {
+					handleError(request, "Error uploading image. Please try again.", out);
+					return;
+				}				
+				BufferedImage originalImage = ImageIO.read(in);
+				in.close();
+				if (originalImage.getWidth() < 100 || originalImage.getHeight() < 100) {
+					handleError(request, "The width and height must be greater than 100 pixels.", out);
+					return;
+				}
+
+				BufferedImage croppedImage100 = ImageUtils.cropImage(originalImage, x, y, width, height);
+				croppedImage100 = ImageUtils.getThumbnail(croppedImage100, 100, 100);
+
+				BufferedImage croppedImage24 = ImageUtils.blurImage(croppedImage100);
+				croppedImage24 = ImageUtils.resizeImage(croppedImage24, 24, 24);
+
+				user.saveAvatar(croppedImage24,croppedImage100);
+				FileUpload.deleteFile(imageName, srcTemp);
+			} catch (ModelException e) {
+				logger.error(toString(), e);
+				errorMessage = e.getMessage();
+			}
+
+			if (errorMessage == null) {
+				response.sendRedirect("/user/ChangeAvatar2$ReloadAvatars.jtp");
+				return;
+			} else {
+				handleError(request, errorMessage, out);
+				return;
+			}
+		} else if ("delete".equals(action)) {
+			user.deleteAvatar();
+			response.sendRedirect("/user/ChangeAvatar2$ReloadAvatars.jtp");
+			return;
+		}
+
+		int imageWidth = 0;
+		int imageHeight = 0;
+		String fileName = "_user" + user.getId() + ".png";
+		try {
+			final Map<String, FileItem> map;
+			try {
+				map = Jtp.getFileItems(request);
+			} catch (FileUploadException e) {
+				logger.warn("",e);
+				throw ModelException.newInstance("upload_avatar_failed","upload failed - " + e.getMessage());
+			}
+			FileItem fi = map.get("image");
+
+			if (fi == null) {
+				handleError(request, "Image upload failed.", out);
+				return;
+			} else if (fi.getSize() > SIZE_LIMIT) {
+				handleError(request, "The file you uploaded is too big. Please upload a smaller image (less than 4Mb).", out);
+				return;
+			}
+
+			InputStream in = fi.getInputStream();
+			BufferedImage image;
+			try {
+				image = ImageIO.read(in);
+			} catch (javax.imageio.IIOException e) {
+				handleError(request, "The uploaded image is broken.", out);
+				return;
+			} finally {
+				in.close();
+			}
+			if (image == null) {
+				handleError(request, "Please upload the image again.", out);
+				return;
+			} else if (image.getWidth() < 100 || image.getHeight() < 100) {
+				handleError(request, "The width and height must be greater than 100 pixels.", out);
+				return;
+			} else if (image.getWidth() > 800 || image.getHeight() > 800) {
+				try {
+					image = ImageUtils.getThumbnail(image, 800, 800);
+				} catch (RuntimeException e) {
+					// This catch block will be improved later when I discover the exact line that throws
+					// a RuntimeException in ImageUtils [Hugo - April/2010]
+					if (fi.getSize() <= FileUpload.MAX_IMAGE_SIZE) {
+						if( Init.tempDir == null ) {
+							logger.info("Init.tempDir == null");
+						} else {
+							FileOutputStream fos = new FileOutputStream(Init.tempDir + FileUpload.getName(fi));
+							fos.write(fi.get());
+							fos.close();
+						}
+					}
+					handleError(request, "Unable to handle this image.", out);
+					return;
+				}
+			}
+
+			imageWidth = image.getWidth();
+			imageHeight = image.getHeight();
+
+			Message.Source tempSrc = Message.SourceType.getType('t').getSource(user.getSite(),user.getId());
+			FileUpload.saveImage(image, fileName, tempSrc);
+		} catch (ModelException e) {
+			errorMessage = e.getMessage();
+			logger.warn("image upload failed", e);
+		}
+
+		if (errorMessage != null) {
+			handleError(request, errorMessage, out);
+			return;
+		}
+		%>
+		<html>
+		<head>
+			<% Shared.title(request,response,"Change Avatar"); %>
+			<style type="text/css">
+				#imageContainer{
+					margin:15px;
+					left:0px;
+					top:0px;
+					position:relative;
+				}
+
+				.crop_transparentDiv{
+					background-color:#FFF;
+					filter:alpha(opacity=80);
+					-khtml-opacity: 0.8;
+					-moz-opacity: 0.8;
+					opacity:0.8;
+					position:absolute;
+				}
+				.crop_dottedDiv{
+					position:absolute;
+					border:1px dotted red;
+					z-index:10000;
+				}
+
+				.crop_dottedDiv div{
+					filter:alpha(opacity=0);
+					opacity:0;
+					-khtml-opacity: 0;
+					-moz-opacity: 0;
+					width:100%;
+					height:100%;
+					background-color:#FFF;
+				}
+			</style>
+			<script type="text/javascript">
+				var crop_script_alwaysPreserveAspectRatio = true; // Always preserve aspect ratio
+				var crop_script_fixedRatio = 1; // Width relative to height 2 = ratio 2:1
+
+				var cropToolBorderWidth = 1; // Width of dotted border around crop rectangle
+				var smallSquareWidth = 7; // Size of small squares used to resize crop rectangle
+
+				// Size of image shown in crop tool
+				var crop_imageWidth = <%=imageWidth%>;
+				var crop_imageHeight = <%=imageHeight%>;
+
+				// Size of original image
+				var crop_originalImageWidth = crop_imageWidth;
+				var crop_originalImageHeight = crop_imageHeight;
+
+				var crop_minimumPercent = 10; // Minimum percent - resize
+				var crop_maximumPercent = 100; // Maximum percent -resize
+
+				var crop_minimumWidthHeight = 100; // Minimum width and height of crop area
+
+				var updateFormValuesAsYouDrag = true; // This variable indicates if form values should be updated as we drag. This process could make the script work a little bit slow. That's why this option is set as a variable.
+				if(!document.all)updateFormValuesAsYouDrag = false; // Enable this feature only in IE
+			</script>
+			<script src="<%=context%>/util/image-crop.js" type="text/javascript"></script>
+		</head>
+		<body>
+		<%
+		Shared.minHeaderGlobal(request, response);
+		Shared.profileHeading(request,out,user,"Change Avatar - Crop Image");
+		%>
+		<b>Please select a square region on the image below.</b><br/>
+		You can drag the red handles to resize the square or drag the whole selected area.
+		<br/><br/>
+		<form action="/user/ChangeAvatar2.jtp" method="POST" accept-charset="UTF-8">
+			<input type="hidden" name="action" value="crop">
+			<input type="hidden" name="image" value="<%=fileName%>">
+			<input type="hidden" name="crop_x" id="input_crop_x">
+			<input type="hidden" name="crop_y" id="input_crop_y">
+			<input type="hidden" name="crop_width" id="input_crop_width">
+			<input type="hidden" name="crop_height" id="input_crop_height">
+			<input type="hidden" name="crop_percent_size" id="crop_percent_size">
+			<input type="submit" value="Crop Image">
+		</form>
+		<div class="crop_content">
+			<div id="imageContainer">
+				<%
+					// Here I create a random number that is appended to the end
+					// of the image URL. This prevents the browser from showing an
+					// old cached image.
+					Random random = new Random();
+					int any = random.nextInt(1000);
+				%>
+				<img src="/file/t<%=user.getId()%>/<%=fileName%>?<%=any%>"/>
+			</div>
+		</div>
+
+		<script type="text/javascript">
+			init_imageCrop('http://<%=request.getHeader("host")%>');
+
+			if (Nabble.isEmbedded) {
+				$(window).load(Nabble.resizeFrames);
+			}
+		</script>
+		
+		<% Shared.footer(request,response); %>
+		<% Shared.analytics(request,response); %>
+		</body>
+		</html>
+		<%
+	}
+
+	public static class ReloadAvatars extends HttpServlet {
+
+		protected void service(HttpServletRequest request, HttpServletResponse response)
+				throws ServletException, IOException
+		{
+			/*
+			This servlet is a tricky way to force the browser to reload the user avatars.
+			Since we don't specify cache headers for images, browsers cache them using
+			internal algorithms. The only way to update those images is by refreshing the page
+			either by pressing F5 or using location.reload(true).
+			*/
+			PrintWriter out = response.getWriter();
+			User user = Jtp.getUser(request, response);
+			if (user == null)
+				return;
+			Jtp.dontCache(response);
+			%>
+			<html>
+				<head>
+					<% Shared.loadJavascript(request, out); %>
+					<script type="text/javascript">
+						function init() {
+							var done = Nabble.getCookie('done');
+							if (done) {
+								Nabble.deleteCookie('done');
+								location = '/template/NamlServlet.jtp?macro=user_profile';
+							} else {
+								Nabble.setCookie('done','y');
+								location.reload(true);
+							}
+						};
+					</script>
+				</head>
+				<body onload="init()">
+					<div style="display:none">
+						<img src="<%=Shared.getAvatarImageURL(user, false)%>"/>
+						<img src="<%=Shared.getAvatarImageURL(user, true)%>"/>
+					</div>
+				</body>
+			</html>
+			<%
+		}
+
+	}
+}
+%>