Skip to content

Commit fb74e25

Browse files
getFileSize fix, no crash and correct return value (#674)
Issue:101381 Co-authored-by: tomas-sexenian <[email protected]>
1 parent 0b3163d commit fb74e25

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

java/src/main/java/com/genexus/GxImageUtil.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,22 @@ private static GXFile getGXFile(String filePathOrUrl) {
4646
public static long getFileSize(String imageFile){
4747
if (!isValidInput(imageFile))
4848
return INVALID_CODE;
49-
50-
return new GXFile(imageFile).getLength();
49+
IHttpContext httpContext = com.genexus.ModelContext.getModelContext().getHttpContext();
50+
if (imageFile.toLowerCase().startsWith("http://") || imageFile.toLowerCase().startsWith("https://") ||
51+
(httpContext.isHttpContextWeb() && imageFile.startsWith(httpContext.getContextPath()))){
52+
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
53+
BufferedImage image = ImageIO.read(new URL(GXDbFile.pathToUrl( imageFile, httpContext)).openStream());
54+
String extension = imageFile.substring(imageFile.lastIndexOf(".") + 1);
55+
ImageIO.write(image, extension, baos);
56+
baos.flush();
57+
byte[] imageInByte = baos.toByteArray();
58+
return imageInByte.length;
59+
} catch (Exception e) {
60+
log.error("getFileSize " + imageFile + " failed" , e);
61+
}
62+
} else
63+
return getGXFile(imageFile).getLength();
64+
return INVALID_CODE;
5165
}
5266

5367
public static int getImageHeight(String imageFile) {

0 commit comments

Comments
 (0)