Skip to content

Commit

Permalink
Cherry pick branch 'genexuslabs:ImageAPITempDirectory' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-sexenian authored and genexusbot committed Oct 10, 2023
1 parent d355870 commit 03e93a3
Showing 1 changed file with 21 additions and 34 deletions.
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

0 comments on commit 03e93a3

Please sign in to comment.