/** @param u User */ private static void handleAvatar(User u) { LOG.trace("handleAvatar"); String fileName = MD5.crypt(Integer.toString(u.getId())); FileItem item = (FileItem) JForumExecutionContext.getRequest().getObjectParameter("avatar"); UploadUtils uploadUtils = new UploadUtils(item); // Gets file extension String extension = uploadUtils.getExtension().toLowerCase(); int type = ImageUtils.IMAGE_UNKNOWN; if (extension.equals("jpg") || extension.equals("jpeg")) { type = ImageUtils.IMAGE_JPEG; } else if (extension.equals("gif") || extension.equals("png")) { type = ImageUtils.IMAGE_PNG; } if (type != ImageUtils.IMAGE_UNKNOWN) { String avatarTmpFileName = SystemGlobals.getApplicationPath() + "/images/avatar/" + fileName + "_tmp." + extension; // We cannot handle gifs if (extension.toLowerCase().equals("gif")) { extension = "png"; } String avatarFinalFileName = SystemGlobals.getApplicationPath() + "/images/avatar/" + fileName + "." + extension; uploadUtils.saveUploadedFile(avatarTmpFileName); // OK, time to check and process the avatar size int maxWidth = SystemGlobals.getIntValue(ConfigKeys.AVATAR_MAX_WIDTH); int maxHeight = SystemGlobals.getIntValue(ConfigKeys.AVATAR_MAX_HEIGHT); BufferedImage image = ImageUtils.resizeImage(avatarTmpFileName, type, maxWidth, maxHeight); ImageUtils.saveImage(image, avatarFinalFileName, type); u.setAvatar(fileName + "." + extension); // Delete the temporary file new File(avatarTmpFileName).delete(); } }
/** * Save image to avatar dir * * @param avatar * @param uploadedFile * @return */ private File saveImage(Avatar avatar, UploadedFile uploadedFile) { String configKey = getAvatarPathConfigKey(avatar); if (configKey != null && uploadedFile != null) { UploadUtils upload = new UploadUtils(uploadedFile); String imageName = String.format( "%s.%s", MD5.hash(uploadedFile.getFileName() + System.currentTimeMillis()), upload.getExtension()); String filePath = String.format( "%s/%s/%s", this.config.getApplicationPath(), this.config.getValue(configKey), imageName); upload.saveUploadedFile(filePath); return new File(filePath); } return null; }