Search in sources :

Example 1 with CroppedImage

use of org.primefaces.model.CroppedImage in project primefaces by primefaces.

the class ImageCropperRenderer method getConvertedValue.

@Override
public Object getConvertedValue(FacesContext context, UIComponent component, Object submittedValue) throws ConverterException {
    String coords = (String) submittedValue;
    if (isValueBlank(coords)) {
        return null;
    }
    String[] cropCoords = coords.split("_");
    int x = (int) Double.parseDouble(cropCoords[0]);
    int y = (int) Double.parseDouble(cropCoords[1]);
    int w = (int) Double.parseDouble(cropCoords[2]);
    int h = (int) Double.parseDouble(cropCoords[3]);
    if (w <= 0 || h <= 0) {
        return null;
    }
    ImageCropper cropper = (ImageCropper) component;
    Resource resource = getImageResource(context, cropper);
    InputStream inputStream = null;
    Object imageObject = cropper.getImage();
    String imagePath = null;
    StreamedContent stream = null;
    if (imageObject instanceof String) {
        imagePath = imageObject.toString();
    } else if (imageObject instanceof StreamedContent) {
        stream = (StreamedContent) imageObject;
    } else {
        throw new IllegalArgumentException("'image' must be either an String relative path or a StreamedObject.");
    }
    String contentType = null;
    try {
        if (resource != null && !"RES_NOT_FOUND".equals(resource.toString())) {
            inputStream = resource.getInputStream();
            contentType = resource.getContentType();
        } else {
            if (imagePath != null) {
                boolean isExternal = imagePath.startsWith("http");
                if (isExternal) {
                    URL url = new URL(imagePath);
                    URLConnection urlConnection = url.openConnection();
                    inputStream = urlConnection.getInputStream();
                    contentType = urlConnection.getContentType();
                } else {
                    ExternalContext externalContext = context.getExternalContext();
                    // GitHub #3268 OWASP Path Traversal
                    imagePath = FileUploadUtils.checkPathTraversal(imagePath);
                    String webRoot = externalContext.getRealPath(Constants.EMPTY_STRING);
                    String fileSeparator = Constants.EMPTY_STRING;
                    if (!(webRoot.endsWith("\\") || webRoot.endsWith("/")) && !(imagePath.startsWith("\\") || imagePath.startsWith("/"))) {
                        fileSeparator = "/";
                    }
                    File file = new File(webRoot + fileSeparator + imagePath);
                    inputStream = new FileInputStream(file);
                }
            } else if (stream != null) {
                inputStream = stream.getStream().get();
                contentType = stream.getContentType();
            }
        }
        // wrap input stream by BoundedInputStream to prevent uncontrolled resource consumption (#3286)
        if (cropper.getSizeLimit() != null) {
            inputStream = new BoundedInputStream(inputStream, cropper.getSizeLimit());
        }
        BufferedImage outputImage = ImageIO.read(inputStream);
        // see #1208
        if (x + w > outputImage.getWidth()) {
            w = outputImage.getWidth() - x;
        }
        if (y + h > outputImage.getHeight()) {
            h = outputImage.getHeight() - y;
        }
        BufferedImage cropped = outputImage.getSubimage(x, y, w, h);
        ByteArrayOutputStream croppedOutImage = new ByteArrayOutputStream();
        String format = guessImageFormat(contentType, imagePath);
        ImageIO.write(cropped, format, croppedOutImage);
        return new CroppedImage(cropper.getImage().toString(), croppedOutImage.toByteArray(), x, y, w, h);
    } catch (IOException e) {
        LOGGER.severe(e.getMessage());
        throw new ConverterException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                LOGGER.severe(e.getMessage());
            }
        }
    }
}
Also used : ConverterException(javax.faces.convert.ConverterException) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) Resource(javax.faces.application.Resource) URL(java.net.URL) URLConnection(java.net.URLConnection) BufferedImage(java.awt.image.BufferedImage) CroppedImage(org.primefaces.model.CroppedImage) ExternalContext(javax.faces.context.ExternalContext) BoundedInputStream(org.apache.commons.io.input.BoundedInputStream) StreamedContent(org.primefaces.model.StreamedContent)

Example 2 with CroppedImage

use of org.primefaces.model.CroppedImage in project primefaces by primefaces.

the class ImageCropperRenderer method encodeScript.

protected void encodeScript(FacesContext context, ImageCropper cropper) throws IOException {
    String widgetVar = cropper.resolveWidgetVar(context);
    String clientId = cropper.getClientId(context);
    String image = clientId + "_image";
    String select = null;
    WidgetBuilder wb = getWidgetBuilder(context);
    wb.initWithComponentLoad("ImageCropper", widgetVar, clientId, clientId + "_image").attr("image", image).attr("viewMode", cropper.getViewMode(), 0).attr("aspectRatio", cropper.getAspectRatio(), Double.MIN_VALUE).attr("responsive", cropper.isResponsive(), true).attr("zoomOnTouch", cropper.isZoomOnTouch(), true).attr("zoomOnWheel", cropper.isZoomOnWheel(), true).attr("guides", cropper.isGuides(), true);
    if (cropper.getMinSize() != null) {
        wb.append(",minSize:[").append(cropper.getMinSize()).append("]");
    }
    if (cropper.getMaxSize() != null) {
        wb.append(",maxSize:[").append(cropper.getMaxSize()).append("]");
    }
    Object value = cropper.getValue();
    if (value != null) {
        CroppedImage croppedImage = (CroppedImage) value;
        int x = croppedImage.getLeft();
        int y = croppedImage.getTop();
        int x2 = x + croppedImage.getWidth();
        int y2 = y + croppedImage.getHeight();
        select = "[" + x + "," + y + "," + x2 + "," + y2 + "]";
    } else if (LangUtils.isNotBlank(cropper.getInitialCoords())) {
        select = "[" + cropper.getInitialCoords() + "]";
    }
    if (select != null) {
        wb.append(",initialCoords:").append(select);
    }
    wb.finish();
}
Also used : CroppedImage(org.primefaces.model.CroppedImage)

Aggregations

CroppedImage (org.primefaces.model.CroppedImage)2 BufferedImage (java.awt.image.BufferedImage)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 Resource (javax.faces.application.Resource)1 ExternalContext (javax.faces.context.ExternalContext)1 ConverterException (javax.faces.convert.ConverterException)1 BoundedInputStream (org.apache.commons.io.input.BoundedInputStream)1 StreamedContent (org.primefaces.model.StreamedContent)1