Skip to content

Commit fdfe731

Browse files
committed
chore: Utils class added
1 parent 3af7846 commit fdfe731

File tree

1 file changed

+85
-0
lines changed
  • imagekit-sdk/src/main/java/io/imagekit/sdk/utils

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package io.imagekit.sdk.utils;
2+
3+
import io.imagekit.sdk.config.Configuration;
4+
5+
import java.io.*;
6+
import java.net.URL;
7+
import java.util.Base64;
8+
import java.util.List;
9+
import java.util.Properties;
10+
11+
public class Utils {
12+
13+
public static String listToString(List<String> list){
14+
StringBuilder builder=new StringBuilder();
15+
for (int i=0; i<list.size(); i++){
16+
if (null!=list.get(i)) {
17+
builder.append(list.get(i));
18+
if (i < list.size() - 1) {
19+
builder.append(",");
20+
}
21+
}
22+
}
23+
return builder.toString();
24+
}
25+
26+
public static String fileToBase64(File file){
27+
String base64File = "";
28+
try (FileInputStream imageInFile = new FileInputStream(file)) {
29+
// Reading a file from file system
30+
byte fileData[] = new byte[(int) file.length()];
31+
imageInFile.read(fileData);
32+
base64File = Base64.getEncoder().encodeToString(fileData);
33+
} catch (FileNotFoundException e) {
34+
System.out.println("File not found" + e);
35+
} catch (IOException ioe) {
36+
System.out.println("Exception while reading the file " + ioe);
37+
}
38+
return base64File;
39+
}
40+
41+
public static byte[] fileToBytes(File file) {
42+
byte[] bytes=null;
43+
try (FileInputStream imageInFile = new FileInputStream(file)) {
44+
// Reading a file from file system
45+
bytes = new byte[(int) file.length()];
46+
imageInFile.read(bytes);
47+
} catch (FileNotFoundException e) {
48+
System.out.println("File not found" + e);
49+
} catch (IOException ioe) {
50+
System.out.println("Exception while reading the file " + ioe);
51+
}
52+
return bytes;
53+
}
54+
55+
public static String bytesToBase64(byte[] fileData){
56+
String base64File = "";
57+
base64File = Base64.getEncoder().encodeToString(fileData);
58+
return base64File;
59+
}
60+
61+
/**
62+
*
63+
* @param cls is a Class name from which this method will invoke
64+
* @return it will return object of Configuration class
65+
* @throws IOException if config.properties file doesn't exists
66+
*/
67+
public static Configuration getSystemConfig(Class<?> cls) throws IOException{
68+
Properties properties=new Properties();
69+
String configFile="config.properties";
70+
InputStream is=cls.getClassLoader().getResourceAsStream(configFile);
71+
if (null!=is){
72+
properties.load(is);
73+
}
74+
else {
75+
throw new FileNotFoundException("Property file '"+configFile+"' not found in classpath");
76+
}
77+
Configuration config=new Configuration();
78+
config.setUrlEndpoint(properties.getProperty("UrlEndpoint"));
79+
config.setPublicKey(properties.getProperty("PublicKey"));
80+
config.setPrivateKey(properties.getProperty("PrivateKey"));
81+
config.validate();
82+
return config;
83+
}
84+
85+
}

0 commit comments

Comments
 (0)