get method

Future<Uint8List?> get(
  1. String cid, {
  2. String path = '',
})

Gets the content of a file or directory from IPFS.

cid is the Content Identifier of the file/directory path is an optional path within the directory

Implementation

Future<Uint8List?> get(String cid, {String path = ''}) async {
  try {
    // MODE: Public / Local / Custom -> Use HTTP Gateway exclusively
    if (_gatewayMode != GatewayMode.internal) {
      String url;
      switch (_gatewayMode) {
        case GatewayMode.public:
          url = 'https://ipfs.io/ipfs';
          break;
        case GatewayMode.local:
          url = 'http://127.0.0.1:8080/ipfs';
          break;
        case GatewayMode.custom:
          url = _customGatewayUrl;
          break;
        default:
          url = 'https://ipfs.io/ipfs';
      }
      _logger.debug('Retrieving via Gateway ($url): $cid');
      // Currently HttpGatewayClient is hardcoded to ipfs.io in its implementation?
      // We might need to update HttpGatewayClient to accept a base URL.
      // For now, assuming HttpGatewayClient logic will be updated or uses default.
      // Actually, let's just use the client we have, but we need to tell it where to look.
      // Since HttpGatewayClient instance is private and hardcoded, we should probably
      // update HttpGatewayClient to take a baseUrl in `get`.
      // Let's assume for this step we update HttpGatewayClient first?
      // Wait, I can't update HttpGatewayClient in this tool call.
      // I will just call it for now and fix it in next step.
      return await _httpGatewayClient.get(cid, baseUrl: url);
    }

    // MODE: Internal -> Use Native P2P
    // First check if we have the block locally
    final block = await _container.get<DatastoreHandler>().getBlock(cid);

    if (block != null) {
      if (path.isEmpty) {
        // Return the raw data if no path is specified
        return block.data;
      } else {
        // If this is a directory and a path is specified,
        // traverse the directory structure
        final node = MerkleDAGNode.fromBytes(block.data);
        if (node.isDirectory) {
          return await _resolvePathInDirectory(node, path);
        }
      }
    }

    // If not found locally, try to fetch from the network
    if (_container.isRegistered(BitswapHandler)) {
      final networkBlock = await _container.get<BitswapHandler>().wantBlock(
        cid,
      );
      if (networkBlock?.data != null) {
        return networkBlock!.data;
      }
    }

    // 3. HTTP Gateway Fallback (Hybrid Compatibility) - KEEP THIS for Internal Mode too?
    // Yes, "Hybrid Fallback" is a feature of Internal Mode.
    _logger.debug(
      'P2P retrieval failed, attempting HTTP gateway fallback for $cid',
    );
    final gatewayData = await _httpGatewayClient.get(cid);
    if (gatewayData != null) {
      return gatewayData;
    }

    return null;
  } catch (e) {
    // print('Error retrieving content for CID $cid: $e');
    return null;
  }
}