Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 5, 2025

This PR adds a convenient constructor to the Image component that accepts byte array content and an image name, making it easier to create images from in-memory data.

New Constructor

public Image(byte[] imageContent, String imageName) {
    setSrc(DownloadHandler.fromInputStream(event -> {
        return new DownloadResponse(new ByteArrayInputStream(imageContent), imageName,
            URLConnection.guessContentTypeFromName(imageName), imageContent.length);
    }).inline());
    setAlt(imageName);
}

Features

  • Automatic content type detection: Uses URLConnection.guessContentTypeFromName() to determine the MIME type based on the file extension
  • Inline content disposition: Automatically sets the download handler to inline mode for proper image display
  • Alt text assignment: Uses the image name as the alternative text for accessibility
  • Seamless integration: Works with the existing DownloadHandler infrastructure

Usage Example

// Load image data from database, API, or generate programmatically
byte[] imageData = loadImageFromDatabase();
String fileName = "profile-picture.png";

// Create image component with one line
Image image = new Image(imageData, fileName);
layout.add(image);

This eliminates the need for developers to manually create DownloadHandler instances and DownloadResponse objects for the common use case of displaying images from byte array data.

Testing

Added comprehensive tests covering:

  • Normal usage with proper file extensions
  • Edge cases with missing file extensions
  • Empty byte arrays
  • Verification of alt text and src attribute assignment

Fixes #21967.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • maven.vaadin.com
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.11/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.11/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.11 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.11/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/flow/flow org.codehaus.plexus.classworlds.launcher.Launcher clean compile -q -pl flow-html-components (dns block)
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.11/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.11/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.11 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.11/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/flow/flow org.codehaus.plexus.classworlds.launcher.Launcher test -q -Dtest=ImageTest -pl flow-html-components (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@CLAassistant
Copy link

CLAassistant commented Aug 5, 2025

CLA assistant check
All committers have signed the CLA.

@Copilot Copilot AI changed the title [WIP] Add Image constructor for typical use-cases Add Image constructor for typical use-cases with byte array content Aug 5, 2025
@Copilot Copilot AI requested a review from mshabarov August 5, 2025 10:35
Copilot finished work on behalf of mshabarov August 5, 2025 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Image constructor for typical use-cases

3 participants