Skip to content

Commit a550024

Browse files
committed
file/folder error message added, example error fixed
1 parent e7414fb commit a550024

File tree

4 files changed

+63
-32
lines changed

4 files changed

+63
-32
lines changed

constants/errorMessages.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ module.exports = {
1515
"MISSING_UPLOAD_FILE_PARAMETER" : { message : "Missing file parameter for upload", help : "" },
1616
"MISSING_UPLOAD_FILENAME_PARAMETER" : { message : "Missing fileName parameter for upload", help : "" },
1717
"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'"},
18+
"INVALID_FOLDER_PATH" : { messages : "Invalid folder path for this request", help : "Path should be a string like '/path/to/folder'"},
19+
"INVALID_FILE_PATH" : { messages : "Invalid file path for this request", help : "Path should be a string like /path/to/file.jpg'"},
1920
"INVALID_FOLDER_NAME": { messages : "Invalid folder name for this request" , help : ""},
2021
// pHash errors
2122
"INVALID_PHASH_VALUE": { message: "Invalid pHash value", help: "Both pHash strings must be valid hexadecimal numbers" },
2223
"MISSING_PHASH_VALUE": { message: "Missing pHash value", help: "Please pass two pHash values" },
2324
"UNEQUAL_STRING_LENGTH": { message: "Unequal pHash string length", help: "For distance calucation, the two pHash strings must have equal length" },
2425
//bulk delete errors
25-
"INVALID_FILEIDS_VALUE": {message: "Invalid value for fileIds", help: "fileIds should be an string array of fileId of the files to delete. The array should have atleast one fileId.",},
26+
"INVALID_FILEIDS_VALUE": {message: "Invalid value for fileIds", help: "fileIds should be an string array of fileId of the files to delete. The array should have atleast one fileId."},
2627
"BULK_ADD_TAGS_INVALID": {message: "Invalid value for tags", help: "tags should be a non empty array of string like ['tag1', 'tag2']."}
2728
};

