Skip to content

Commit

Permalink
create folder error handled
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshrvel committed Feb 10, 2019
1 parent 841e63a commit 3cc4dd3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 10 deletions.
73 changes: 63 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class MTP {
RENAME_FAILED: `Some error occured while renaming`,
FILE_INFO_FAILED: `Some error occured while fetching the file information`,
DOWNLOAD_FILE_FAILED: `Some error occured while copying the file from MTP device`,
NO_FILES_COPIED: `No files were copied. Refresh your MTP`
NO_FILES_COPIED: `No files were copied. Refresh your MTP`,
CREATE_FOLDER_FAILED: `Some error occured while creating a new folder`,
CREATE_FOLDER_FILE_FAILED: `A file with a similar name exists`
};
}

Expand Down Expand Up @@ -370,17 +372,68 @@ class MTP {
}
}

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

return Promise.resolve(
this.mtpHelper.Create_Folder(
try {
const createdFolder = this.mtpHelper.Create_Folder(
this.device,
folderPath,
newFolderName,
parentId,
this.storageId
)
);
);

if (createdFolder === 0) {
const {
error: fileExistsError,
data: fileExistsData
} = await this.fileExists({
fileName: newFolderName,
parentId
});

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

if (
undefinedOrNull(fileExistsData) ||
undefinedOrNull(fileExistsData.id)
) {
return Promise.resolve({
data: null,
error: this.ERR.CREATE_FOLDER_FAILED
});
}

if (!fileExistsData.isFolder) {
return Promise.resolve({
data: null,
error: this.ERR.CREATE_FOLDER_FILE_FAILED
});
}

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

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

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

async listMtpFileTree({
Expand Down Expand Up @@ -537,7 +590,7 @@ class MTP {
rootNode = false,
nodes,
destinationFilePath,
callback = _ => {}
callback
}) {
if (!this.device) return this.throwMtpError();

Expand Down Expand Up @@ -646,13 +699,13 @@ class MTP {

if (item.isFolder) {
newId = await this.createFolder({
folderPath: item.name,
newFolderName: item.name,
parentId
});

if (newId === 0) {
const _folderExists = await this.fileExists({
fileName: path.basename(item.name),
fileName: item.name,
parentId
});

Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,26 @@ async function run(resetmtp = false, searchDir = null) {
console.log(fileExistsData.name);
}*/

/**
* =====================================================================
* Create Folder
*/
/*
const {
error: createFolderError,
data: createFolderData
} = await mtpObj.createFolder({
newFolderName: 'ABCD',
parentId: MTP_FLAGS.FILES_AND_FOLDERS_ROOT
});
if (createFolderError) {
console.error(createFolderError);
}
if (createFolderData) {
console.log(createFolderData);
}*/

/**
* =====================================================================
* Download file tree
Expand Down

0 comments on commit 3cc4dd3

Please sign in to comment.