@@ -116,41 +116,28 @@ public static String crop(String imageFile, int x, int y, int width, int height)
116116 private static final Pattern IMAGE_PATTERN = Pattern .compile ("\\ .(jpg|jpeg|png|bmp|webp|jfif)([/?]|$)" , Pattern .CASE_INSENSITIVE );
117117
118118 private static String writeImage (BufferedImage bufferedImage , String destinationFilePathOrUrl ) throws IOException {
119- IHttpContext httpContext = com .genexus .ModelContext .getModelContext ().getHttpContext ();
120- if (destinationFilePathOrUrl .toLowerCase ().startsWith ("http://" ) || destinationFilePathOrUrl .toLowerCase ().startsWith ("https://" ) || (httpContext .isHttpContextWeb () && destinationFilePathOrUrl .startsWith (httpContext .getContextPath ()))){
121-
122- String newFileName ;
123- if (!IMAGE_PATTERN .matcher (destinationFilePathOrUrl ).find ()) {
124- URL imageUrl = new URL (destinationFilePathOrUrl );
125- HttpURLConnection connection = null ;
126- String format ;
127- try {
128- connection = (HttpURLConnection ) imageUrl .openConnection ();
129- format = connection .getContentType ().split ("/" )[1 ];
130- } finally {
131- if (connection != null ) connection .disconnect ();
132- }
133- newFileName = PrivateUtilities .getTempFileName (format );
134- } else
135- newFileName = PrivateUtilities .getTempFileName (CommonUtil .getFileType (destinationFilePathOrUrl ));
136-
137- try (ByteArrayOutputStream outStream = new ByteArrayOutputStream ()) {
138- ImageIO .write (bufferedImage , CommonUtil .getFileType (newFileName ), outStream );
139- try (ByteArrayInputStream inStream = new ByteArrayInputStream (outStream .toByteArray ())) {
140- GXFile file = getGXFile (newFileName );
141- file .create (inStream , true );
142- file .close ();
143- return file .getURI ();
144- }
119+ String newFileName ;
120+ if (!IMAGE_PATTERN .matcher (destinationFilePathOrUrl ).find ()) {
121+ URL imageUrl = new URL (destinationFilePathOrUrl );
122+ HttpURLConnection connection = null ;
123+ String format ;
124+ try {
125+ connection = (HttpURLConnection ) imageUrl .openConnection ();
126+ format = connection .getContentType ().split ("/" )[1 ];
127+ } finally {
128+ if (connection != null ) connection .disconnect ();
145129 }
146-
147- } else {
148- String newFileName = PrivateUtilities .getTempFileName (CommonUtil .getFileType (destinationFilePathOrUrl ));
149- try (ByteArrayOutputStream outStream = new ByteArrayOutputStream ()) {
150- ImageIO .write (bufferedImage , CommonUtil .getFileType (newFileName ), outStream );
151- outStream .flush ();
152- byte [] imageInByte = outStream .toByteArray ();
153- return GXutil .blobFromBytes (imageInByte ,CommonUtil .getFileType (newFileName ));
130+ newFileName = PrivateUtilities .getTempFileName (format );
131+ } else
132+ newFileName = PrivateUtilities .getTempFileName (CommonUtil .getFileType (destinationFilePathOrUrl ));
133+
134+ try (ByteArrayOutputStream outStream = new ByteArrayOutputStream ()) {
135+ ImageIO .write (bufferedImage , CommonUtil .getFileType (newFileName ), outStream );
136+ try (ByteArrayInputStream inStream = new ByteArrayInputStream (outStream .toByteArray ())) {
137+ GXFile file = getGXFile (Preferences .getDefaultPreferences ().getPRIVATE_PATH () + newFileName );
138+ file .create (inStream , true );
139+ file .close ();
140+ return file .getURI ();
154141 }
155142 }
156143 }
0 commit comments