read method
Reads data from a file at the given path, optionally starting at
offset and limiting to count bytes.
path may be an MFS path or an /ipfs/<cid>[/sub] path. Reading a
directory is an error, matching Kubo files read.
Implementation
Future<Stream<List<int>>> read(String path, {int? offset, int? count}) async {
final cid = await _resolveAny(path);
if (cid == null) throw Exception('Path not found: $path');
if (cid.codec != 'raw') {
final type = await _unixfsType(cid);
if (type == Data_DataType.Directory || type == Data_DataType.HAMTShard) {
throw Exception('Path is a directory: $path');
}
}
// A missing root block surfaces as an error on the returned stream
// rather than throwing here, so consumers terminate instead of hanging.
final controller = StreamController<List<int>>();
final cursor = _ReadCursor(skip: offset ?? 0, limit: count);
unawaited(
_readRecursive(
cid,
controller,
cursor,
).then((_) => controller.close()).catchError((Object e) {
controller.addError(e);
// Close the stream after the error so consumers terminate
// instead of hanging forever.
return controller.close();
}),
);
return controller.stream;
}