Skip to content

Commit

Permalink
check file exists error handle
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshrvel committed Feb 10, 2019
1 parent 122bbb9 commit 8057006
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 26 deletions.
71 changes: 47 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class MTP {
}

listStorageDevices() {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

const storageData = {};

Expand Down Expand Up @@ -186,7 +186,7 @@ class MTP {
}

async setStorageDevices({ deviceIndex = 0 }) {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

const listStorageDevices = await this.listStorageDevices();

Expand Down Expand Up @@ -219,7 +219,7 @@ class MTP {
}

releaseDevice() {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

try {
this.mtpHelper.Release_Device(this.device);
Expand All @@ -239,6 +239,8 @@ class MTP {
}

getFileInfo({ fileId }) {
if (!this.device) return this.throwMtpError();

try {
const fileInfo = this.mtpHelper.Get_Filemetadata(this.device, fileId);

Expand All @@ -264,27 +266,48 @@ class MTP {
}

async fileExists({ fileName, parentId }) {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

if (undefinedOrNull(fileName)) {
return null;
}
try {
const {
error: listMtpFileTreeError,
data: listMtpFileTreeData
} = await this.listMtpFileTree({
folderId: parentId,
recursive: false
});

const listMtpFileTree = await this.listMtpFileTree({
folderId: parentId,
recursive: false
});
if (listMtpFileTreeError) {
return Promise.resolve({
data: null,
error: listMtpFileTreeError
});
}

const foundItem = findLodash(listMtpFileTree, { name: fileName });
if (foundItem) {
return Promise.resolve(foundItem);
}
const foundItem = findLodash(listMtpFileTreeData, { name: fileName });
if (!foundItem) {
return Promise.resolve({
data: false,
error: null
});
}

return Promise.resolve({
data: foundItem,
error: null
});
} catch (e) {
console.error(`MTP -> fileExists`, e);

return Promise.resolve(null);
return Promise.resolve({
data: null,
error: e
});
}
}

deleteFile({ fileId }) {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

try {
this.mtpHelper.Destroy_file(this.device, fileId);
Expand All @@ -304,7 +327,7 @@ class MTP {
}

async renameFile({ fileId, currentFileName, newfileName }) {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

try {
const file = new this.mtpHelper.file_t();
Expand Down Expand Up @@ -346,7 +369,7 @@ class MTP {
}

createFolder({ folderPath, parentId }) {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

return Promise.resolve(
this.mtpHelper.Create_Folder(
Expand All @@ -364,7 +387,7 @@ class MTP {
fileTreeStructure = [],
parentPath = ''
}) {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

try {
const files = this.mtpHelper.Get_Files_And_Folders(
Expand Down Expand Up @@ -474,7 +497,7 @@ class MTP {
}

downloadFile({ destinationFilePath, fileId }) {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

return Promise.resolve(
this.mtpHelper.Get_File_To_File(
Expand All @@ -489,7 +512,7 @@ class MTP {
}

async downloadFileTree({ nodes, destinationFilePath }) {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

for (let i = 0; i < nodes.length; i += 1) {
const item = nodes[i];
Expand Down Expand Up @@ -522,7 +545,7 @@ class MTP {
}

uploadFile({ filePath, parentId, size }) {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

const file = new this.mtpHelper.file_t();
file.size = size;
Expand All @@ -544,7 +567,7 @@ class MTP {
}

async uploadFileTree({ nodes, parentId }) {
if (this.device === null) return this.throwMtpError();
if (!this.device) return this.throwMtpError();

for (let i = 0; i < nodes.length; i += 1) {
const item = nodes[i];
Expand Down
29 changes: 27 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async function run(resetmtp = false, searchDir = null) {
* =====================================================================
* Rename File
*/
const {
/* const {
error: renameFileError,
data: renameFileData
} = await mtpObj.renameFile({
Expand All @@ -126,7 +126,7 @@ async function run(resetmtp = false, searchDir = null) {
if (renameFileError) {
console.error(renameFileError);
}
}*/

/**
* =====================================================================
Expand All @@ -143,6 +143,31 @@ async function run(resetmtp = false, searchDir = null) {
}
console.log(getFileInfoData.name);*/

/**
* =====================================================================
* File Exists
*/

/* const {
error: fileExistsError,
data: fileExistsData
} = await mtpObj.fileExists({
fileName: 'Files',
parentId: MTP_FLAGS.FILES_AND_FOLDERS_ROOT
});
if (fileExistsError) {
console.error(fileExistsError);
}
if (fileExistsData) {
console.log(fileExistsData.name);
}*/

/**
* =====================================================================
* File Exists
*/

/*const downloadFileTree = await mtpObj.downloadFileTree({
nodes: listMtpFileTree,
destinationFilePath: `~/Desktop/2`
Expand Down

0 comments on commit 8057006

Please sign in to comment.