Skip to content

Commit

Permalink
listLocalFileTree error handled
Browse files Browse the repository at this point in the history
  • Loading branch information
ganeshrvel committed Feb 10, 2019
1 parent cf89134 commit 5b1578c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 37 deletions.
69 changes: 40 additions & 29 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,45 +342,56 @@ class MTP {
}
}

_listLocalFileTree({
async listLocalFileTree({
folderPath,
recursive = false,
fileTreeStructure = []
}) {
fs.readdirSync(folderPath).forEach(file => {
if (!junk.is(file)) {
const fullPath = path.join(folderPath, file);
const stats = fs.lstatSync(fullPath);
const size = stats.size;
const isFolder = stats.isDirectory();
const fileInfo = {
id: quickHash(fullPath),
name: file,
size,
isFolder,
path: fullPath,
children: []
};

if (isWritable(fullPath)) {
const lastIndex = fileTreeStructure.push(fileInfo) - 1;
try {
const files = fs.readdirSync(folderPath);
for (let i = 0; i < files.length; i += 1) {
const file = files[i];

if (isFolder && recursive) {
this._listLocalFileTree({
folderPath: fullPath,
recursive,
fileTreeStructure: fileTreeStructure[lastIndex].children
});
if (!junk.is(file)) {
const fullPath = path.join(folderPath, file);
const stats = fs.lstatSync(fullPath);
const size = stats.size;
const isFolder = stats.isDirectory();
const fileInfo = {
id: quickHash(fullPath),
name: file,
size,
isFolder,
path: fullPath,
children: []
};

if (isWritable(fullPath)) {
const lastIndex = fileTreeStructure.push(fileInfo) - 1;

if (isFolder && recursive) {
await this.listLocalFileTree({
folderPath: fullPath,
recursive,
fileTreeStructure: fileTreeStructure[lastIndex].children
});
}
}
}
}
});

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

listLocalFileTree({ ...args }) {
return Promise.resolve(this._listLocalFileTree({ ...args }));
return Promise.resolve({
data: null,
error: e
});
}
}

downloadFile({ destinationFilePath, fileId }) {
Expand Down
37 changes: 29 additions & 8 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async function run(resetmtp = false, searchDir = null) {

if (setStorageDevicesError) {
console.error(setStorageDevicesError);
return;
}

if (resetmtp) {
Expand All @@ -43,27 +44,47 @@ async function run(resetmtp = false, searchDir = null) {
});

if (listMtpFileTreeError) {
console.log(listMtpFileTreeError);
console.error(listMtpFileTreeError);
return;
}

if (searchDir) {
console.log(findLodash(listMtpFileTreeData, { name: searchDir }));
console.error(findLodash(listMtpFileTreeData, { name: searchDir }));
return;
}

console.log(listMtpFileTreeData);
console.error(listMtpFileTreeData);
return;
}

/*const listMtpFileTree = await mtpObj.listMtpFileTree({
folderId: 49,
/*const {
error: listMtpFileTreeError,
data: listMtpFileTreeData
} = await mtpObj.listMtpFileTree({
folderId: 34,
recursive: true,
parentPath: '/'
});*/
});
//console.log(listMtpFileTree);
//todo: handle error here
if (listMtpFileTreeError) {
console.error(listMtpFileTreeError);
return;
}
console.log(listMtpFileTreeData);*/

const {
error: listLocalFileTreeError,
data: listLocalFileTreeData
} = await mtpObj.listLocalFileTree({
folderPath: '/Users/ganeshr/Desktop/2',
recursive: true
});

if (listLocalFileTreeError) {
console.error(listLocalFileTreeError);
return;
}
console.log(listLocalFileTreeData);

//await mtpObj.deleteFile({ fileId: 57 });
//todo: handle error here
Expand Down

0 comments on commit 5b1578c

Please sign in to comment.