From 63e0d7dee2399d0246748dfc9bb825cfe8ec575d Mon Sep 17 00:00:00 2001 From: tomas-sexenian Date: Fri, 14 Apr 2023 11:18:12 -0300 Subject: [PATCH 1/2] Correct blob path in ImageAPI with external storage Issue:102380 --- .../main/java/com/genexus/GxImageUtil.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/java/src/main/java/com/genexus/GxImageUtil.java b/java/src/main/java/com/genexus/GxImageUtil.java index 9c831a32e..feffff476 100644 --- a/java/src/main/java/com/genexus/GxImageUtil.java +++ b/java/src/main/java/com/genexus/GxImageUtil.java @@ -51,7 +51,7 @@ public static long getFileSize(String imageFile){ if (imageFile.toLowerCase().startsWith("http://") || imageFile.toLowerCase().startsWith("https://") || (httpContext.isHttpContextWeb() && imageFile.startsWith(httpContext.getContextPath()))){ try { - URL url = new URL(imageFile); + URL url = new URL(GXDbFile.pathToUrl( imageFile, httpContext)); URLConnection connection = url.openConnection(); return Long.parseLong(connection.getHeaderField("Content-Length")); } catch (Exception e) { @@ -112,11 +112,22 @@ public static String crop(String imageFile, int x, int y, int width, int height) private static String writeImage(BufferedImage croppedImage, String destinationFilePathOrUrl) throws IOException { String newFileName = PrivateUtilities.getTempFileName(CommonUtil.getFileType(destinationFilePathOrUrl)); - try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()) { + IHttpContext httpContext = com.genexus.ModelContext.getModelContext().getHttpContext(); + try (ByteArrayOutputStream outStream = new ByteArrayOutputStream()){ ImageIO.write(croppedImage, CommonUtil.getFileType(newFileName), outStream); - outStream.flush(); - byte[] imageInByte = outStream.toByteArray(); - return GXutil.blobFromBytes(imageInByte); + if (destinationFilePathOrUrl.toLowerCase().startsWith("http://") || destinationFilePathOrUrl.toLowerCase().startsWith("https://") || + (httpContext.isHttpContextWeb() && destinationFilePathOrUrl.startsWith(httpContext.getContextPath()))){ + try (ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray())) { + GXFile file = getGXFile(newFileName); + file.create(inStream, true); + file.close(); + return file.getURI(); + } + } else { + outStream.flush(); + byte[] imageInByte = outStream.toByteArray(); + return GXutil.blobFromBytes(imageInByte); + } } } From 735cdb7aca7506f7867dbd2c563f72aaa3d77aa7 Mon Sep 17 00:00:00 2001 From: tomas-sexenian Date: Fri, 14 Apr 2023 12:48:39 -0300 Subject: [PATCH 2/2] Remove KB file filesize fix, to be added in another PR --- java/src/main/java/com/genexus/GxImageUtil.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/src/main/java/com/genexus/GxImageUtil.java b/java/src/main/java/com/genexus/GxImageUtil.java index feffff476..69042e222 100644 --- a/java/src/main/java/com/genexus/GxImageUtil.java +++ b/java/src/main/java/com/genexus/GxImageUtil.java @@ -51,7 +51,7 @@ public static long getFileSize(String imageFile){ if (imageFile.toLowerCase().startsWith("http://") || imageFile.toLowerCase().startsWith("https://") || (httpContext.isHttpContextWeb() && imageFile.startsWith(httpContext.getContextPath()))){ try { - URL url = new URL(GXDbFile.pathToUrl( imageFile, httpContext)); + URL url = new URL(imageFile); URLConnection connection = url.openConnection(); return Long.parseLong(connection.getHeaderField("Content-Length")); } catch (Exception e) {