Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 21 additions & 34 deletions java/src/main/java/com/genexus/GxImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,41 +116,28 @@ public static String crop(String imageFile, int x, int y, int width, int height)
private static final Pattern IMAGE_PATTERN = Pattern.compile("\\.(jpg|jpeg|png|bmp|webp|jfif)([/?]|$)", Pattern.CASE_INSENSITIVE);

private static String writeImage(BufferedImage bufferedImage, String destinationFilePathOrUrl) throws IOException {
IHttpContext httpContext = com.genexus.ModelContext.getModelContext().getHttpContext();
if (destinationFilePathOrUrl.toLowerCase().startsWith("http://") || destinationFilePathOrUrl.toLowerCase().startsWith("https://") || (httpContext.isHttpContextWeb() && destinationFilePathOrUrl.startsWith(httpContext.getContextPath()))){

String newFileName;
if (!IMAGE_PATTERN.matcher(destinationFilePathOrUrl).find()) {
URL imageUrl = new URL(destinationFilePathOrUrl);
HttpURLConnection connection = null;
String format;
try {
connection = (HttpURLConnection) imageUrl.openConnection();
format = connection.getContentType().split("/")[1];
} finally {
if (connection != null) connection.disconnect();
}
newFileName = PrivateUtilities.getTempFileName(format);
} else
newFileName = PrivateUtilities.getTempFileName(CommonUtil.getFileType(destinationFilePathOrUrl));

try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
ImageIO.write(bufferedImage, CommonUtil.getFileType(newFileName), outStream);
try (ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray())) {
GXFile file = getGXFile(newFileName);
file.create(inStream, true);
file.close();
return file.getURI();
}
String newFileName;
if (!IMAGE_PATTERN.matcher(destinationFilePathOrUrl).find()) {
URL imageUrl = new URL(destinationFilePathOrUrl);
HttpURLConnection connection = null;
String format;
try {
connection = (HttpURLConnection) imageUrl.openConnection();
format = connection.getContentType().split("/")[1];
} finally {
if (connection != null) connection.disconnect();
}

} else {
String newFileName = PrivateUtilities.getTempFileName(CommonUtil.getFileType(destinationFilePathOrUrl));
try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
ImageIO.write(bufferedImage, CommonUtil.getFileType(newFileName), outStream);
outStream.flush();
byte[] imageInByte = outStream.toByteArray();
return GXutil.blobFromBytes(imageInByte,CommonUtil.getFileType(newFileName));
newFileName = PrivateUtilities.getTempFileName(format);
} else
newFileName = PrivateUtilities.getTempFileName(CommonUtil.getFileType(destinationFilePathOrUrl));

try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
ImageIO.write(bufferedImage, CommonUtil.getFileType(newFileName), outStream);
try (ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray())) {
GXFile file = getGXFile(Preferences.getDefaultPreferences().getPRIVATE_PATH() + newFileName);
file.create(inStream, true);
file.close();
return file.getURI();
}
}
}
Expand Down