Skip to content

Commit

Permalink
Revert "Send all manipulated images to temp media directory"
Browse files Browse the repository at this point in the history
This reverts commit 08d93bf.
  • Loading branch information
tomas-sexenian committed Oct 9, 2023
1 parent b3f6c8e commit 420ea86
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions java/src/main/java/com/genexus/GxImageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down

0 comments on commit 420ea86

Please sign in to comment.