/** * Get file extension corresponds to MIMEType. If MIMEType is empty or equals default MIMEType * empty string will be returned. If there is no file extension for specific MIMEType the empty * string will be returned also. In case when there are more than one extension for specific * MIMEType the first occurred extension in the list will be returned if MIMEType ends with this * extension otherwise just first occurred. * * @param mimeType MIMEType * @return file extension */ public String getExtension(String mimeType) { String extension = resolver.getExtension(mimeType); if (extension.length() == 0) { // use this resolver map (the same logic as in MimeTypeResolver) mimeType = mimeType.toLowerCase(); if (mimeType.isEmpty() || mimeType.equals(resolver.getDefaultMimeType())) { return ""; } List<String> values = extentions.get(mimeType); if (values == null) { return ""; } String resultExt = ""; for (String ext : values) { if (mimeType.endsWith(ext)) { return ext; } if (resultExt.isEmpty()) { resultExt = ext; } } extension = resultExt; } return extension; }
@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); } }