protected FileEntry addTempImageFileEntry(PortletRequest portletRequest) throws Exception { UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(portletRequest); ThemeDisplay themeDisplay = (ThemeDisplay) portletRequest.getAttribute(WebKeys.THEME_DISPLAY); String contentType = uploadPortletRequest.getContentType("fileName"); if (!MimeTypesUtil.isWebImage(contentType)) { throw new ImageTypeException(); } String fileName = uploadPortletRequest.getFileName("fileName"); try { TempFileEntryUtil.deleteTempFileEntry( themeDisplay.getScopeGroupId(), themeDisplay.getUserId(), getTempImageFolderName(), fileName); } catch (Exception e) { } InputStream inputStream = null; try { inputStream = uploadPortletRequest.getFileAsStream("fileName"); return TempFileEntryUtil.addTempFileEntry( themeDisplay.getScopeGroupId(), themeDisplay.getUserId(), getTempImageFolderName(), fileName, inputStream, contentType); } finally { StreamUtil.cleanUp(inputStream); } }
protected FileEntry saveTempImageFileEntry(ActionRequest actionRequest) throws Exception { FileEntry tempFileEntry = null; InputStream tempImageStream = null; try { tempFileEntry = getTempImageFileEntry(actionRequest); tempImageStream = tempFileEntry.getContentStream(); ImageBag imageBag = ImageToolUtil.read(tempImageStream); RenderedImage renderedImage = imageBag.getRenderedImage(); String cropRegionJSON = ParamUtil.getString(actionRequest, "cropRegion"); if (Validator.isNotNull(cropRegionJSON)) { JSONObject jsonObject = JSONFactoryUtil.createJSONObject(cropRegionJSON); int height = jsonObject.getInt("height"); int width = jsonObject.getInt("width"); int x = jsonObject.getInt("x"); int y = jsonObject.getInt("y"); if ((x == 0) && (y == 0) && (renderedImage.getHeight() == height) && (renderedImage.getWidth() == width)) { return tempFileEntry; } if ((height + y) > renderedImage.getHeight()) { height = renderedImage.getHeight() - y; } if ((width + x) > renderedImage.getWidth()) { width = renderedImage.getWidth() - x; } renderedImage = ImageToolUtil.crop(renderedImage, height, width, x, y); } byte[] bytes = ImageToolUtil.getBytes(renderedImage, imageBag.getType()); ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY); File file = FileUtil.createTempFile(bytes); try { TempFileEntryUtil.deleteTempFileEntry( themeDisplay.getScopeGroupId(), themeDisplay.getUserId(), getTempImageFolderName(), getTempImageFileName(actionRequest)); } catch (Exception e) { } return TempFileEntryUtil.addTempFileEntry( themeDisplay.getScopeGroupId(), themeDisplay.getUserId(), getTempImageFolderName(), getTempImageFileName(actionRequest), file, tempFileEntry.getMimeType()); } catch (NoSuchFileEntryException nsfee) { throw new UploadException(nsfee); } catch (NoSuchRepositoryException nsre) { throw new UploadException(nsre); } finally { StreamUtil.cleanUp(tempImageStream); } }