Skip to content

Commit 64c7f18

Browse files
Manu ChaudharyManu Chaudhary
authored andcommitted
Buffer upload fix
1 parent a0af134 commit 64c7f18

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

libs/upload/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ module.exports = function(uploadOptions, defaultOptions, callback) {
2323
return;
2424
}
2525

26+
if(typeof uploadOptions.file != "string") {
27+
uploadOptions.file = {
28+
value : uploadOptions.file,
29+
options: {
30+
'filename': uploadOptions.fileName,
31+
'contentType': null
32+
}
33+
};
34+
}
35+
2636
var requestOptions = {
2737
url : "https://api.imagekit.io/v1/files/upload",
2838
method : "POST",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imagekit",
3-
"version": "3.0.4",
3+
"version": "3.0.5",
44
"description": "Offical NodeJS SDK for ImageKit.io integration",
55
"main": "index.js",
66
"scripts": {

sample/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@ const sampleApp = async () => {
1717
// Uploading images through binary
1818
let i =0;
1919
while (i < 8){
20-
const response = await uploadFileBin(imagekit, FILE_PATH, `${FILE_NAME}_bin_${i+1}`);
20+
const response = await uploadLocalFile(imagekit, FILE_PATH, `${FILE_NAME}_bin_${i+1}`);
2121
console.log(`Binary upload response # ${i+1}:`, JSON.stringify(response, undefined, 2), "\n");
2222
i++;
2323
}
2424

2525
// Uploading images with base64
2626
const uploadResponse_base64 = await uploadFileBase64(imagekit, FILE_PATH, `${FILE_NAME}_base64`);
2727
console.log(`Base64 upload response:`, JSON.stringify(uploadResponse_base64, undefined, 2), "\n");
28+
29+
// Uploading images with buffer
30+
const uploadResponse_buffer = await uploadFileBuffer(imagekit, FILE_PATH, `${FILE_NAME}_buffer`);
31+
console.log(`Buffer upload response:`, JSON.stringify(uploadResponse_buffer, undefined, 2), "\n");
2832

2933
// Uploading images with URL
3034
const uploadResponse_url = await uploadFileURL(imagekit, IMG_URL, `${FILE_NAME}_url`);
@@ -102,12 +106,18 @@ const sampleApp = async () => {
102106

103107
}
104108

105-
const uploadFileBin = async (imagekitInstance, filePath, fileName) => {
109+
const uploadLocalFile = async (imagekitInstance, filePath, fileName) => {
106110
const file = fs.createReadStream(filePath);
107111
const response = await imagekitInstance.upload({file, fileName});
108112
return response;
109113
}
110114

115+
const uploadFileBuffer = async (imagekitInstance, filePath, fileName) => {
116+
const buffer = fs.readFileSync(filePath);
117+
const response = await imagekitInstance.upload({file: buffer, fileName});
118+
return response;
119+
}
120+
111121
const uploadFileBase64 = async (imagekitInstance, filePath, fileName) => {
112122
const file_base64 = fs.readFileSync(filePath, 'base64');
113123
const response = await imagekitInstance.upload({file: file_base64, fileName});

0 commit comments

Comments
 (0)