@Override public void execute(Event<UIAvatarUploader> event) throws Exception { WebuiRequestContext ctx = event.getRequestContext(); UIApplication uiApplication = ctx.getUIApplication(); UIAvatarUploader uiAvatarUploader = event.getSource(); UIFormUploadInput uiAvatarUploadInput = uiAvatarUploader.getChild(UIFormUploadInput.class); UIPopupWindow uiPopup = uiAvatarUploader.getParent(); InputStream uploadedStream = uiAvatarUploadInput.getUploadDataAsStream(); if (uploadedStream == null) { uiApplication.addMessage( new ApplicationMessage(MSG_IMG_NOT_UPLOADED, null, ApplicationMessage.ERROR)); ctx.addUIComponentToUpdateByAjax(uiAvatarUploader); return; } UploadResource uploadResource = uiAvatarUploadInput.getUploadResource(); String mimeType = uploadResource.getMimeType(); String uploadId = uiAvatarUploadInput.getUploadId(); if (!uiAvatarUploader.isAcceptedMimeType(mimeType)) { UploadService uploadService = (UploadService) PortalContainer.getComponent(UploadService.class); uploadService.removeUploadResource(uploadId); uiApplication.addMessage( new ApplicationMessage(MSG_MIMETYPE_NOT_ACCEPTED, null, ApplicationMessage.ERROR)); ctx.addUIComponentToUpdateByAjax(uiAvatarUploader); } else { MimeTypeResolver mimeTypeResolver = new MimeTypeResolver(); String fileName = uploadResource.getFileName(); // @since 1.1.3 String extension = mimeTypeResolver.getExtension(mimeType); if ("".equals(extension)) { mimeType = uiAvatarUploader.getStandardMimeType(mimeType); } // Resize avatar to fixed width if can't(avatarAttachment == null) keep // origin avatar AvatarAttachment avatarAttachment = ImageUtils.createResizedAvatarAttachment( uploadedStream, WIDTH, 0, null, fileName, mimeType, null); if (avatarAttachment == null) { avatarAttachment = new AvatarAttachment( null, fileName, mimeType, uploadedStream, null, System.currentTimeMillis()); } UploadService uploadService = (UploadService) PortalContainer.getComponent(UploadService.class); uploadService.removeUploadResource(uploadId); UIAvatarUploadContent uiAvatarUploadContent = uiAvatarUploader.createUIComponent(UIAvatarUploadContent.class, null, null); uiAvatarUploadContent.setAvatarAttachment(avatarAttachment); uiPopup.setUIComponent(uiAvatarUploadContent); ctx.addUIComponentToUpdateByAjax(uiPopup); } }
@Override public void execute(Event<UIAvatarUploader> event) throws Exception { UIAvatarUploader uiAvatarUploader = event.getSource(); UIPopupWindow uiPopup = uiAvatarUploader.getParent(); uiPopup.setShow(false); }