libs/manage/file.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,13 @@ module.exports.bulkRemoveTags = function(fileIdArray, tags, defaultOptions, call
202202
*/
203203
module.exports.copyFile = function(sourceFilePath, destinationPath, defaultOptions, callback) {
204204

205-
if(typeof(destinationPath) !== 'string' || typeof(sourceFilePath) !== 'string'
206-
|| destinationPath.length === 0 || sourceFilePath.length === 0) {
207-
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
205+
if(typeof(sourceFilePath) !== 'string' || sourceFilePath.length === 0) {
206+
respond(true, errorMessages.INVALID_FILE_PATH, callback);
207+
return;
208+
}
209+
210+
if(typeof(destinationPath) !== 'string' || destinationPath.length === 0) {
211+
respond(true, errorMessages.INVALID_FOLDER_PATH, callback);
208212
return;
209213
}
210214

@@ -227,9 +231,13 @@ module.exports.copyFile = function(sourceFilePath, destinationPath, defaultOptio
227231
*/
228232
module.exports.moveFile = function(sourceFilePath, destinationPath, defaultOptions, callback) {
229233

230-
if(typeof(destinationPath) !== 'string' || typeof(sourceFilePath) !== 'string'
231-
|| destinationPath.length === 0 || sourceFilePath.length === 0) {
232-
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
234+
if(typeof(sourceFilePath) !== 'string' || sourceFilePath.length === 0) {
235+
respond(true, errorMessages.INVALID_FILE_PATH, callback);
236+
return;
237+
}
238+
239+
if(typeof(destinationPath) !== 'string' || destinationPath.length === 0) {
240+
respond(true, errorMessages.INVALID_FOLDER_PATH, callback);
233241
return;
234242
}
235243

@@ -254,7 +262,7 @@ module.exports.copyFolder = function(sourceFolderPath, destinationPath, defaultO
254262

255263
if(typeof(destinationPath) !== 'string' || typeof(sourceFolderPath) !== 'string'
256264
|| destinationPath.length === 0 || sourceFolderPath.length === 0) {
257-
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
265+
respond(true, errorMessages.INVALID_FOLDER_PATH, callback);
258266
return;
259267
}
260268

@@ -279,7 +287,7 @@ module.exports.moveFolder = function(sourceFolderPath, destinationPath, defaultO
279287

280288
if(typeof(destinationPath) !== 'string' || typeof(sourceFolderPath) !== 'string'
281289
|| destinationPath.length === 0 || sourceFolderPath.length === 0) {
282-
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
290+
respond(true, errorMessages.INVALID_FOLDER_PATH, callback);
283291
return;
284292
}
285293

@@ -308,7 +316,7 @@ module.exports.createFolder = function(folderName, parentFolderPath, defaultOpti
308316
}
309317

310318
if(typeof(parentFolderPath) !== 'string' || parentFolderPath.length === 0) {
311-
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
319+
respond(true, errorMessages.INVALID_FOLDER_PATH, callback);
312320
return;
313321
}
314322

@@ -318,7 +326,7 @@ module.exports.createFolder = function(folderName, parentFolderPath, defaultOpti
318326
}
319327

320328
const requestOptions = {
321-
url: "https://api.imagekit.io/v1/folder/",
329+
url: "https://api.imagekit.io/v1/folder",
322330
method: "POST",
323331
json: data
324332
}
@@ -332,7 +340,7 @@ module.exports.createFolder = function(folderName, parentFolderPath, defaultOpti
332340
module.exports.deleteFolder = function(folderPath, defaultOptions, callback) {
333341

334342
if(typeof(folderPath) !== 'string' || folderPath.length === 0) {
335-
respond(true, errorMessages.INVALID_DIRECTORY_PATH, callback);
343+
respond(true, errorMessages.INVALID_FOLDER_PATH, callback);
336344
return;
337345
}
338346

sample/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const sampleApp = async () => {
105105
console.log("File copy response: ", copyFileResponse, "\n");
106106

107107
// Move file
108-
const moveFileResponse = await moveFile(imagekit, `/folder1/${fileDetails_1.filePath}`, "/folder2/");
108+
const moveFileResponse = await moveFile(imagekit, `/folder1${fileDetails_1.filePath}`, "/folder2/");
109109
console.log("File move response: ", moveFileResponse, "\n");
110110

111111
// Copy folder

tests/mediaLibrary.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,45 @@ describe("Media library APIs", function () {
8686
});
8787
});
8888

89-
it('Copy file invalid path', function (done) {
90-
sourceFilePath = "/";
89+
it('Copy file invalid folder path', function (done) {
90+
sourceFilePath = "/file.jpg";
9191
imagekit.copyFile(sourceFilePath, null, function (err, response) {
9292
expect(err).to.deep.equal({
93-
messages : "Invalid file/folder path for this request",
94-
help : "Path should be a string like '/path/of/folder', '/path/of/file.jpg'"
93+
messages : "Invalid folder path for this request",
94+
help : "Path should be a string like '/path/to/folder'"
9595
})
9696
done();
9797
});
9898
});
9999

100-
it('Move file invalid path', function (done) {
101-
sourceFilePath = "/";
100+
it('Copy file invalid file path', function (done) {
101+
destinationPath = "/";
102+
imagekit.copyFile(null, destinationPath, function (err, response) {
103+
expect(err).to.deep.equal({
104+
messages : "Invalid file path for this request",
105+
help : "Path should be a string like /path/to/file.jpg'"
106+
})
107+
done();
108+
});
109+
});
110+
111+
it('Move file invalid folder path', function (done) {
112+
sourceFilePath = "/file.jpg";
102113
imagekit.moveFile(sourceFilePath, null, function (err, response) {
103114
expect(err).to.deep.equal({
104-
messages : "Invalid file/folder path for this request",
105-
help : "Path should be a string like '/path/of/folder', '/path/of/file.jpg'"
115+
messages : "Invalid folder path for this request",
116+
help : "Path should be a string like '/path/to/folder'"
117+
})
118+
done();
119+
});
120+
});
121+
122+
it('Move file invalid file path', function (done) {
123+
destinationPath = "/";
124+
imagekit.moveFile(null, destinationPath, function (err, response) {
125+
expect(err).to.deep.equal({
126+
messages : "Invalid file path for this request",
127+
help : "Path should be a string like /path/to/file.jpg'"
106128
})
107129
done();
108130
});
@@ -112,8 +134,8 @@ describe("Media library APIs", function () {
112134
sourceFolderPath = "/";
113135
imagekit.copyFolder(sourceFolderPath, null, function (err, response) {
114136
expect(err).to.deep.equal({
115-
messages : "Invalid file/folder path for this request",
116-
help : "Path should be a string like '/path/of/folder', '/path/of/file.jpg'"
137+
messages : "Invalid folder path for this request",
138+
help : "Path should be a string like '/path/to/folder'"
117139
})
118140
done();
119141
});
@@ -123,8 +145,8 @@ describe("Media library APIs", function () {
123145
sourceFolderPath = "/";
124146
imagekit.moveFolder(sourceFolderPath, null, function (err, response) {
125147
expect(err).to.deep.equal({
126-
messages : "Invalid file/folder path for this request",
127-
help : "Path should be a string like '/path/of/folder', '/path/of/file.jpg'"
148+
messages : "Invalid folder path for this request",
149+
help : "Path should be a string like '/path/to/folder'"
128150
})
129151
done();
130152
});
@@ -147,8 +169,8 @@ describe("Media library APIs", function () {
147169
parentFolderPath = "";
148170
imagekit.createFolder(folderName, parentFolderPath, function (err, response) {
149171
expect(err).to.deep.equal({
150-
messages : "Invalid file/folder path for this request",
151-
help : "Path should be a string like '/path/of/folder', '/path/of/file.jpg'"
172+
messages : "Invalid folder path for this request",
173+
help : "Path should be a string like '/path/to/folder'"
152174
})
153175
done();
154176
});
@@ -157,8 +179,8 @@ describe("Media library APIs", function () {
157179
it('Delete folder invalid path', function (done) {
158180
imagekit.deleteFolder(null, function (err, response) {
159181
expect(err).to.deep.equal({
160-
messages : "Invalid file/folder path for this request",
161-
help : "Path should be a string like '/path/of/folder', '/path/of/file.jpg'"
182+
messages : "Invalid folder path for this request",
183+
help : "Path should be a string like '/path/to/folder'"
162184
})
163185
done();
164186
});
@@ -630,7 +652,7 @@ describe("Media library APIs", function () {
630652
var parentFolderPath = "/";
631653

632654
const scope = nock('https://api.imagekit.io')
633-
.post('/v1/folder/')
655+
.post('/v1/folder')
634656
.basicAuth({ user: initializationParams.privateKey, pass: '' })
635657
.reply(201, dummyAPISuccessResponse)
636658

@@ -933,7 +955,7 @@ describe("Media library APIs", function () {
933955
var parentFolderPath = "/";
934956

935957
const scope = nock('https://api.imagekit.io')
936-
.post('/v1/folder/')
958+
.post('/v1/folder')
937959
.basicAuth({ user: initializationParams.privateKey, pass: '' })
938960
.reply(500, dummyAPIErrorResponse)
939961

0 commit comments

Comments
 (0)