Skip to content

Commit 51e9f87

Browse files
author
Jagveer
committed
Added more upload method in the sample app
1 parent ce5f26b commit 51e9f87

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

sample/index.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,42 @@ const ImageKit = require("../index");
22
const fs = require("fs");
33
const path = require("path");
44

5+
// const CONFIG_OPTIONS = {
6+
// publicKey : "your_public_api_key",
7+
// privateKey : "your_private_api_key",
8+
// urlEndpoint : "https://ik.imagekit.io/your_imagekit_id/"
9+
// }
10+
511
const CONFIG_OPTIONS = {
6-
publicKey : "your_public_api_key",
7-
privateKey : "your_private_api_key",
8-
urlEndpoint : "https://ik.imagekit.io/your_imagekit_id/"
9-
}
12+
publicKey: '81shyFkcqoR/It6sCR1P845UtCY=',
13+
privateKey: 'qqHhrm/7WtoRcMuIORD2+75uPOA=',
14+
urlEndpoint: 'https://ik.imagekit.io/s11xanjcm/'
15+
};
1016

11-
const FILE_PATH = path.resolve(__dirname, "./test_image.jpg"), FILE_NAME = "test_image";
17+
const FILE_PATH = path.resolve(__dirname, "./test_image.jpg"), FILE_NAME = "test_image", IMG_URL = "https://images.pexels.com/photos/247676/pexels-photo-247676.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260";
1218

1319
const sampleApp = async () => {
1420
try {
1521
const imagekit = new ImageKit(CONFIG_OPTIONS);
1622

17-
// Uploading images
23+
// Uploading images through binary
1824
let i =0;
19-
while (i < 10){
20-
const response = await uploadFile(imagekit, FILE_PATH, `${FILE_NAME}_${i+1}`);
21-
console.log(`Upload response # ${i+1}:`, JSON.stringify(response, undefined, 2), "\n");
25+
while (i < 8){
26+
const response = await uploadFileBin(imagekit, FILE_PATH, `${FILE_NAME}_bin_${i+1}`);
27+
console.log(`Binary upload response # ${i+1}:`, JSON.stringify(response, undefined, 2), "\n");
2228
i++;
2329
}
2430

31+
// Uploading images with base64
32+
const uploadResponse_base64 = await uploadFileBase64(imagekit, FILE_PATH, `${FILE_NAME}_base64`);
33+
console.log(`Base64 upload response:`, JSON.stringify(uploadResponse_base64, undefined, 2), "\n");
34+
35+
// Uploading images with URL
36+
const uploadResponse_url = await uploadFileURL(imagekit, IMG_URL, `${FILE_NAME}_url`);
37+
console.log(`URL upload response:`, JSON.stringify(uploadResponse_url, undefined, 2), "\n");
38+
2539
// Listing Files
26-
const filesList = await listFiles(imagekit, 10, 0);
40+
const filesList = await listFiles(imagekit, 12, 0);
2741
console.log("List of first 10 files: ", JSON.stringify(filesList, undefined, 2), "\n");
2842

2943
// Generating URLs
@@ -83,12 +97,23 @@ const sampleApp = async () => {
8397

8498
}
8599

86-
const uploadFile = async (imagekitInstance, filePath, fileName) => {
100+
const uploadFileBin = async (imagekitInstance, filePath, fileName) => {
87101
const file = fs.createReadStream(filePath);
88102
const response = await imagekitInstance.upload({file, fileName});
89103
return response;
90104
}
91105

106+
const uploadFileBase64 = async (imagekitInstance, filePath, fileName) => {
107+
const file_base64 = fs.readFileSync(filePath, 'base64');
108+
const response = await imagekitInstance.upload({file: file_base64, fileName});
109+
return response;
110+
}
111+
112+
const uploadFileURL = async (imagekitInstance, url, fileName) => {
113+
const response = await imagekitInstance.upload({file: url, fileName});
114+
return response;
115+
}
116+
92117
const listFiles = async (imagekitInstance, limit=10, skip=0) => {
93118
const response = await imagekitInstance.listFiles({
94119
limit,
@@ -137,11 +162,4 @@ const bulkDeleteFiles = async (imagekitInstance, fileIds) => {
137162
}
138163

139164

140-
sampleApp();
141-
142-
143-
144-
145-
146-
// //Generating Authentication token
147-
//
165+
sampleApp();

sample/test_image.jpg

100755100644
161 KB
Loading

0 commit comments

Comments
 (0)