Created
June 24, 2023 15:47
-
-
Save akirco/7aee727cfd518f75a29ec83edadb360e to your computer and use it in GitHub Desktop.
use set to cache repeat solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
你好吗?藤井君