Repository: primefaces/primefaces (java)

Issue:
ImageCropper: originalFilename does not match the name attribute of DefaultStreamedContent
### Describe the bug

`CroppedImage `object has an `originalFilename` attribute which is the name of the image that has been cropped.

If one is cropping an image which is obtained via a `StreamContent` rather than a static file, then there is no filename as such. Conveniently, a name can still be set to the `StreamContent`:

`StreamedContent imageAsStream = DefaultStreamedContent.builder()**.name("example")**.contentType("image/png").stream(() -> stream).build();`

yet, when one calls the method `getOriginalFileName `on the `CroppedImage`, it produces a `String `which is the result of the `toString()` method of the original `StreamContent`, instead of returning the `name` attribute that had been set in the builder.

Use case: I need to match the original pic with the cropped pic, and it would make my life easier if I could just use their names to match them.




### Reproducer

This case is deeply embedded in an app, can't create a reproducer.

### Expected behavior

The `originalFilename `attribute of `CroppedImage `should match the `name `attribute of the image that was cropped and which originated from a `DefaultStreamContent`.

### PrimeFaces edition

Community

### PrimeFaces version

12.0

### Theme

default theme

### JSF implementation

Mojarra

### JSF version

2.3

### Java version

17

### Browser(s)

_No response_

Pull request:
Fix #10365: 13.1 ImageCropper originalFilename for streamed content

Patch:
diff --git a/primefaces/src/main/java/org/primefaces/component/imagecropper/ImageCropperRenderer.java b/primefaces/src/main/java/org/primefaces/component/imagecropper/ImageCropperRenderer.java
index ee0349d145..76da8daf1f 100644
--- a/primefaces/src/main/java/org/primefaces/component/imagecropper/ImageCropperRenderer.java
+++ b/primefaces/src/main/java/org/primefaces/component/imagecropper/ImageCropperRenderer.java
@@ -206,7 +206,20 @@ public class ImageCropperRenderer extends CoreRenderer {
             String format = guessImageFormat(stream.getContentType(), stream.getName());
             ImageIO.write(cropped, format, croppedOutImage);
 
-            return new CroppedImage(cropper.getImage().toString(), croppedOutImage.toByteArray(), x, y, w, h);
+            Object imageObject = cropper.getImage();
+            String originalFileName;
+            if (imageObject instanceof String) {
+                originalFileName = (String) imageObject;
+            }
+            else if (imageObject instanceof StreamedContent) {
+                StreamedContent streamedContentTmp = (StreamedContent) imageObject;
+                originalFileName = streamedContentTmp.getName();
+            }
+            else {
+                throw new IllegalArgumentException("ImageCropper 'image' must be either a String relative path or a StreamedObject.");
+            }
+
+            return new CroppedImage(originalFileName, croppedOutImage.toByteArray(), x, y, w, h);
         }
         catch (IOException e) {
             throw new ConverterException(e);
@@ -291,7 +304,7 @@ public class ImageCropperRenderer extends CoreRenderer {
                 contentType = streamedContentTmp.getContentType();
             }
             else {
-                throw new IllegalArgumentException("'image' must be either an String relative path or a StreamedObject.");
+                throw new IllegalArgumentException("ImageCropper 'image' must be either a String relative path or a StreamedObject.");
             }
         }
 


Test patch:
diff --git a/primefaces/src/test/java/org/primefaces/component/imagecropper/ImageCropperRendererTest.java b/primefaces/src/test/java/org/primefaces/component/imagecropper/ImageCropperRendererTest.java
index 5934fefa49..2dc34702ea 100644
--- a/primefaces/src/test/java/org/primefaces/component/imagecropper/ImageCropperRendererTest.java
+++ b/primefaces/src/test/java/org/primefaces/component/imagecropper/ImageCropperRendererTest.java
@@ -97,7 +97,7 @@ public class ImageCropperRendererTest {
         }
         catch (IllegalArgumentException e) {
             String message = e.getMessage();
-            if (!"'image' must be either an String relative path or a StreamedObject.".equals(message)) {
+            if (!"ImageCropper 'image' must be either a String relative path or a StreamedObject.".equals(message)) {
                 Assertions.fail("should thrown IllegalArgumentException with message: " + message);
             }
         }