Created
January 25, 2024 22:47
-
-
Save dblodorn/ca9bd68e445e8245ffed25e551980f88 to your computer and use it in GitHub Desktop.
List all the files in a nested ipfs directory running a node locally
This file contains hidden or 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
import type { IPFSHTTPClient } from 'ipfs-http-client' | |
import all from 'it-all' | |
let ipfs: IPFSHTTPClient | |
async function getIpfsClient() { | |
if (!ipfs) { | |
const { create: createIpfsClient } = await import('ipfs-http-client') | |
ipfs = createIpfsClient({ | |
url: `http://localhost:5001/api/v0`, | |
}) | |
} | |
return ipfs | |
} | |
export async function getDirectoryListing(cidHash: string) { | |
const client = await getIpfsClient() | |
async function getAllContents(cidHash: string): Promise<string[]> { | |
const folder = await all(client.ls(cidHash)) | |
const directory = folder.filter((entry) => entry.name !== '.DS_Store') | |
const files = await Promise.all( | |
directory.map(async (entry) => { | |
if (entry.type === 'dir') { | |
const nestedContents = await getAllContents(entry.cid.toString()) | |
return nestedContents | |
} else { | |
return entry.path | |
} | |
}) | |
) | |
return files.flat() | |
} | |
const files = await getAllContents(cidHash) | |
return files | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment