readFile method

  1. @override
Future<Uint8List> readFile(
  1. Object path, {
  2. Context? ctx,
})
override

Reads bytes from a file relative to workingDirectory, optionally using ctx.

Throws an ArgumentError if the environment rejects the path, such as a relative path or an absolute path under the working directory that escapes it.

Implementation

@override
Future<Uint8List> readFile(Object path, {Context? ctx}) async {
  final String pathStr = _resolvePath(path);
  _requireInitialized();
  final http.Client client = _httpClient ?? http.Client();
  try {
    final Uri uri = Uri.parse(
      '$_effectiveApiUrl/sandbox/$_sandboxId/file',
    ).replace(queryParameters: <String, String>{'path': pathStr});
    final http.Response response = await client.get(uri, headers: _headers);
    if (response.statusCode >= 200 && response.statusCode < 300) {
      await _refreshActivity(client);
      return response.bodyBytes;
    }
    throw HttpException(
      'Failed to read file $pathStr (${response.statusCode}): '
      '${response.body}',
      uri: uri,
    );
  } finally {
    if (_httpClient == null) {
      client.close();
    }
  }
}