From 420ea86a4079100d49182bff5f2f1acab0a11454 Mon Sep 17 00:00:00 2001 From: tomas-sexenian Date: Mon, 9 Oct 2023 10:59:57 -0300 Subject: [PATCH] Revert "Send all manipulated images to temp media directory" This reverts commit 08d93bff1ea5422bef909b747cfa340409efa4e0. --- .../main/java/com/genexus/GxImageUtil.java | 55 ++++++++++++------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/java/src/main/java/com/genexus/GxImageUtil.java b/java/src/main/java/com/genexus/GxImageUtil.java index 2c55d5fae..5a578c734 100644 --- a/java/src/main/java/com/genexus/GxImageUtil.java +++ b/java/src/main/java/com/genexus/GxImageUtil.java @@ -116,28 +116,41 @@ 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 { - 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(); + 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(); + } } - 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(); + + } 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)); } } }