Skip to content

Commit 46e0c29

Browse files
committed
chore: sample sdk running code added
1 parent f928dbe commit 46e0c29

File tree

4 files changed

+389
-0
lines changed

4 files changed

+389
-0
lines changed
Lines changed: 362 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,362 @@
1+
package io.imagekit.sampleapp;
2+
3+
4+
import com.google.gson.Gson;
5+
import io.imagekit.sdk.config.Configuration;
6+
import io.imagekit.sdk.ImageKit;
7+
import io.imagekit.sdk.models.BaseFile;
8+
import io.imagekit.sdk.models.FileCreateRequest;
9+
import io.imagekit.sdk.models.FileUpdateRequest;
10+
import io.imagekit.sdk.models.results.*;
11+
import io.imagekit.sdk.tasks.UrlGen;
12+
import io.imagekit.sdk.utils.Utils;
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.math.BigInteger;
17+
import java.net.MalformedURLException;
18+
import java.net.URI;
19+
import java.net.URL;
20+
import java.util.*;
21+
import java.util.stream.Collectors;
22+
23+
class App{
24+
public static void main(String[] args) throws Exception{
25+
ImageKit imageKit=ImageKit.getInstance();
26+
//getSystemConfig(Class<?> cls) method need current class as parameter
27+
Configuration config = Utils.getSystemConfig(App.class);
28+
imageKit.setConfig(config);
29+
30+
// uploadFromURL();
31+
// uploadFromBase64();
32+
uploadFromBytes();
33+
//
34+
// calculateDistance();
35+
// generatingAuthParams();
36+
//
37+
// List<BaseFile> files=getList(0,10);
38+
// if(null!=files && files.size()>0){
39+
// getFileDetail(files.get(0).getFileId());
40+
// getFileMetaData(files.get(0).getFileId());
41+
// getRemoteFileMetaData(files.get(1).getUrl());
42+
// updateDetails(files.get(0).getFileId());
43+
// ResultCache resultCache = purgeCache(files.get(0).getUrl());
44+
// getPurgeCacheStatus(resultCache.getRequestId());
45+
// generateUrl(files.get(0));
46+
//
47+
// // System.out.println(Color.ANSI_CYAN+"Do you want to delete uploaded files? (yes/No): "+Color.ANSI_BLUE);
48+
// // String choice=new Scanner(System.in).nextLine();
49+
// // if (!"yes".equalsIgnoreCase(choice.trim().toLowerCase())){
50+
// // System.exit(0);
51+
// // }
52+
// System.out.println(Color.ANSI_RESET);
53+
// deleteFile(files.get(0).getFileId());
54+
// files.remove(0);
55+
// deleteFiles(files);
56+
// }
57+
58+
System.exit(0);
59+
}
60+
61+
private static void generatingAuthParams() {
62+
System.out.println(Color.ANSI_CYAN+">> Generating Authentication Parameters:"+Color.ANSI_RESET);
63+
Map<String, String> authenticationParameters = ImageKit.getInstance().getAuthenticationParameters();
64+
System.out.println(Color.ANSI_GREEN+">> Results:"+Color.ANSI_RESET);
65+
System.out.println(authenticationParameters);
66+
System.out.println("\n\n");
67+
}
68+
69+
private static void generateUrl(BaseFile baseFile) {
70+
System.out.println(Color.ANSI_CYAN+">> URL Generation:"+Color.ANSI_RESET);
71+
// String urlEndpoint=ImageKit.getInstance().getConfig().getUrlEndpoint();
72+
String urlEndpoint="https://ik.imagekit.io/bowstring/";
73+
74+
Map<String, String> queryParam=new HashMap<>();
75+
queryParam.put("v","123");
76+
77+
List<Map<String, String>> transformation=new ArrayList<Map<String, String>>();
78+
Map<String, String> scale=new HashMap<>();
79+
scale.put("height","600");
80+
scale.put("width","400");
81+
transformation.add(scale);
82+
Map<String, String> rotate=new HashMap<>();
83+
rotate.put("rotation","90");
84+
transformation.add(rotate);
85+
86+
Map<String, String> format=new HashMap<>();
87+
format.put("format","jpg");
88+
format.put("progressive","true");
89+
format.put("effectSharpen","-");
90+
format.put("effectContrast","1");
91+
format.put("blur","5%");
92+
93+
transformation.add(format);
94+
95+
96+
Map<String, Object> options=new HashMap();
97+
options.put("path",baseFile.getFilePath());
98+
options.put("transformation", transformation);
99+
100+
String url1=ImageKit.getInstance().getUrl(options);
101+
102+
options.clear();
103+
options.put("path",baseFile.getFilePath());
104+
options.put("urlEndpoint",urlEndpoint);
105+
options.put("queryParameters",queryParam);
106+
options.put("transformation", transformation);
107+
options.put("transformationPosition", "query");
108+
options.put("signed",true);
109+
options.put("expireSeconds",10);
110+
111+
String url2 = ImageKit.getInstance().getUrl(options);
112+
113+
options.remove("transformationPosition");
114+
String url3 = ImageKit.getInstance().getUrl(options);
115+
116+
options.clear();
117+
options.put("src",baseFile.getUrl());
118+
options.put("queryParameters",queryParam);
119+
options.put("transformation",transformation);
120+
121+
String url4 = ImageKit.getInstance().getUrl(options);
122+
123+
124+
System.out.println(">> Generated URL #1:\t"+url1);
125+
System.out.println(">> Generated URL #2:\t"+url2);
126+
System.out.println(">> Generated URL #3:\t"+url3);
127+
System.out.println(">> Generated URL #4:\t"+url4);
128+
System.out.println("\n\n");
129+
}
130+
131+
private static void calculateDistance() {
132+
System.out.println(Color.ANSI_CYAN+">> Calculating pHash Distance:"+Color.ANSI_RESET);
133+
int d1 = ImageKit.getInstance().pHashDistance("f06830ca9f1e3e90", "f06830ca9f1e3e90");
134+
System.out.println(">> Distance 1: "+d1);
135+
// output: 0 (same images)
136+
137+
int d2 = ImageKit.getInstance().pHashDistance("2d5ad3936d2e015b", "2d6ed293db36a4fb");
138+
System.out.println(">> Distance 2: "+d2);
139+
// output: 17 (similar images)
140+
141+
int d3 = ImageKit.getInstance().pHashDistance("a4a65595ac94518b", "7838873e791f8400");
142+
System.out.println(">> Distance 3: "+d3);
143+
// output: 37 (dissimilar images)
144+
System.out.println("\n\n");
145+
}
146+
147+
148+
private static void getPurgeCacheStatus(String requestId) {
149+
System.out.println(Color.ANSI_CYAN+">> Fetching cache status:"+Color.ANSI_RESET);
150+
System.out.println(">> Sending request...");
151+
ResultCacheStatus result=ImageKit.getInstance().getPurgeCacheStatus(requestId);
152+
System.out.println(">> Request complete...");
153+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
154+
System.out.println(result);
155+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
156+
System.out.println(result.getRaw());
157+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
158+
System.out.println(result.getMap());
159+
System.out.println("\n\n");
160+
}
161+
162+
private static ResultCache purgeCache(String filePath) {
163+
System.out.println(Color.ANSI_CYAN+">> Purging cache:"+Color.ANSI_RESET);
164+
System.out.println(">> Sending request...");
165+
ResultCache result=ImageKit.getInstance().purgeCache(filePath);
166+
System.out.println(">> Request complete...");
167+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
168+
System.out.println(result);
169+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
170+
System.out.println(result.getRaw());
171+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
172+
System.out.println(result.getMap());
173+
System.out.println("\n\n");
174+
return result;
175+
}
176+
177+
private static void deleteFile(String fileId) {
178+
System.out.println(Color.ANSI_CYAN+">> Deleting file:"+Color.ANSI_RESET);
179+
System.out.println(">> Sending file id: "+fileId);
180+
Result result=ImageKit.getInstance().deleteFile(fileId);
181+
System.out.println(">> File deleted...");
182+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
183+
System.out.println(result);
184+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
185+
System.out.println(result.getRaw());
186+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
187+
System.out.println(result.getMap());
188+
System.out.println("\n\n");
189+
}
190+
191+
private static void deleteFiles(List<BaseFile> files) {
192+
List<String> fileIds=files.stream().map(baseFile -> baseFile.getFileId()).collect(Collectors.toList());
193+
System.out.println(Color.ANSI_CYAN+">> Deleting file:"+Color.ANSI_RESET);
194+
System.out.println(">> Sending file id: "+fileIds);
195+
ResultFileDelete result=ImageKit.getInstance().bulkDeleteFiles(fileIds);
196+
System.out.println(">> File deleted...");
197+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
198+
System.out.println(result);
199+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
200+
System.out.println(result.getRaw());
201+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
202+
System.out.println(result.getMap());
203+
System.out.println("\n\n");
204+
}
205+
206+
private static void updateDetails(String fileId) {
207+
System.out.println(Color.ANSI_CYAN+">> Updating file details:"+Color.ANSI_RESET);
208+
System.out.println(">> Updating file details...");
209+
List<String> tags=new ArrayList<>();
210+
tags.add("Software");
211+
tags.add("Developer");
212+
tags.add("Engineer");
213+
FileUpdateRequest fileUpdateRequest =new FileUpdateRequest(fileId);
214+
fileUpdateRequest.setTags(tags);
215+
fileUpdateRequest.setCustomCoordinates("10,10,40,40");
216+
Result result=ImageKit.getInstance().updateFileDetail(fileUpdateRequest);
217+
System.out.println(">> Updating done...");
218+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
219+
System.out.println(result);
220+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
221+
System.out.println(result.getRaw());
222+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
223+
System.out.println(result.getMap());
224+
System.out.println("\n\n");
225+
}
226+
227+
private static void getFileMetaData(String fileId) {
228+
System.out.println(Color.ANSI_CYAN+">> Get file Metadata:"+Color.ANSI_RESET);
229+
System.out.println(">> Fetching Metadata...");
230+
ResultMetaData result=ImageKit.getInstance().getFileMetadata(fileId);
231+
System.out.println(">> Fetching done...");
232+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
233+
System.out.println(result);
234+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
235+
System.out.println(result.getRaw());
236+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
237+
System.out.println(result.getMap());
238+
System.out.println("\n\n");
239+
}
240+
241+
private static void getRemoteFileMetaData(String url) {
242+
System.out.println(Color.ANSI_CYAN+">> Get Remote file Metadata:"+Color.ANSI_RESET);
243+
System.out.println(">> Fetching Metadata...");
244+
ResultMetaData result=ImageKit.getInstance().getRemoteFileMetadata(url);
245+
System.out.println(">> Fetching done...");
246+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
247+
System.out.println(result);
248+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
249+
System.out.println(result.getRaw());
250+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
251+
System.out.println(result.getMap());
252+
System.out.println("\n\n");
253+
}
254+
255+
private static void getFileDetail(String fileId) {
256+
System.out.println(Color.ANSI_CYAN+">> Get file details:"+Color.ANSI_RESET);
257+
System.out.println(">> Fetching details...");
258+
Result result=ImageKit.getInstance().getFileDetail(fileId);
259+
System.out.println(">> Fetching done...");
260+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
261+
System.out.println(result);
262+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
263+
System.out.println(result.getRaw());
264+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
265+
System.out.println(result.getMap());
266+
System.out.println("\n\n");
267+
}
268+
269+
private static List<BaseFile> getList(int skip, int limit) {
270+
System.out.println(Color.ANSI_CYAN+">> Get Uploaded file as List:"+Color.ANSI_RESET);
271+
System.out.println(">> Fetching list...");
272+
Map<String , String> options=new HashMap<>();
273+
options.put("skip",""+skip);
274+
options.put("limit", ""+limit);
275+
ResultList resultList=ImageKit.getInstance().getFileList(options);
276+
System.out.println(">> Fetching done...");
277+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
278+
System.out.println(">> No. of files in server: "+resultList.getResults().size());
279+
System.out.println(">> FileIds: "+resultList.getResults().stream().map(baseFile -> baseFile.getFileId()).collect(Collectors.toList()));
280+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
281+
System.out.println(resultList.getRaw());
282+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
283+
System.out.println(resultList.getMap());
284+
System.out.println("\n\n");
285+
return resultList.getResults();
286+
}
287+
288+
private static void uploadFromURL() {
289+
System.out.println(Color.ANSI_CYAN+">> Uploading from URL:"+Color.ANSI_RESET);
290+
System.out.println(">> Start uploading...");
291+
String imageUrl="https://homepages.cae.wisc.edu/~ece533/images/cat.png";
292+
URL url= null;
293+
try {
294+
url = URI.create(imageUrl).toURL();
295+
} catch (MalformedURLException e) {
296+
e.printStackTrace();
297+
}
298+
FileCreateRequest fileCreateRequest = new FileCreateRequest(url, "sample-image.jpg");
299+
fileCreateRequest.setFolder("demo1");
300+
String customCoordinates="10,10,20,20";
301+
fileCreateRequest.setCustomCoordinates(customCoordinates);
302+
List<String> tags=new ArrayList<>();
303+
tags.add("Software");
304+
tags.add("Developer");
305+
tags.add("Engineer");
306+
fileCreateRequest.setTags(tags);
307+
308+
List<String> responseFields=new ArrayList<>();
309+
responseFields.add("thumbnail");
310+
responseFields.add("tags");
311+
responseFields.add("customCoordinates");
312+
313+
fileCreateRequest.setResponseFields(responseFields);
314+
System.out.println(">> Ref: URL= "+imageUrl);
315+
Result result = ImageKit.getInstance().upload(fileCreateRequest);
316+
System.out.println(">> Uploading done.");
317+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
318+
System.out.println(result);
319+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
320+
System.out.println(result.getRaw());
321+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
322+
System.out.println(result.getMap());
323+
System.out.println("\n\n");
324+
}
325+
326+
private static void uploadFromBase64() {
327+
System.out.println(Color.ANSI_CYAN+">> Uploading Base64 Image:"+Color.ANSI_RESET);
328+
System.out.println(">> Start uploading...");
329+
URL url = App.class.getClassLoader().getResource("sample1.jpg");
330+
File file=new File(url.getPath());
331+
String base64= Utils.fileToBase64(file);
332+
FileCreateRequest fileCreateRequest =new FileCreateRequest(base64, "sample_base64_image.jpg");
333+
Result result = ImageKit.getInstance().upload(fileCreateRequest);
334+
System.out.println(">> Uploading done.");
335+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
336+
System.out.println(result);
337+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
338+
System.out.println(result.getRaw());
339+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
340+
System.out.println(result.getMap());
341+
System.out.println("\n\n");
342+
}
343+
344+
private static void uploadFromBytes() {
345+
System.out.println(Color.ANSI_CYAN+">> Uploading Image from file:"+Color.ANSI_RESET);
346+
System.out.println(">> Start uploading...");
347+
URL url = App.class.getClassLoader().getResource("sample1.jpg");
348+
File file=new File(url.getPath());
349+
byte[] bytes= Utils.fileToBytes(file);
350+
FileCreateRequest fileCreateRequest =new FileCreateRequest(bytes, "sample_image.jpg");
351+
fileCreateRequest.setUseUniqueFileName(false);
352+
Result result = ImageKit.getInstance().upload(fileCreateRequest);
353+
System.out.println(">> Uploading done.");
354+
System.out.println(Color.ANSI_GREEN+">> Response:"+Color.ANSI_RESET);
355+
System.out.println(result);
356+
System.out.println(Color.ANSI_GREEN+">> Raw Response:"+Color.ANSI_RESET);
357+
System.out.println(result.getRaw());
358+
System.out.println(Color.ANSI_GREEN+">> Map Response:"+Color.ANSI_RESET);
359+
System.out.println(result.getMap());
360+
System.out.println("\n\n");
361+
}
362+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.imagekit.sampleapp;
2+
3+
public class Color {
4+
public static final String ANSI_RESET = "\u001B[0m";
5+
public static final String ANSI_BLACK = "\u001B[30m";
6+
public static final String ANSI_RED = "\u001B[31m";
7+
public static final String ANSI_GREEN = "\u001B[32m";
8+
public static final String ANSI_YELLOW = "\u001B[33m";
9+
public static final String ANSI_BLUE = "\u001B[34m";
10+
public static final String ANSI_PURPLE = "\u001B[35m";
11+
public static final String ANSI_CYAN = "\u001B[36m";
12+
public static final String ANSI_WHITE = "\u001B[37m";
13+
public static final String ANSI_BLACK_BACKGROUND = "\u001B[40m";
14+
public static final String ANSI_RED_BACKGROUND = "\u001B[41m";
15+
public static final String ANSI_GREEN_BACKGROUND = "\u001B[42m";
16+
public static final String ANSI_YELLOW_BACKGROUND = "\u001B[43m";
17+
public static final String ANSI_BLUE_BACKGROUND = "\u001B[44m";
18+
public static final String ANSI_PURPLE_BACKGROUND = "\u001B[45m";
19+
public static final String ANSI_CYAN_BACKGROUND = "\u001B[46m";
20+
public static final String ANSI_WHITE_BACKGROUND = "\u001B[47m";
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copy this sample file add rename it to "config.properties"
2+
# Put essential values of keys [UrlEndpoint, PrivateKey, PublicKey]
3+
4+
UrlEndpoint=<-YOUR-ENDPOINT-URL-HERE->
5+
PrivateKey=<-YOUR-PRIVATE-KEY-HERE->
6+
PublicKey=<-YOUR-PUBLIC-KEY-HERE->

src/main/resources/sample1.jpg

49.9 KB
Loading

0 commit comments

Comments
 (0)