Skip to content

Instantly share code, notes, and snippets.

@akirco
Created June 24, 2023 15:47
Show Gist options
  • Save akirco/7aee727cfd518f75a29ec83edadb360e to your computer and use it in GitHub Desktop.
Save akirco/7aee727cfd518f75a29ec83edadb360e to your computer and use it in GitHub Desktop.
use set to cache repeat solutions
/**
* @description Get file tree by a mtp device parent path.
* @param parentPath The root address can be passed in the string `/`.
* @returns `Array<FileInfo>` File info array
* @example
*
* [{
* name: '1',
* size: 0,
* type: 'FOLDER',
* id: 156,
* modificationdate: 1672041505,
* parent_id: 152,
* storage_id: 65537
* },
* {
* name: 'download.zip',
* size: 8893742,
* type: 'FILE',
* id: 158,
* modificationdate: 1673321627,
* parent_id: 152,
* storage_id: 65537
* }]
*
*/
// getList(parentPath: string): Array<FileInfo>;
export async function getImages(parentPath, visitedFolders = new Set()) {
if (visitedFolders.has(parentPath)) {
return [];
}
visitedFolders.add(parentPath);
const fileLists = await this.getFileList(parentPath);
const imageExtensions = /\.(jpg|png|gif|webp|jpeg)$/i;
let images = [];
const promises = [];
fileLists.forEach((target) => {
if (target.type === "FOLDER") {
const currentPath = parentPath + "/" + target.name;
promises.push(this.getImages(currentPath, visitedFolders));
} else if (target.type === "FILE" && imageExtensions.test(target.name)) {
images.push(target);
}
});
const results = await Promise.all(promises);
results.forEach((result) => {
images = images.concat(result);
});
return images;
}
@akirco
Copy link
Author

akirco commented Jul 29, 2023

你好吗?藤井君

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment