55import java .awt .geom .AffineTransform ;
66import java .awt .image .AffineTransformOp ;
77import java .awt .image .BufferedImage ;
8- import java .io .File ;
9- import java .io .FileOutputStream ;
10- import java .io .IOException ;
8+ import java .io .*;
119
10+ import com .genexus .db .driver .ResourceAccessControlList ;
11+ import com .genexus .util .GxFileInfoSourceType ;
1212import com .genexus .util .GXFile ;
1313import org .apache .logging .log4j .Logger ;
1414
1515public class GxImageUtil {
1616 private static Logger log = org .apache .logging .log4j .LogManager .getLogger (GxImageUtil .class );
17+ private static int INVALID_CODE = -1 ;
1718
18- private static String getImageAbsolutePath (String imageFile ){
19- if (CommonUtil .isUploadPrefix (imageFile )) {
20- return new GXFile (imageFile ).getAbsolutePath ();
19+ private static InputStream getInputStream (String filePathOrUrl ) throws IOException {
20+ return getGXFile (filePathOrUrl ).getStream ();
21+ }
22+
23+ private static BufferedImage createBufferedImageFromURI (String filePathOrUrl ) throws IOException
24+ {
25+ try (InputStream is = getGXFile (filePathOrUrl ).getStream ()) {
26+ return ImageIO .read (is );
27+ }
28+ catch (IOException e ) {
29+ log .error ("Failed to read image stream: " + filePathOrUrl );
30+ throw e ;
2131 }
22- String defaultPath = com .genexus .ModelContext .getModelContext ().getHttpContext ().getDefaultPath ();
23- return imageFile .startsWith (defaultPath )? imageFile : defaultPath + imageFile .replace ("/" , File .separator );
32+ }
33+
34+ private static GXFile getGXFile (String filePathOrUrl ) {
35+ String basePath = (com .genexus .ModelContext .getModelContext () != null ) ? com .genexus .ModelContext .getModelContext ().getHttpContext ().getDefaultPath (): "" ;
36+ return new GXFile (basePath , filePathOrUrl , ResourceAccessControlList .Default , GxFileInfoSourceType .Unknown );
2437 }
2538
2639 public static long getFileSize (String imageFile ){
27- return new File (getImageAbsolutePath (imageFile )).length ();
40+ if (!isValidInput (imageFile ))
41+ return INVALID_CODE ;
42+
43+ return new GXFile (imageFile ).getLength ();
2844 }
2945
3046 public static int getImageHeight (String imageFile ) {
47+ if (!isValidInput (imageFile ))
48+ return INVALID_CODE ;
49+
3150 try {
32- BufferedImage image = ImageIO .read (new File (getImageAbsolutePath (imageFile )));
33- return image .getHeight ();
51+ return createBufferedImageFromURI (imageFile ).getHeight ();
3452 }
35- catch (IOException e ) {
53+ catch (Exception e ) {
3654 log .error ("getImageHeight " + imageFile + " failed" , e );
37- return 0 ;
3855 }
56+ return INVALID_CODE ;
57+ }
58+
59+ private static boolean isValidInput (String imageFile ) {
60+ boolean isValid = imageFile != null && imageFile .length () > 0 ;
61+ if (!isValid ) {
62+ log .debug ("Image Api - FileName cannot be empty" );
63+ }
64+ return isValid ;
3965 }
4066
4167 public static int getImageWidth (String imageFile ) {
68+ if (!isValidInput (imageFile ))
69+ return INVALID_CODE ;
70+
4271 try {
43- BufferedImage image = ImageIO .read (new File (getImageAbsolutePath (imageFile )));
44- return image .getWidth ();
72+ return createBufferedImageFromURI (imageFile ).getWidth ();
4573 }
46- catch (IOException e ) {
74+ catch (Exception e ) {
4775 log .error ("getImageWidth " + imageFile + " failed" , e );
48- return 0 ;
4976 }
77+ return INVALID_CODE ;
5078 }
5179
52- public static String crop (String imageFile , int x , int y , int width , int height ){
80+ public static String crop (String imageFile , int x , int y , int width , int height ) {
81+ if (!isValidInput (imageFile ))
82+ return "" ;
83+
5384 try {
54- String absolutePath = getImageAbsolutePath (imageFile );
55- BufferedImage image = ImageIO .read (new File (absolutePath ));
56- BufferedImage cropedImage = image .getSubimage (x , y , width , height );
57- ImageIO .write (cropedImage , CommonUtil .getFileType (absolutePath ), new FileOutputStream (absolutePath ));
85+ BufferedImage image = createBufferedImageFromURI (imageFile );
86+ BufferedImage croppedImage = image .getSubimage (x , y , width , height );
87+ writeImage (croppedImage , imageFile );
5888 }
59- catch (IOException e ) {
89+ catch (Exception e ) {
6090 log .error ("crop " + imageFile + " failed" , e );
6191 }
6292 return imageFile ;
6393 }
6494
65- public static String flipHorizontally (String imageFile ){
95+ private static void writeImage (BufferedImage croppedImage , String destinationFilePathOrUrl ) throws IOException {
96+ try (ByteArrayOutputStream outStream = new ByteArrayOutputStream ()) {
97+ ImageIO .write (croppedImage , CommonUtil .getFileType (destinationFilePathOrUrl ), outStream );
98+ try (ByteArrayInputStream inStream = new ByteArrayInputStream (outStream .toByteArray ())) {
99+ GXFile file = getGXFile (destinationFilePathOrUrl );
100+ file .create (inStream , true );
101+ file .close ();
102+ }
103+ }
104+ }
105+
106+ public static String flipHorizontally (String imageFile ) {
107+ if (!isValidInput (imageFile ))
108+ return "" ;
109+
66110 try {
67- String absolutePath = getImageAbsolutePath (imageFile );
68- BufferedImage image = ImageIO .read (new File (absolutePath ));
111+ BufferedImage image = createBufferedImageFromURI (imageFile );
69112 AffineTransform tx = AffineTransform .getScaleInstance (-1 , 1 );
70113 tx .translate (-image .getWidth (null ), 0 );
71114 AffineTransformOp op = new AffineTransformOp (tx , AffineTransformOp .TYPE_NEAREST_NEIGHBOR );
72- BufferedImage flipedImage = op .filter (image , null );
73- ImageIO . write ( flipedImage , CommonUtil . getFileType ( absolutePath ), new FileOutputStream ( absolutePath ) );
115+ BufferedImage flippedImage = op .filter (image , null );
116+ writeImage ( flippedImage , imageFile );
74117 }
75- catch (IOException e ) {
118+ catch (Exception e ) {
76119 log .error ("flip horizontal " + imageFile + " failed" , e );
77120 }
78121 return imageFile ;
79122 }
80123
81- public static String flipVertically (String imageFile ){
124+ public static String flipVertically (String imageFile ) {
125+ if (!isValidInput (imageFile ))
126+ return "" ;
127+
82128 try {
83- String absolutePath = getImageAbsolutePath (imageFile );
84- BufferedImage image = ImageIO .read (new File (absolutePath ));
129+ BufferedImage image = createBufferedImageFromURI (imageFile );
85130 AffineTransform tx = AffineTransform .getScaleInstance (1 , -1 );
86131 tx .translate (0 , -image .getHeight (null ));
87132 AffineTransformOp op = new AffineTransformOp (tx , AffineTransformOp .TYPE_NEAREST_NEIGHBOR );
88- BufferedImage flipedImage = op .filter (image , null );
89- ImageIO . write ( flipedImage , CommonUtil . getFileType ( absolutePath ), new FileOutputStream ( absolutePath ) );
133+ BufferedImage flippedImage = op .filter (image , null );
134+ writeImage ( flippedImage , imageFile );
90135 }
91- catch (IOException e ) {
136+ catch (Exception e ) {
92137 log .error ("flip vertical " + imageFile + " failed" , e );
93138 }
94139 return imageFile ;
95140 }
96141
97- public static String resize (String imageFile , int width , int height , boolean keepAspectRatio ){
142+ public static String resize (String imageFile , int width , int height , boolean keepAspectRatio ) {
143+ if (!isValidInput (imageFile ))
144+ return "" ;
145+
98146 try {
99- String absolutePath = getImageAbsolutePath (imageFile );
100- BufferedImage image = ImageIO .read (new File (absolutePath ));
147+ BufferedImage image = createBufferedImageFromURI (imageFile );
101148 if (keepAspectRatio ) {
102149 double imageHeight = image .getHeight ();
103150 double imageWidth = image .getWidth ();
@@ -112,34 +159,37 @@ public static String resize(String imageFile, int width, int height, boolean kee
112159 Graphics2D g2d = resizedImage .createGraphics ();
113160 g2d .drawImage (image , 0 , 0 , width , height , null );
114161 g2d .dispose ();
115- ImageIO . write (resizedImage , CommonUtil . getFileType ( absolutePath ), new FileOutputStream ( absolutePath ) );
162+ writeImage (resizedImage , imageFile );
116163 }
117- catch (IOException e ) {
164+ catch (Exception e ) {
118165 log .error ("resize " + imageFile + " failed" , e );
119166 }
120167 return imageFile ;
121168 }
122169
123- public static String scale (String imageFile , short percent ){
170+ public static String scale (String imageFile , short percent ) {
171+ if (!isValidInput (imageFile ))
172+ return "" ;
173+
124174 try {
125- String absolutePath = getImageAbsolutePath (imageFile );
126- BufferedImage image = ImageIO .read (new File (absolutePath ));
175+ BufferedImage image = createBufferedImageFromURI (imageFile );
127176 imageFile = resize (imageFile , image .getWidth () * percent / 100 , image .getHeight () * percent / 100 ,true );
128177 }
129- catch (IOException e ) {
178+ catch (Exception e ) {
130179 log .error ("scale " + imageFile + " failed" , e );
131180 }
132181 return imageFile ;
133182 }
134183
135- public static String rotate (String imageFile , short angle ){
184+ public static String rotate (String imageFile , short angle ) {
185+ if (!isValidInput (imageFile ))
186+ return "" ;
136187 try {
137- String absolutePath = getImageAbsolutePath (imageFile );
138- BufferedImage image = ImageIO .read (new File (absolutePath ));
188+ BufferedImage image = createBufferedImageFromURI (imageFile );
139189 BufferedImage rotatedImage = rotateImage (image , angle );
140- ImageIO . write (rotatedImage , CommonUtil . getFileType ( absolutePath ), new FileOutputStream ( absolutePath ) );
190+ writeImage (rotatedImage , imageFile );
141191 }
142- catch (IOException e ) {
192+ catch (Exception e ) {
143193 log .error ("rotate " + imageFile + " failed" , e );
144194 }
145195 return imageFile ;
0 commit comments