Skip to content

Commit d44800a

Browse files
committed
new APIs wrapper functions and sample added
1 parent 804e41e commit d44800a

File tree

5 files changed

+301
-3
lines changed

5 files changed

+301
-3
lines changed

constants/errorMessages.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ module.exports = {
1414
"MISSING_UPLOAD_DATA" : { message : "Missing data for upload", help : "" },
1515
"MISSING_UPLOAD_FILE_PARAMETER" : { message : "Missing file parameter for upload", help : "" },
1616
"MISSING_UPLOAD_FILENAME_PARAMETER" : { message : "Missing fileName parameter for upload", help : "" },
17+
"JOB_ID_MISSING" : { message : "Missing Job ID parameter for this request", help : "" },
18+
"INVALID_DIRECTORY_PATH" : { messages : "Invalid file/folder path for this request", help : "Path should be a string like '/path/of/folder', '/path/of/file.jpg'"},
19+
"INVALID_FOLDER_NAME": { messages : "Invalid folder name for this request" , help : ""},
1720
// pHash errors
1821
"INVALID_PHASH_VALUE": { message: "Invalid pHash value", help: "Both pHash strings must be valid hexadecimal numbers" },
1922
"MISSING_PHASH_VALUE": { message: "Missing pHash value", help: "Please pass two pHash values" },

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ var ImageKit = function(opts) {
151151

152152
// Copy folder API
153153
this.copyFolder = promisify(function(sourceFolderPath, destinationPath, callback) {
154-
return manage.copyFile(sourceFolderPath, destinationPath, this.options, callback);
154+
return manage.copyFolder(sourceFolderPath, destinationPath, this.options, callback);
155155
});
156156

157157
// Move folder API

libs/manage/file.js

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,209 @@ module.exports.bulkAddTags = function(fileIdArray, tags, defaultOptions, callbac
161161
json: data
162162
}
163163

164+
request(requestOptions, defaultOptions, callback);
165+
};
166+
167+
/*
168+
Remove tags in bulk
169+
*/
170+
module.exports.bulkRemoveTags = function(fileIdArray, tags, defaultOptions, callback) {
171+
172+
if(!Array.isArray(fileIdArray)
173+
|| fileIdArray.length === 0
174+
|| fileIdArray.filter(fileId => typeof(fileId) !== 'string').length > 0) {
175+
respond(true, errorMessages.INVALID_FILEIDS_VALUE, callback);
176+
return;
177+
}
178+
179+
if(!Array.isArray(tags)
180+
|| tags.length === 0
181+
|| tags.filter(tag => typeof(tag) !== 'string').length > 0) {
182+
respond(true, errorMessages.BULK_ADD_TAGS_INVALID, callback);
183+
return;
184+
}
185+
186+
const data = {
187+
fileIds: fileIdArray,
188+
tags: tags
189+
}
190+
191+
const requestOptions = {
192+
url: "https://api.imagekit.io/v1/files/removeTags",
193+
method: "POST",
194+
json: data
195+
}
196+
197+
request(requestOptions, defaultOptions, callback);
198+
};
199+
200+
/*
201+
Copy file
202+
*/
203+
module.exports.copyFile = function(sourceFilePath, destinationPath, defaultOptions, callback) {
204+
205+
if(sourceFilePath.length === 0 || typeof(sourceFilePath) !== 'string'
206+
|| destinationPath.length === 0 || typeof(destinationPath) !== 'string') {
207+
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
208+
return;
209+
}
210+
211+
const data = {
212+
sourceFilePath: sourceFilePath,
213+
destinationPath: destinationPath
214+
}
215+
216+
const requestOptions = {
217+
url: "https://api.imagekit.io/v1/files/copy",
218+
method: "POST",
219+
json: data
220+
}
221+
222+
request(requestOptions, defaultOptions, callback);
223+
};
224+
225+
/*
226+
Move file
227+
*/
228+
module.exports.moveFile = function(sourceFilePath, destinationPath, defaultOptions, callback) {
229+
230+
if(sourceFilePath.length === 0 || typeof(sourceFilePath) !== 'string'
231+
|| destinationPath.length === 0 || typeof(destinationPath) !== 'string') {
232+
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
233+
return;
234+
}
235+
236+
const data = {
237+
sourceFilePath: sourceFilePath,
238+
destinationPath: destinationPath
239+
}
240+
241+
const requestOptions = {
242+
url: "https://api.imagekit.io/v1/files/move",
243+
method: "POST",
244+
json: data
245+
}
246+
247+
request(requestOptions, defaultOptions, callback);
248+
};
249+
250+
/*
251+
Copy Folder
252+
*/
253+
module.exports.copyFolder = function(sourceFolderPath, destinationPath, defaultOptions, callback) {
254+
255+
if(sourceFolderPath.length === 0 || typeof(sourceFolderPath) !== 'string'
256+
|| destinationPath.length === 0 || typeof(destinationPath) !== 'string') {
257+
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
258+
return;
259+
}
260+
261+
const data = {
262+
sourceFolderPath: sourceFolderPath,
263+
destinationPath: destinationPath
264+
}
265+
266+
const requestOptions = {
267+
url: "https://api.imagekit.io/v1/bulkJobs/copyFolder",
268+
method: "POST",
269+
json: data
270+
}
271+
272+
request(requestOptions, defaultOptions, callback);
273+
};
274+
275+
/*
276+
Move Folder
277+
*/
278+
module.exports.moveFolder = function(sourceFolderPath, destinationPath, defaultOptions, callback) {
279+
280+
if(sourceFolderPath.length === 0 || typeof(sourceFolderPath) !== 'string'
281+
|| destinationPath.length === 0 || typeof(destinationPath) !== 'string') {
282+
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
283+
return;
284+
}
285+
286+
const data = {
287+
sourceFolderPath: sourceFolderPath,
288+
destinationPath: destinationPath
289+
}
290+
291+
const requestOptions = {
292+
url: "https://api.imagekit.io/v1/bulkJobs/moveFolder",
293+
method: "POST",
294+
json: data
295+
}
296+
297+
request(requestOptions, defaultOptions, callback);
298+
};
299+
300+
/*
301+
Create folder
302+
*/
303+
module.exports.createFolder = function(folderName, parentFolderPath, defaultOptions, callback) {
304+
305+
if(folderName.length === 0 || typeof(folderName) !== 'string') {
306+
respond(true, errorMessages.INVALID_FOLDER_NAME, callback);
307+
return;
308+
}
309+
310+
if(parentFolderPath.length === 0 || typeof(parentFolderPath) !== 'string') {
311+
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
312+
return;
313+
}
314+
315+
const data = {
316+
folderName: folderName,
317+
parentFolderPath: parentFolderPath
318+
}
319+
320+
const requestOptions = {
321+
url: "https://api.imagekit.io/v1/folder/",
322+
method: "POST",
323+
json: data
324+
}
325+
326+
request(requestOptions, defaultOptions, callback);
327+
};
328+
329+
/*
330+
Delete folder
331+
*/
332+
module.exports.deleteFolder = function(folderPath, defaultOptions, callback) {
333+
334+
if(folderPath.length === 0 || typeof(folderPath) !== 'string') {
335+
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
336+
return;
337+
}
338+
339+
const data = {
340+
folderPath: folderPath
341+
}
342+
343+
const requestOptions = {
344+
url: "https://api.imagekit.io/v1/folder",
345+
method: "DELETE",
346+
json: data
347+
}
348+
349+
request(requestOptions, defaultOptions, callback);
350+
};
351+
352+
/*
353+
Bulk job status
354+
*/
355+
module.exports.getBulkJobStatus = function(jobId, defaultOptions) {
356+
357+
if(!jobId) {
358+
respond(true, errorMessages.JOB_ID_MISSING, callback);
359+
return;
360+
}
361+
362+
const requestOptions = {
363+
url : "https://api.imagekit.io/v1/bulkJobs/" + jobId,
364+
method : "GET",
365+
json : true
366+
};
367+
164368
request(requestOptions, defaultOptions, callback);
165369
};

libs/manage/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,14 @@ module.exports.updateFileDetails = file.updateDetails;
77
module.exports.getFileMetadata = file.getMetadata;
88
module.exports.deleteFile = file.deleteFile;
99
module.exports.bulkDeleteFiles = file.bulkDeleteFiles;
10+
module.exports.bulkAddTags = file.bulkAddTags;
11+
module.exports.bulkRemoveTags = file.bulkRemoveTags;
12+
module.exports.copyFile = file.copyFile;
13+
module.exports.moveFile = file.moveFile;
14+
module.exports.copyFolder = file.copyFolder;
15+
module.exports.moveFolder = file.moveFolder;
16+
module.exports.createFolder = file.createFolder;
17+
module.exports.deleteFolder = file.deleteFolder;
18+
module.exports.getBulkJobStatus = file.getBulkJobStatus;
1019
module.exports.purgeCache = cache.purgeCache;
1120
module.exports.getPurgeCacheStatus = cache.getPurgeCacheStatus;

sample/index.js

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,51 @@ const sampleApp = async () => {
8484
console.log("Purge Response: ", JSON.stringify(purgeStatus, undefined, 2), "\n");
8585

8686

87+
// Bulk add tags
88+
let fileIds = filesList.map(file => file.fileId);
89+
fileIds.shift();
90+
tags = ['red', 'blue'];
91+
const bulkAddTagsResponse = await bulkAddTags(imagekit, fileIds, tags);
92+
console.log("Bulk add tags response: ", bulkAddTagsResponse, "\n");
93+
94+
// Bulk remove tags
95+
const bulkRemoveTagsResponse = await bulkRemoveTags(imagekit, fileIds, tags);
96+
console.log("Bulk remove tags response: ", bulkRemoveTagsResponse, "\n");
97+
98+
// Create folder
99+
const createFolderResponse_1 = await createFolder(imagekit, "folder1", "/");
100+
const createFolderResponse_2 = await createFolder(imagekit, "folder2", "/");
101+
console.log("Folder creation response: ", createFolderResponse_2, "\n");
102+
103+
// Copy file
104+
const copyFileResponse = await copyFile(imagekit, fileDetails_1.filePath, "/folder1/");
105+
console.log("File copy response: ", copyFileResponse, "\n");
106+
107+
// Move file
108+
const moveFileResponse = await moveFile(imagekit, `/folder1/${fileDetails_1.filePath}`, "/folder2/");
109+
console.log("File move response: ", moveFileResponse, "\n");
110+
111+
// Copy folder
112+
const copyFolderResponse = await copyFolder(imagekit, "/folder2", "/folder1/");
113+
console.log("Copy folder response: ", JSON.stringify(copyFolderResponse, undefined, 2), "\n");
114+
115+
// Move folder
116+
const moveFolderResponse = await moveFolder(imagekit, "/folder1", "/folder2/");
117+
console.log("Move folder response: ", JSON.stringify(moveFolderResponse, undefined, 2),"\n");
118+
119+
// Get bulk job status
120+
const getBulkJobStatusResponse = await getBulkJobStatus(imagekit, moveFolderResponse.jobId);
121+
console.log("Bulk job status response: ", JSON.stringify(getBulkJobStatusResponse), "\n");
122+
123+
// Delete folder
124+
const deleteFolderResponse = await deleteFolder(imagekit, "/folder2/");
125+
console.log("Delete folder response: ", deleteFolderResponse, "\n");
126+
87127
// Deleting Files
88128
const deleteResponse = await deleteFile(imagekit, fileDetails_1.fileId);
89129
console.log("Deletion response: ", deleteResponse, "\n");
90130

91131
// Bulk Delete Files
92-
let fileIds = filesList.map(file => file.fileId);
93-
fileIds.shift();
94132
const bulkDeleteResponse = await bulkDeleteFiles(imagekit, fileIds);
95133
console.log("Bulk deletion response: ", bulkDeleteResponse, "\n");
96134

@@ -176,5 +214,49 @@ const bulkDeleteFiles = async (imagekitInstance, fileIds) => {
176214
return "success";
177215
}
178216

217+
const bulkAddTags = async (imagekitInstance, fileIds, tags) => {
218+
const response = await imagekitInstance.bulkAddTags(fileIds, tags);
219+
return "success";
220+
}
221+
222+
const bulkRemoveTags = async (imagekitInstance, fileIds, tags) => {
223+
const response = await imagekitInstance.bulkRemoveTags(fileIds, tags);
224+
return "success";
225+
}
226+
227+
const copyFile = async (imagekitInstance, sourceFilePath, destinationPath) => {
228+
const response = await imagekitInstance.copyFile(sourceFilePath, destinationPath);
229+
return "success";
230+
}
231+
232+
const moveFile = async (imagekitInstance, sourceFilePath, destinationPath) => {
233+
const response = await imagekitInstance.moveFile(sourceFilePath, destinationPath);
234+
return "success";
235+
}
236+
237+
const copyFolder = async (imagekitInstance, sourceFolderPath, destinationPath) => {
238+
const response = await imagekitInstance.copyFolder(sourceFolderPath, destinationPath);
239+
return response;
240+
}
241+
242+
const moveFolder = async (imagekitInstance, sourceFolderPath, destinationPath) => {
243+
const response = await imagekitInstance.moveFolder(sourceFolderPath, destinationPath);
244+
return response;
245+
}
246+
247+
const createFolder = async (imagekitInstance, folderName, parentFolderPath) => {
248+
const response = await imagekitInstance.createFolder(folderName, parentFolderPath);
249+
return "success";
250+
}
251+
252+
const deleteFolder = async (imagekitInstance, folderPath) => {
253+
const response = await imagekitInstance.deleteFolder(folderPath);
254+
return "success";
255+
}
256+
257+
const getBulkJobStatus = async (imagekitInstance, jobId) => {
258+
const response = await imagekitInstance.getBulkJobStatus(jobId);
259+
return response;
260+
}
179261

180262
sampleApp();

0 commit comments

Comments
 (0)