ls method

Future<List<Link>> ls(
  1. String cid
)

Lists the contents of an IPFS directory.

Implementation

Future<List<Link>> ls(String cid) async {
  try {
    // Get the block
    Block? block = await _container.get<DatastoreHandler>().getBlock(cid);
    if (block == null) {
      // Try fetching from network if not found locally
      final data = await _container.get<BitswapHandler>().wantBlock(cid);
      if (data == null) {
        throw Exception('Directory not found: $cid');
      }
      block = data; // data IS a Block
    }

    // Parse the directory node using MerkleDAGNode
    // Note: MerkleDAGNode must parse strict UnixFS to know if it's a directory
    final node = MerkleDAGNode.fromBytes(block.data);

    if (!node.isDirectory) {
      throw Exception('CID does not point to a directory: $cid');
    }

    // Convert directory links to Link objects
    return node.links;
  } catch (e) {
    // print('Error listing directory $cid: $e');
    return [];
  }
}