Skip to content

Instantly share code, notes, and snippets.

@dblodorn
Created January 25, 2024 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dblodorn/ca9bd68e445e8245ffed25e551980f88 to your computer and use it in GitHub Desktop.
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
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