writeFile method
Writes content (String or List<int>) to a file relative to workingDirectory.
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<void> writeFile(Object path, Object content, {Context? ctx}) async {
if (content is! String && content is! List<int>) {
throw ArgumentError(
'content must be str or bytes, got ${content.runtimeType}',
);
}
final String pathStr = _resolvePath(path);
_requireInitialized();
final Uint8List data = encodeWriteFileContent(content);
final http.Client client = _httpClient ?? http.Client();
try {
await _ensureParentDirs(client, pathStr);
final Uri uri = Uri.parse(
'$_effectiveApiUrl/sandbox/$_sandboxId/file',
).replace(queryParameters: <String, String>{'path': pathStr});
final Map<String, String> uploadHeaders = <String, String>{
if (_effectiveApiKey.isNotEmpty)
'Authorization': 'Bearer $_effectiveApiKey',
'Content-Type': 'application/octet-stream',
};
final http.Response response = await client.post(
uri,
headers: uploadHeaders,
body: data,
);
if (response.statusCode < 200 || response.statusCode >= 300) {
throw HttpException(
'Failed to write file $pathStr (${response.statusCode}): '
'${response.body}',
uri: uri,
);
}
await _refreshActivity(client);
} finally {
if (_httpClient == null) {
client.close();
}
}
}